0% found this document useful (0 votes)
28 views

Python Day 2 Practical Notes

This document contains notes on Python concepts like if/else statements, loops (for, while), nested loops and loop control statements. It includes examples of using these concepts with explanations and output.

Uploaded by

souvik singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Python Day 2 Practical Notes

This document contains notes on Python concepts like if/else statements, loops (for, while), nested loops and loop control statements. It includes examples of using these concepts with explanations and output.

Uploaded by

souvik singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

11/16/22, 3:56 PM python notes 2 - Jupyter Notebook

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

localhost:8888/notebooks/python notes 2.ipynb# 1/9


11/16/22, 3:56 PM python notes 2 - Jupyter Notebook

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

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

non of the above

localhost:8888/notebooks/python notes 2.ipynb# 2/9


11/16/22, 3:56 PM python notes 2 - Jupyter Notebook

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)

enter a odd number : 2


please enter only odd number
enter a odd number : 4
please enter only odd number
enter a odd number : 6
please enter only odd number
enter a odd number : 3
congratulations 3 is a odd number

for loop

localhost:8888/notebooks/python notes 2.ipynb# 3/9


11/16/22, 3:56 PM python notes 2 - Jupyter Notebook

In [79]: 1 s="vaishnavi"
2 for i in s:
3 print(i)

v
a
i
s
h
n
a
v
i

In [84]: 1 l=["hi","vaishnavi","how uh" ]


2 for i in l:
3 print(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

Loop control statements

break

localhost:8888/notebooks/python notes 2.ipynb# 4/9


11/16/22, 3:56 PM python notes 2 - Jupyter Notebook

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

In [164]: 1 val=int(input("please enter a number"))


2 for i in range(1,val+1):
3 for j in range(1,i+1):
4 print(j,end="")
5 print()

please enter a number5


1
12
123
1234
12345

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

In [28]: 1 #creating a programm which gives length


2 l=["1","2","vaihsnavi",3.2,"26","7"]
3 length=0
4 i=0
5 try:
6 while l[i]:
7 length=length+1
8 i=i+1
9 except Exception as e:
10 print(length)

localhost:8888/notebooks/python notes 2.ipynb# 6/9


11/16/22, 3:56 PM python notes 2 - Jupyter Notebook

In [34]: 1 n=int(input("enter a number "))


2 i=1
3 while i<=n:
4 j=1
5 while j<=i:
6 print(i,end=" ")
7 j+=1
8 i+=1
9 print()

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

In [10]: 1 #programm of a small game


2 import random
3 nump=random.randint(1000,9999)
4 print (nump)
5 n=int(input("please enter your four digit numbers: "))
6 while n!=10:
7 num=nump
8 cor=0
9 while num%10:
10 numc=num%10
11 nc=n%10
12 num=num//10
13 n=n//10
14 if numc==nc:
15 cor+=1
16 if cor==4:
17 print("congrats! you guessed it right")
18 break
19 else:
20 print("%d digits were guess right" %cor)
21 n=int(input("please enter your four digit numbers: "))
22 else:
23 print("you quit the game")

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 [25]: 1 from array import *


2 arr=array("b",[1,2,3,4])
3 print(arr)

array('b', [1, 2, 3, 4])

In [26]: 1 arr=array("B",[1,2,3,4])
2 print(arr)

array('B', [1, 2, 3, 4])

In [30]: 1 arr.buffer_info()

Out[30]: (2510742607664, 4)

In [32]: 1 for i in arr:


2 print(i)

1
2
3
4

In [33]: 1 arr.reverse()

In [34]: 1 print(arr)

array('B', [4, 3, 2, 1])

In [35]: 1 print(arr[3])

In [2]: 1 from array import*


2 arr=array("i",[])
3 x=int(input("enter size of array "))
4 print("enter %d elemets" %x)
5 for i in range(x):
6 n=int(input())
7 arr.append(n)
8 print(arr)

enter size of array 4


enter 4 elemets
1
2
3
4
array('i', [1, 2, 3, 4])

Functions

localhost:8888/notebooks/python notes 2.ipynb# 8/9


11/16/22, 3:56 PM python notes 2 - Jupyter Notebook

In [3]: 1 def welcome():


2 print("Good morning")

In [4]: 1 welcome()

Good morning

In [5]: 1 def addition(a,b):


2 total=a+b
3 print("the sum is" ,total)

In [8]: 1 addition(3,5)

the sum is 8

In [9]: 1 x=1085369
2 y=8745774
3 addition(x,y)

the sum is 9831143

localhost:8888/notebooks/python notes 2.ipynb# 9/9

You might also like