Control statement in python Examples

Last Updated On Friday 21st Jan 2022

Control statements: if…elif…else, while, for, break and continue

if elif else in python

	a = int(input("Enter Your Height in (cm): "))
if a >= 180:
    print("Good Height")
elif a == 178:
    print("Standard height")
elif 160 < a < 178:
    print("Medium height")
else:
    print("Hey Small")
	
	# Enter Your Height in (cm): 176
# Medium height
	

While statement python

	abc = 1

while abc <= 100:
    abc = abc*2
    print(abc)
	
	2
4
8
16
32
64
128
	

For loop for iteration

	for item in [1, 2, 3, 4, 5]:
    print(item)
letters = ["A", "B", "C", "D"]
for i in letters:
    print(i)
	
	1
2
3
4
5
A
B
C
D
	
	total = 0
for i in range(10):
    print(i, end='  ')
    total += i
total
	
	0  1  2  3  4  5  6  7  8  9
	

Making a for loop with augmented assignments.

	total = 0
NumStudents = 0
grades = [98, 76, 71, 87, 83, 90, 57, 79, 82, 94]
for i in grades:
    total += i
    NumStudents += 1
average = total/NumStudents

print(f'Average is {average}')
	
	# Average is 81.7