PS: These are notes, there could be grammar error and spelling errors.
Last time had got until here,
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.
#!/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)
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 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:
Similar many programming language although below is reference table.
|<||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:
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
When you perform operation on same variable where you are storing value for example.
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:
As we were saw previously we were converting string input into float and integers. So is a example of how you do it.