Python Day 2 Practical Notes
Python Day 2 Practical Notes
If statement
In [12]: 1 #if given "if" statement is true it comes under if block and will execute gi
2 a=100
3 if a > 50:
4 print("if statement is executed")
5 print("if statement will not executed")
if statement is executed
if statement will not executed
In [13]: 1 #if given "if" statement is false it does not falls under if block and will
2 #but it will execute other statement which is outside the block
3 a=100
4 if a > 150:
5 print("if statement is executed")
6 print("if statement will not executed")
If else statement
In [17]: 1 #if the statement in "if" block is true then "if" block will be execeuted or
2 a=20
3 if a%2 == 0:
4 print("even")
5 else:
6 print("odd")
even
In [19]: 1 #if the statement in "if" block is false then "if" block will not execeute i
2 a=23
3 if a%2 == 0:
4 print("even")
5 else:
6 print("odd")
odd
Nested if statement
In [5]: 1 #it is used when you want to filter variables multiple times
2 a=50
3 if a >10:
4 print("a is greater than 50")
5 if a%2==0:
6 print("a is even number less than 50")
7 else:
8 print("a is either less than 50 or even number")
a is greater than 50
a is even number less than 50
In [8]: 1 a=50
2 if a >63:
3 print("a is greater than 50")
4 if a%2==0:
5 print("a is even number less than 50")
6 else:
7 print("a is either less than 50 or even number")
If ,elif,else statement
In [11]: 1 var="z"
2 if var== "a":
3 print("a is a vowel")
4 elif var== "e":
5 print("e is a vowel")
6 elif var== "i":
7 print("i is a vowel")
8 elif var== "o":
9 print("o is a vowel")
10 elif var== "u":
11 print("u is a vowel")
12 else:
13 print("non of the above")
In [12]: 1 var="o"
2 if var== "a":
3 print("a is a vowel")
4 elif var== "e":
5 print("e is a vowel")
6 elif var== "i":
7 print("i is a vowel")
8 elif var== "o":
9 print("o is a vowel")
10 elif var== "u":
11 print("u is a vowel")
12 else:
13 print("non of the above")
o is a vowel
Loops
whileloop
In [61]: 1 #creating user input loops
2 val=int(input("enter a odd number : "))
3 while val%2==0:
4 print("please enter only odd number")
5 val=int(input("enter a odd number : "))
6 else:
7 print("congratulations %d is a odd number"%val)
for loop
In [79]: 1 s="vaishnavi"
2 for i in s:
3 print(i)
v
a
i
s
h
n
a
v
i
hi
vaishnavi
how uh
In [87]: 1 t=("python","is","easy")
2 for i in t:
3 print(i)
python
is
easy
nested loops
In [107]: 1 x=[[1,2,3],["a","b","c"],[4,5,6]]
2 for i in x:
3 for j in i:
4 print(j,end="")
5 print()
123
abc
456
break
In [114]: 1 x=[1,2,3,4,5,6,7,8]
2 for i in x:
3 if i==6:
4 break
5 print(i)
1
2
3
4
5
continue
In [120]: 1 x=[1,2,3,4,5,6,7,8,9,10]
2 for i in x:
3 if i<6:
4 continue
5 print(i)
6
7
8
9
10
For loops
In [136]: 1 sum=0
2 for i in range(0,21):
3 if i%2==0:
4 sum=sum+i
5 print(sum,end=" ")
110
While loops
localhost:8888/notebooks/python notes 2.ipynb# 5/9
11/16/22, 3:56 PM python notes 2 - Jupyter Notebook
In [191]: 1 i=1
2 while i<=10:
3 print("vaishnavi")
4 i+=1
vaishnavi
vaishnavi
vaishnavi
vaishnavi
vaishnavi
vaishnavi
vaishnavi
vaishnavi
vaishnavi
vaishnavi
In [1]: 1 sum=0
2 i=1
3 while i<=10:
4 i+=1
5 sum=sum+i
6 print(sum)
65
enter a number 9
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
7 7 7 7 7 7 7
8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9
1517
please enter your four digit numbers: 2347
1 digits were guess right
please enter your four digit numbers: 2311
1 digits were guess right
please enter your four digit numbers: 10
you quit the game
Array
localhost:8888/notebooks/python notes 2.ipynb# 7/9
11/16/22, 3:56 PM python notes 2 - Jupyter Notebook
In [26]: 1 arr=array("B",[1,2,3,4])
2 print(arr)
In [30]: 1 arr.buffer_info()
Out[30]: (2510742607664, 4)
1
2
3
4
In [33]: 1 arr.reverse()
In [34]: 1 print(arr)
In [35]: 1 print(arr[3])
Functions
In [4]: 1 welcome()
Good morning
In [8]: 1 addition(3,5)
the sum is 8
In [9]: 1 x=1085369
2 y=8745774
3 addition(x,y)