Basic Python Session 01
Basic Python Session 01
Run the above statements using the ‘Run’ Icon in the left, or press ‘Shift+Enter’ in
your keyboard. No output will be displayed.
In the statements above, the first assigns a string to a new variable named “mymes-
sage”; the second gives the integer 42 to n ; the third assigns the approximate value
of φ (a decimal value or float) to phi.
Insert a new cell below by clicking on the ‘+ code’ icon on your python notebook
menu bar. For each new code chunk below, insert a new cell.
There are other built-in data types such as complex, long, tuple, list, dictionary,...
1
BM659: Hands-on tutorial for Python DoMS@IITRoorkee
As a programmer choose meaningful names for your variables. Names can be long.
For e.g., use boiling_point_of_water = 100 instead of t = 100.
Syntax errors are mistakes made while typing the code (similar to spelling mistakes).
Such errors make it impossible for the interpreter (compiler) to understand what you
have coded. This makes the code un-executable. However, to HELP you correct the
error, the Python interpreter will display a message & highlight the error. You can
read the message, and appropriately correct the code.
>>> x=2+
SyntaxError: invalid syntax
Also, if you give illegal variable name, you will get a syntax error:
>>> 10dulk@r = 53 [ Press Shift+Enter]
SyntaxError: invalid syntax cannot use numbers at start of variable
names; @, $,&, cannot be part of variable name
Python’s keywords. They are predefined for use by python and cannot be used as
variable names.
2
BM659: Hands-on tutorial for Python DoMS@IITRoorkee
3. Operators
Rules of Precedence
Highest precedence
() (anything in brackets is done first)
** (exponentiation)
*, /, %, //
+, -
relational operators: <, >, <=, >=, !=, ==
logical not
logical and
logical or
Lowest precedence
Integer division vs. floating point division → Both same in Python 3.0
>>> 7/2 [Press shift+enter]
3.5
3
BM659: Hands-on tutorial for Python DoMS@IITRoorkee
Relational operators
>>> x = 3
>>> y = 3.0
>>> x == y
Observe the output and understand
Miscellaneous
>>> x=5
>>> y=x/2
>>> print(“when x=5, (x,y)= ”, x, y)
>>> x=7
>>> print(“when x=7, (x,y)= ”, x, y)
>>> del x The variable x is deleted from memory.
>>> print(x) What happens?
4. Comments in Python
We use hash (#) to indicate comments. Anything following # will be ignored by
Python interpreter. For example:
>>> 32 * 12 #this is a sample comment
>>> 32 #* 12 #-->multiplication is not done due to #
5. Data Type conversion
Sometimes we need to convert data from one type to another. For example, convert
number to a string or a string to a number.
>>> n = 2103
>>> m = str(n)
>>> print(m)
>>> n*n → This should work [ Press Shift+Enter]
4
BM659: Hands-on tutorial for Python DoMS@IITRoorkee
Now, Insert New Cell. In the new cell, add the following lines:
x = input("Enter any Number : ")
print('entered ', x)
print('calc1 ', x-2)
print('calc2 ', x/3)
Run the cell
5
BM659: Hands-on tutorial for Python DoMS@IITRoorkee
Now, go back to earlier cell. Modify program so that the full program looks as
follows:
x = input("Enter any Number : ")
print('entered ', x)
print('calc1 ', int(x)-2)
print('calc2 ', float(x)/3)
Run the cell
What happens when you input an integer? Does it give the correct output now?
What happens when you input a decimal number? Does it give the correct output
now? Can you identify the cause of the error?