Ut 1
Ut 1
Conditional and loops : if statement, else statement, Elif statement , while statement , for
statement, break statement, continue statement , pass statement , else statement
If else Statement
The if-else statement in Python is used to execute a block of code when the condition in
the if statement is true, and another block of code when the condition is false.
age=25
print ("age: ", age)
if age >=18:
print ("eligible to vote")
else:
print ("not eligible to vote")
The if elif else statement allows you to check multiple expressions for TRUE and execute a
block of code as soon as one of the conditions evaluates to TRUE
number = 10
if number > 10:
print("The number is greater than 10.")
elif number == 10:
print("The number is exactly 10.")
else:
print("The number is less than 10.")
While Loop
count = 1
For loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a
set, or a string).
This is less like the for keyword in other programming languages, and works more like an
iterator method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set
etc.
With the break statement we can stop the loop before it has looped through all the items
i=1
while (i < 5):
if (i == 2):
pass # Skip when i is 2
else:
print(i)
i += 1