PART - 2 L Python
PART - 2 L Python
Programming
Part 2
Shrihari A
IIT Guwahati
Output:
The value of Pl is 3.142
The value of Pl is approximately 3.14.
Latest Python Version is: 3
Python : 3000
Guided Activity 1: Using Jupyter Notebook to create and execute Python Program
• Programming Fundamentals
- Identifiers
- Variables
- Data types
- Operators etc
• No declaration of variables
Types of Comments:
Operators Description
Example
+ Additive operator (also used for String concatenation) 2+3=5
- Subtraction operator 5-3=2
** Exponentiation 10 ** 1000
- Used in conditional statements to compare values and take action depending on the
result
Operators Description
-- Equal to
< Less than
> Greater than
<= Lesser than or equal to
>= Greater than or equal to
I= Not equal to
<> Similar to Not equal to
• Multiple Assignments - Same value can be assigned to more than one variable
Ex.1: Students Ram, Sham, John belong to semester 6 Ex.2: a, b, c = 10, 20, 30 is same
Ram = Sham = John = 6 as a = 10, b = 20, c = 30
Operators Description
& Binary AND
I Binary OR
I\
Binary XOR
~ Binary Ones Complement
<< Binary Left Shift
>> Binary Right Shift
Operator Meaning
and Short Circuit-AND
or Short Circuit-OR
Operators Description
In Returns to true if it finds a variable in given sequence else false
Returns to true if it does not find a variable in given sequence
not in
else false
• Identity Operators
- Are used to compare memory locations of 2 objects
Operators Description
Returns to true if variables on either side of operator are
IS
referring to same object else false
Returns to false if variables on either side of operator are
is not
referring to same object else true
str b = "Hello"
Output:
print("Type of 'str_b':", type(str_b))
Type of 'int_a': <class 'int'>
Type of 'str_b': <class 'str'>
list_c = []
Type of 'list_c': <class 'list'>
print("Type of 'list_c':", type(list_c))
• if statement checks for a condition and if that is found true a particular set of
instructions gets executed
Syntax:
Example:
x=8
if condition1:
if X < 10: statement( s)
print("Value of x is %d" %x) else:
v a r = 10 statement( s)
if v a r > 5:
print ("Hi") # line belongs to if block
print("l'm out of if")
Output:
Value of x is 8
Hi
I'm out of if
• While Loop
• For Loop
• Range
• Break
• Continue
• Pass
Syntax:
while condition:
Example: statement( s)
var=5
while var>O: Output:
print ("I'm in iteration",var) I'm in iteration 5
var-=1 I'm still in while
if var==2: I'm in iteration 4
break I'm still in while
print ("I'm still in while") I'm in iteration 3
print ("I'm out of while loop") I'm out of while loop
Syntax:
1
Example: 2
Sita
for counter in 1,2,'Sita', 7,'Ram',5: 7
print(counter) Ram
5
for ch in "Hello World": print(ch.upper()) HELLO Prints all the characters in the
W O R LO string converting them to upper
case
• Break Statement:
- Terminates the loop statement and transfers execution to the statement immediately
following the loop.
Example:
var =3
while var> 0: Output:
print ("I'm in iteration ",var) I'm in iteration 3
var-= 1 I'm out of while loop
if var ==2:
break
print ("I'm still in while")
print ("I'm out of while loop")
var= 3
while var > 0: Output:
print ("I'm in iteration ", var)
I'm in iteration 3
var-= 1
I'm in iteration 2
if var ==2:
I'm still in while
continue
I'm in iteration 1
print ("I'm still in if block")
I'm still in while
print ("I'm still in while")
print ("I'm out of while loop") I'm out of while loop
- Used when a statement is required syntactically but do not want any command
or code to execute or if the code need to be implemented in future.
Example:
X = "Joy"
if x == "John": Output:
print ("Name:",x)
elif x == "Joy": INo Output
pass
else:
print ("in else")