Day 2

PS: These are notes, there could be grammar error and spelling errors.

Last time had got until here,

We don’t need specify data type with python something similar to javascript where we use var for every type of variable.(const and let are new ES6 standards, although they are not new but new)

So basically

1
2
3
a = 10
b = 1.0
c = "test"

in above a become integer, b becomes float and c becomes string.

Reading input from the Keyboard

For taking input from client we can input("string to show while taking input"). PS: while taking input it would always be strings similar to command line input in C. So we would type cast it into required datatype. For example integer or float. As it is already string we don’t need to typecast if we need a string.

Some mathematical operations BODMAS

python operations would follow BODMAS, and we can evaluate any operations as follows: We would create si.py file with below text. Please note #!/usr/bin/env python3 is very important line without which you would get an error in your code while running file.(Saying with experience)

1
2
3
4
5
6
#!/usr/bin/env python3
p = float(input("Enter amount: "))
r = float(input("Enter Interest rate: "))
n =  int(input("Enter period: "))
si = (p*r*n)/100
print("Simple Interest: %.2f" % si)

Multiple assignments on variable

Is a way to assign value to multiple in a single go(Title itself makes it clear)

1
2
3
4
5
>>> a , b = "test", "test1"
>>> a
'test'
>>> b
'test1'

A question which is always part of any programming language you learn in college, how to swap two number with and without using third variable. In python because of multiple assignment approach things becomes easy.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
>>> a , b = "test", "test1"
>>> a
'test'
>>> b
'test1'
>>> a , b = b, a
>>> a
'test1'
>>> b
'test'

Formating our printed strings

There are several ways to format strings one of them we saw before in codes above:

  • using %
1
print("Simple Interest: %.2f" % si)
  • using .format
1
2
3
4
5
>>> name = "Python"
>>> typeof = "snake"
>>> msg = "{0} is {1}.".format(name, typeof)
>>> print(msg)
Python is snake.
  • using f-strings(More to read:link)
1
2
3
4
5
>>> name = "Python"
>>> typeof = "snake"
>>> msg = f"{name} is {typeof}."
>>> print(msg)
Python is snake.

I find f-string more easy because it somewhat similar syntax to template string in javascript.

Operators and expressions

In Python most of the lines you will write will be expressions. Expressions are made of operators and operands. An expression is like 2 + 3 .

Operators

Operators are symbols which tell python interpreter to do mathematical or logical operations ie. addition, subtraction and etc.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# addition
>>> 2 + 3
5
# subtraction
>>> 23 - 3
20
# divide
>>> 22.0 / 12
1.8333333333333333
# modulo
>>> 14 % 3
2

Example why python is awesome

1
2
3
#!/usr/bin/env python3
days = int(input("Enter days: "))
print("Months = %d Days = %d" % (divmod(days, 30)))

Save above code in days.py file. When you run this file as ./days.py it would ask you to enter days you enter let say 50 then it would print both months and remaining days with single function because the divmod(num1, num2) function returns two values , first is the division of num1 and num2 and in second the modulo of num1 and num2.

Above code could also be written as follows the longer way:

1
2
3
4
5
#!/usr/bin/env python3
days = int(input("Enter days: "))
months = days / 30
days = days % 30
print("Months = %d Days = %d" % (months, days))

Relational Operators

Similar many programming language although below is reference table.

Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal to

Some examples also:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
>>> 1 < 2
True
>>> 3 > 34
False
>>> 23 == 45
False
>>> 34 != 323
True
# '//' Gives floor division result, could be useful at many places where you would call floor function
>>> 4.0 // 3 
1.0
>>> 4.0 / 3
1.3333333333333333

Logical Operators

Note for self: There is difference between logical and bitwise operators. Please don’t get confused. In logical operators when first value is not true then it would return second value

1
2
3
4
5
6
7
8
>>> 1 and 4
4
>>> 1 or 4
1
>>> -1 or 4
-1
>>> 0 or 4
4

Shorthand Operators

When you perform operation on same variable where you are storing value for example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
>>> a = 12
>>> a += 13
>>> a
25
>>> a /= 3
>>> a
8.333333333333334
>>> a += (26 * 32)
>>> a
840.3333333333334

Expressions

General rule whenever you write any operator give spaces between them. Evaluation of expression follows BODMAS. For example in following code evaluation of x,y,z would be:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#!/usr/bin/env python3
a = 9
b = 12
c = 3
x = a - b / 3 + c * 2 - 1
y = a - b / (3 + c) * (2 - 1)
z = a - (b / (3 + c) * 2) - 1
print("X = ", x)
print("Y = ", y)
print("Z = ", z)

For x:

9 - 12 / 3 + 3 * 2 -1
9 - 4 + 3 * 2 - 1
9 - 4 + 6 - 1
5 + 6 - 1
11 - 1
10

for y:

a - b / (3 + c) * (2 - 1)
9 - 12 / (3 + 3) * (2 - 1)
9 - 12 / 6 * 1
9 - 2 * 1
9 - 2
7

for z:

a - (b / (3 + c) * 2) - 1
9 - (12 / (3 + 3) * 2) - 1
9 - (12 / 6 * 2) - 1
9 - (2 * 2) - 1
9 - 4 - 1
4

Type Conversions

As we were saw previously we were converting string input into float and integers. So is a example of how you do it.

1
2
3
float(string) -> float value
int(string) -> integer value
str(integer) or str(float) -> string representation