







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








It is used to execute one or more statement depending on whether a condition is TRUE or NOT. The IF statement execute a block of code only if the condition is TRUE. SYNTAX:
statem ent/ Block of code Conditio n True? Execute code False Indentation is mandatory
x = 10 if x >5: pri nt( “ x is True Output: x is greater than 5 Output Ngawang
The IF ELIE ELSE statement allows us to check multiple condition. Only one block of code is executed. SYNTAX: **if condition1:
elif condition2: #code block 2 else: #code block 3 True False** Conditio n 1 True? Execute code 1 Conditio n 2 True? False Execute code 3 Output True Execute code 2
x = 7 if x % 2 = = 0: print(“Even Number”) else: print(“Odd number”) OR n1 = int(input(“ Enter the Number”)) if n1 % 2 = = 0: print(“Even Number”) else: print(“Odd number”) Marks = 70 if Marks > = 90 : print(“ Grade A”) elif Marks > = 90 : print(“ Grade B”) elif Marks > = 90 : print(“ Grade C”) else: print(“ Grade D”)
SYNTAX for^ **loop while condition:
Example: x = 1 while x < = 10 : print(x) x+= print(“End”) It is used to iterate over the element of a sequence. It means for loop can be used to execute a group of statement repeatedly depending upon the number of element in the sequence. It can work with sequences like List, Tuple, array etc Best when you know how many time to repeat.
Nested Loops It is possible to write one loop inside another loop. **for variable in sequence:
Example: for i in range(3): for j in range(4): print(“ i ” = i, \t, ” j ” = j ) for i in range(1,6): print( i ) Output: 1,2,3,4, Program: A python program that display star in right angled triangle form using nested loop.
for i in range(1,5): for j in range(1, i + 1): print(“ * “, end = “ ” ) print()
The statement changes the normal flow of loops.
(^) Control move to the next statement after the loop.
if i = = 3 : brea k print( i
Ngawang
It is used in a loop to go back to the beginning of the loop. It means when the continue is executed, the next repetition will start and the subsequent statement in the loop are not executed.
for i in range(1,6): if i = = 3 : continu e print( i )