Conditional Statement
Conditional Statement >> decision making statement if if else if elif else
In [ ]:
Syntax
if condition : # True or False statement 1 statement 2 statement 3 else: # executed commands if condiiton is
false statement 1 statement 2 statement 3 if condition : # True or False statement 1 statement 2 statement 3
elif: # True or False statement 1 statement 2 statement 3 else: statement 1 statement 2 statement 3if condition:
# True or false statement 1
In [ ]:
Valid syntax: if if >> else if >> elif if >> elif >> elif >> elif if >> elif >> elif >> else invalid syntax: if >> else >> elif
In [1]: print("python")
print("Data Science")
python
Data Science
In [2]: print("python", end= " ")
print("Data Science")
python Data Science
In [3]: print("python", end= ", ")
print("Data Science")
python, Data Science
In [4]: print("python", end= " & ")
print("Data Science")
python & Data Science
In [ ]:
In [ ]:
In [5]: a = 20
b = 30
if a > b: #true
print(" a is greater than b")
else :
print( "a is not greaterthan than b")
a is not greaterthan than b
In [ ]:
In [6]: a = int(input(" enter the value of a = "))
b = int(input(" enter the value of b = "))
if a > b :
print(" a is greater than b")
else:
print( "a is not greaterthan b")
a is not greaterthan b
In [7]: a = int(input(" enter the value of a = "))
b = int(input(" enter the value of b = "))
if a!= b:
print("both the numbers are not same")
both the numbers are not same
In [8]: a =30
b = 40
c = 35
if a > b and a > c:
print( " a is the laregest number")
elif b > a and b > c:
print("b is the largest number")
else:
print("c is the largest number")
b is the largest number
In [9]: a = int(input(" enter the value of a = "))
b = int(input(" enter the value of b = "))
c = int(input(" enter the value of c = "))
if a > b and a > c:
print( " a is the laregest number")
elif b > a and b > c:
print(" b is the largest number")
else:
print(" c is the largest number")
c is the largest number
In [ ]:
Short Hand if-else Statement
In [ ]:
In [10]: a = 5
b = 6
if a > b:
print (" a is greaterthan number")
else:
print(" a is lessthan b")
a is lessthan b
In [11]: a = 5
b = 6
print ("a is greaterthan number") if a > b else print("a is lessthan b")
a is lessthan b
In [ ]:
nested if - else statement
if block indie if block
In [12]: a =80
if a > 50:
print( "a is greater than 50")
if a > 70:
print("a is greaterthan 70")
if a > 90:
print( "a is greaterthan 90")
else:
print("a is lessthan or equalto 90")
else:
print("a lessthan or equal to 70")
else:
print("a is lessthan or equl to 50")
a is greater than 50
a is greaterthan 70
a is lessthan or equalto 90
In [ ]:
In [13]: a = 23
if a % 2 ==0:
print(" a is an even number")
else:
print("a is odd number")
a is odd number
In [53]: # leap year
year = int(input( " Enter the year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print( year, "is a leap year")
else:
print( year, " is not leap year")
2024 is a leap year
In [ ]:
In [14]:
1
2
3
4
In [ ]:
In [ ]:
In [ ]:
For Loop
For loop works like an iterator
for variable in seqience: Statement 1 Statement 2 Statement 3
code 1
code 2
for value in seqience: Statement 1 Statement 2 Statement 3
for j in seqience: Statement 1 Statement 2 Statement 3
for char in seqience: Statement 1 Statement 2 Statement 3
In [54]: for i in range (1, int(input(" enter number of iterations"))):
print(i)
1
2
3
In [16]: for i in range(5):
print(i)
0
1
2
3
4
In [17]: str1= "Python"
for i in range(5):
print(str1)
Python
Python
Python
Python
Python
In [18]: for char in "python":
print(char)
p
y
t
h
o
n
In [19]: for index, value in enumerate("python"):
print(f" char at index {index} is {value}")
char at index 0 is p
char at index 1 is y
char at index 2 is t
char at index 3 is h
char at index 4 is o
char at index 5 is n
In [ ]:
for - else
Else block will be executed only when the loop completes its all iterations
range
range(start, stop, step) range(start, stop)>>> default step size is oneRange(0,6)>>> 0 included, 6 excluded>>>
0,1,2,3,5
In [20]: for i in range(5):
if i == 3:
break
print(i)
else :
print(" all iterations completed")
0
1
2
In [21]: for i in range(5):
if i == 3:
print(i)
else :
print(" all iterations completed")
3
all iterations completed
In [ ]:
In [22]: # seperate list of integers, float and string
list1=[ 3,5.6,7, "python", 2.9, 4, "data", 100,200, 4.56]
list1
int_list=[]
float_list =[]
str_list=[]
for i in list1:
if type(i)==int:
int_list.append(i)
elif type(i) == float:
float_list.append(i)
else:
str_list.append(i)
print(" list of integres >> " , int_list)
print(" list of float >> " , float_list)
print(" list of string >> " , str_list)
list of integres >> [3, 7, 4, 100, 200]
list of float >> [5.6, 2.9, 4.56]
list of string >> ['python', 'data']
In [23]: list1=[ 3,5.6,7, "python", 2.9, 4, "data", 100,200, 4.56]
int_list = [ i for i in list1 if type(i) ==int]
float_list = [ i for i in list1 if type(i) == float]
str_list = [ i for i in list1 if type(i) == str]
print(" list of integres >> " , int_list)
print(" list of float >> " , float_list)
print(" list of string >> " , str_list)
list of integres >> [3, 7, 4, 100, 200]
list of float >> [5.6, 2.9, 4.56]
list of string >> ['python', 'data']
In [ ]:
Loop Control Statement
1. break
2. continue
3. pass
1. Break Statement
In [10]: for i in range(1, 6):
print(i, "$")
if i == 4:
print(i)
1 $
2 $
3 $
4 $
4
5 $
In [2]: for i in range(1, 6):
if i == 4:
break
print(i)
1
2
3
2. Continue
In [3]: for i in range(1, 6):
if i == 4:
continue
print(i)
1
2
3
5
In [ ]:
3. Pass
In [1]: for i in range(1, 6):
if i == 4:
pass
print(i)
1
2
3
4
5
In [ ]:
Factorial
In [24]: n = int(input(" enter the number ==" ))
fact =1
for i in range(1, n+1):
fact = fact*i
print(f" factorial of {n} is {fact}")
factorial of 5 is 1
factorial of 5 is 2
factorial of 5 is 6
factorial of 5 is 24
factorial of 5 is 120
In [25]: n = int(input(" enter the number ==" ))
fact =1
for i in range(1, n+1):
fact = fact*i
print(f" factorial of {n} is {fact}")
factorial of 5 is 120
In [ ]:
write a program to print Fibonacci Series
0 1 1 2 3 5 8 13 21 34 ...
In [15]: # using for loop
n = int(input("enter the value of n : "))
first_term = 0
second_term = 1
print( first_term, "\n",second_term)
for i in range(0, n):
new = first_term+ second_term
print(new)
first_term = second_term
second_term = new
0
1
1
2
3
5
8
13
21
In [ ]:
In [ ]:
In [ ]:
Write a program to check if number is prime or not
In [27]: n = int(input(" Enter the number : " ))
for i in range (2, n):
if n % i == 0:
print(f"{n} is not a prime number")
break
else :
print(f"{n} is a prime number")
7 is a prime number
In [ ]:
In [ ]:
In [2]: # print all prime numbers less than n
n = int(input("Enter the number: "))
for num in range(2, n):
for i in range(2, num):
if num % i == 0:
break
else:
print(num)
2
3
5
Nested Loop
For loop inside for loopfor var1 in sequence1: Stement 1 Statement2 for var2 in sequence2: statement 3
statement 4 for var3 in sequence3: statement 5 statement 6
In [30]: for i in range(5):
print("_"*50)
print(f" we are in main loop ,{i}" )
for j in range(4):
print(f" we are in nested loop ,{j}" )
__________________________________________________
we are in main loop ,0
we are in nested loop ,0
we are in nested loop ,1
we are in nested loop ,2
we are in nested loop ,3
__________________________________________________
we are in main loop ,1
we are in nested loop ,0
we are in nested loop ,1
we are in nested loop ,2
we are in nested loop ,3
__________________________________________________
we are in main loop ,2
we are in nested loop ,0
we are in nested loop ,1
we are in nested loop ,2
we are in nested loop ,3
__________________________________________________
we are in main loop ,3
we are in nested loop ,0
we are in nested loop ,1
we are in nested loop ,2
we are in nested loop ,3
__________________________________________________
we are in main loop ,4
we are in nested loop ,0
we are in nested loop ,1
we are in nested loop ,2
we are in nested loop ,3
In [31]: for i in range(3):
for j in range(2):
print("*")
*
*
*
*
*
*
In [32]: for i in range(3):
for j in range(2):
print("*", end =" ")
* * * * * *
In [6]: for i in range(5):
print(f"i=={i}")
for j in range(5):
print("*", end =" ")
print()
i==0
* * * * *
i==1
* * * * *
i==2
* * * * *
i==3
* * * * *
i==4
* * * * *
In [34]: for i in range(5):
for j in range(5):
print("*", end =" ")
print()
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
In [7]: n=5
for i in range(1, n+1):
print(f"i=={i}")
for j in range(i):
print("*", end =" ")
print()
i==1
*
i==2
* *
i==3
* * *
i==4
* * * *
i==5
* * * * *
In [13]: n=5
for i in range(1, n+1):
for j in range(i):
print("*", end =" ")
print()
*
* *
* * *
* * * *
* * * * *
In [ ]: print(1)
In [10]: n=5
for i in range(1, n+1):
for j in range(i):
print(i, end =" ")
print()
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
In [38]: n=5
x= 1
for i in range(1, n+1):
for j in range(i):
print(x, end =" ")
x= x+ 1
print()
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
In [39]: n=5
for i in range(n,0,-1):
print(i)
5
4
3
2
1
In [40]: n = 6
for i in range(n, 0, -1):
print(i)
for j in range(i):
print("*", end=" " )
6
* * * * * * 5
* * * * * 4
* * * * 3
* * * 2
* * 1
*
In [41]: for i in range(1, n+1):
for j in range(i):
print(f"i== {i}")
print(f" j== {j}")
print("*", end =" ")
print()
for i in range(n-1, 0, -1):
for j in range(i):
print("*", end= " ")
print()
i== 1
j== 0
*
i== 2
j== 0
* i== 2
j== 1
*
i== 3
j== 0
* i== 3
j== 1
* i== 3
j== 2
*
i== 4
j== 0
* i== 4
j== 1
* i== 4
j== 2
* i== 4
j== 3
*
i== 5
j== 0
* i== 5
j== 1
* i== 5
j== 2
* i== 5
j== 3
* i== 5
j== 4
*
i== 6
j== 0
* i== 6
j== 1
* i== 6
j== 2
* i== 6
j== 3
* i== 6
j== 4
* i== 6
j== 5
*
* * * * *
* * * *
* * *
* *
*
In [42]: n = 6
for i in range(1, n+1):
for j in range(i):
print("*", end =" ")
print()
for i in range(n, 0, -1):
for j in range(i):
print("*", end= " ")
print()
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
In [43]: n = 6
for i in range(1, n+1):
for j in range(i):
print("*", end =" ")
print()
for i in range(n-1, 0, -1):
for j in range(i):
print("*", end= " ")
print()
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
In [52]: # print N natural number
n= int(input("Enter the number: "))
for i in range(1, 6):
print(i)
1
2
3
4
5
In [55]: # print all prime numbers less than n
n = int(input("Enter the number: "))
for num in range(2, n):
for i in range(2, num):
if num % i == 0:
break
else:
print(num)
2
3
5
7
In [ ]:
In [ ]:
While loop
while loop is used to execute a block of code until a given condition is true while condition: code condiiton is:
1. True >> Execute block of code 2. False >> Exit while loop
In [44]: str1 = "python"
for i in str1:
print(i)
p
y
t
h
o
n
In [45]: str1= "python"
i = 0
while i < len(str1):
print(str1[i])
i+=1
p
y
t
h
o
n
In [ ]:
In [46]: n = 10
num = 1
addition = 0
while num <= n:
addition = addition+num
num=num+1
print(f"sum of first {n} numbers is {addition}")
sum of first 10 numbers is 55
In [47]: n = 10
num = 1
addition = 0
while num <= n:
addition = addition+num
num=num+1
print(f"sum of first {n} numbers is {addition}")
sum of first 10 numbers is 1
sum of first 10 numbers is 3
sum of first 10 numbers is 6
sum of first 10 numbers is 10
sum of first 10 numbers is 15
sum of first 10 numbers is 21
sum of first 10 numbers is 28
sum of first 10 numbers is 36
sum of first 10 numbers is 45
sum of first 10 numbers is 55
In [48]: # print N natural number
n= int(input("Enter the number: "))
i= 1
while i <=n:
print(i)
i+=1
1
2
3
4
5
In [51]: # print N natural number
n= int(input("Enter the number: "))
i= 1
while i <=n:
print(i)
i+=1
1
2
3
4
5
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [49]: # using while loop
n = int(input("enter the value of n : "))
first_term = 0
second_term = 1
count = 0
print( first_term, "\n",second_term)
while count < n:
new = first_term+ second_term
print(new)
first_term = second_term
second_term = new
count =count+1
0
1
1
2
3
5
8
13
21
In [50]: # Fibonacci series(if n is -ve)
n = int(input("enter the value of n : "))
first_term = 0
second_term = 1
count = 0
print(first_term, "\n",second_term)
while count < n:
if count <=1:
new = count
else:
new = first_term+ second_term
print(new)
first_term = second_term
second_term = new
count =count+1
0
1
In [ ]:
In [ ]: