Learning python through pym 12-08-2019
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
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)
Multiple assignments on variable
Is a way to assign value to multiple in a single go(Title itself makes it clear)
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.
Formating our printed strings
There are several ways to format strings one of them we saw before in codes above:
- using %
|
|
- using .format
- using f-strings(More to read:link)
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.
Example why python is awesome
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:
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:
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
Shorthand Operators
When you perform operation on same variable where you are storing value for example.
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:
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.