0% found this document useful (0 votes)
4 views6 pages

Basic Python Session 01

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views6 pages

Basic Python Session 01

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

BM659: Hands-on tutorial for Python DoMS@IITRoorkee

Go To https://colab.research.google.com/. And open a new notebook.

Session I: Variables and data types

Open a New iPython Notebook, named “Session 1”

1. Variables and data types


In the iPython notebook cell, type the following (DON’T type ‘>>>’)

>>> mymessage = 'I am going to learn Python'


>>> n = 42
>>> phi = 1.6180339

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.

Notion of pointers: The variable names points to the data.


mymessage  ‘I am going to learn Python’ n  42 phi  1.6180339

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.

To display the values of the variables, use print function


>>> print(mymessage)
>>> print(phi)

Run the above statements, and view results.

The type of variable is the type of value it refers to:


>>> type(mymessage) [ Press Shift+Enter]
<class 'str'>

>>> type(phi) [ Press Shift+Enter]


<class 'float'>

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.

2. Syntax Errors, Variable names, and Keywords

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.

Let’s try to make errors and correct them.

>>> mystr= "IIT' [ Press Shift+Enter]


SyntaxError: EOL while scanning string literal
 Strings to be enclosed using " " or ' '

>>> x=2*pi [ Press Shift+Enter]


NameError: name 'pi' is not defined  we have used pi without
defining it

>>> 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

>>> class = 'sachin tendulkar' [ Press Shift+Enter]


SyntaxError: invalid syntax  This is because class is one of
Python’s keywords.

Python’s keywords. They are predefined for use by python and cannot be used as
variable names.

You can view the keywords by typing


>>> help('keywords')

2
BM659: Hands-on tutorial for Python DoMS@IITRoorkee

3. Operators

• The mathematical operators +, -, *, / , **, and // perform addition, subtraction,


multiplication, division, exponentiation (power), and floor division.
• There are relation operators such: <, >, <=, >=, !=, and ==, representing strictly
less than, strictly greater than, less than or equal to, greater than or equal to, not
equal to and if equals.

Try the following


>>> 16 / 2 * 3 + 2 [Press shift+enter]
result is?
>>> 3 * 16 / 2 + 2 [Press shift+enter]
Is the result as expected?

>>> 2**3 [Press shift+enter] (** Computes square)


>>> 5*5+2**3-1 [Press shift+enter]
Is the result as expected?
>>> 5*5+2**(3-1) [Press shift+enter]
Is the result as expected?

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

>>> 7.0/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

String Operation (Mathematical operations cannot be done on strings_


>>> college = 'IIT'
>>> city = 'Roorkee'
>>> college+city [ Press Shift+Enter]
'IITRoorkee '
 The + operator on string, concatenates or joins strings together.

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

Insert New Cell


>>> m*m [This should throw an error since m is string]
Insert New Cell
>>> float(m)/2 [This should work, converts m to floating point (decimal)]

6. Getting User input


Insert New Cell
print("This is an interactive program")
name = input("What is your name? ")
print('Hello ' + name)
Run the cell

In the Output area, the following output should be displayed:


This is an interactive program
What is your name? <Enter your Name and Press Enter>
Hello <Name entered>

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

What happens? Is x-2 computed correctly? Is x/3 computed correctly?


Python interpreter will throw an error since all user inputs are taken as
STRINGS BY DEFAULT. We need to convert it into a number if we want to do
any numerical computations on it.

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?

You might also like