if, elif, else Conditions
Python uses the if keyword to implement decision control.
if [boolean expression]:
statement1
statement2
...
statement
• Any Boolean expression evaluating to True or False appears after the if keyword.
• Use the : symbol and press Enter after the expression to start a block with an
increased indent.
• One or more statements written with the same level of indent will be executed
if the Boolean expression evaluates to True.
• To end the block, decrease the indentation.
marks = 100
if marks < 40:
print(“Fail marks is less than 40") #Single Statement
Multiple statement in if block
length = 50
width = 5
if length*width < 400:
print(“length*width is less than 400")
print(“length = ", length)
print(“width = ", width)
length = 50
width = 5
if length*width < 400:
print(“length*width is less than 400")
print(“length = ", length)
print(“width = ", width)
print(" (“width = ", width)
^
IdentationError: unexpected indent
The statements with the same indentation level as if condition will not consider
in the if block. They will consider out of the if condition.
length = 50
width = 5
if length*width < 100:
print(“length*width is less than 400")
print(“length = ", length)
print(“width = ", width)
print("No if block executed.")
Multiple if Conditions
length = 100
if length > 100:
print("length is greater than 100")
if length == 100:
print("length is 100")
if length < 100:
print("length is less than 100")
else Condition
Along with the if statement, the else condition can be optionally used to define an
alternate block of statements to be executed if the boolean expression in the if
condition evaluates to False.
if [boolean expression]:
statement1
statement2
...
statementn
else:
statement1
statement2
...
statementn
length = 50
if length >= 100:
print("length is greater than 100")
else:
print("length is less than 100")
the if condition length >= 100 is False, so the else block will be executed. The
else block can also contain multiple statements with the same indentation;
otherwise, it will raise the IndentationError.
elif Condition
Use the elif condition is used to include multiple conditional
expressions after the if condition or between the if and else
conditions.
if [boolean expression]:
[statements]
elif [boolean expresion]:
[statements]
elif [boolean expresion]:
[statements]
else:
[statements]
The elif block is executed if the specified condition evaluates to True.
length = 100
if length > 100:
print("length is greater than 100")
elif length == 100:
print("length is 100")
elif length < 100:
print("length is less than 100")
if-elif-else Conditions
length = 50
if length > 100:
print("length is greater than 100")
elif length == 100:
print("length is 100")
else:
print("length is less than 100")
length = 50
if length > 100:
print("length is greater than 100")
elif length == 100:
print("length is 100")
else length < 100:
print("length is less than 100")
elif length == 100:
^
IdentationError: unindent does not match any outer indentation level
Nested if-elif-else condition
length = 50
width = 5
area = length*width
if area > 100:
if area > 500:
print("area is greater than 500")
else:
if area < 500 and area > 400:
print("area is")
elif area < 500 and area > 300:
print("area is between 300 and 500")
else:
print("area is between 200 and 500")
elif area == 100:
print("area is 100")
else:
print("area is less than 100")
Python Scripts
1. Write a program to check whether a person is eligible for voting or not. Input will be taken
by user.
2. Write a program to check whether a number is even or odd. Input will be taken by the
user.
3. Write a program to check whether an year is leap year or not input will be taken by user
4. Write a program to accept the cost price of a mobile and display GST to be paid according
to the following criteria
Cost Price in Rs. GST
>100000 18%
>50000 and <=100000 15%
<50000 10%
5. Write a Program to check whether the last digit of a number is divisible by 7 or not input will
be taken by user
Python - While Loop
Python while loop is used to run a block code until a certain condition is met.
while [boolean expression/condition]:
statement1
statement2
...
statementN
Here,
A while loop evaluates the condition
If the condition evaluates to True, the code inside the while loop is executed.
condition is evaluated again.
This process continues until the condition is False.
When condition evaluates to False, the loop stops.
num =0
while num < 5:
num = num + 1
print('num = ', num)
runfile('C:/Users/Aditya/Desktop/untitled13.py', wdir='C:/Users/Aditya/Desktop')
num = 1
num = 2
num = 3
num = 4
num = 5
Note: All the statements in the body of the loop must start with the same
indentation, otherwise it will raise a IndentationError.
Exit from the While Loop
Use the break keyword to exit the while loop at some condition. Use the if condition to
determine when to exit from the while loop,
num = 0
while num < 5:
num += 1
print('num = ', num)
if num == 3: # condition before exiting a loop
break
runfile('C:/Users/Aditya/Desktop/untitled13.py', wdir='C:/Users/Aditya/Desktop')
num = 1
num = 2
num = 3
Continue Next Iteration
Use the continue keyword to start the next iteration and skip the statements after the
continue statement on some conditions,
num = 0
while num < 5:
num += 1
if num == 3: # condition before exiting a loop
continue
print('num = ', num)
runfile('C:/Users/Aditya/Desktop/untitled13.py', wdir='C:/Users/Aditya/Desktop')
num = 1
num = 2
num = 4
num = 5
While Loop with else Block
The else block can follow the while loop. The else block will be executed when
the boolean expression of the while loop evaluates to False.
num = 0
while num == 3:
num += 1
print('num = ', num)
else:
print('else block executed’)
runfile('C:/Users/Aditya/Desktop/untitled13.py', wdir='C:/Users/Aditya/Desktop')
else block executed
Questions
• WAP to print first 10 integers and their squares
• WAP to print the table of the number entered from the user
• WAP to print even numbers falls between two numbers these two
numbers will be taken from user
• WAP to check whether the number is prime or not
• WAP to reverse the number inputted user
• WAP to find Armstrong, Fibonacci, factorial, palindrome of inputted
number.
Python for loop
• In Python, the for loop is used for iterating over sequence types such
as list, tuple, set, range, etc.
for val in sequence:
statement1
statement2
...
statementN
• To start with, a variable val in the for statement refers to the item at the 0 index in
the sequence. The block of statements with increased uniform indent after the :
symbol will be executed. A variable val now refers to the next item and repeats the
body of the loop till the sequence is exhausted.
nums = [10, 20, 30, 40, 50]
for a in nums:
print(a)
nums = (10, 20, 30, 40, 50)
for a in nums:
print(a)
for ch in 'Hello':
print (ch)
numNames = { 1:'One', 2: 'Two', 3: 'Three'}
for pair in numNames.items():
print(pair)
• The key-value paris can be unpacked into two variables in the for loop to get
the key and value separately.
numNames = { 1:'One', 2: 'Two', 3: 'Three'}
for k,v in numNames.items():
print("key = ", k , ", value =", v)
• It is also used to iterate over heterogeneous values
For loop and range function
The range class is an immutable sequence type. The range() returns the range object that can
be used with the for loop.
for i in range(5):
print(i)
Exit the For Loop
The execution of the for loop can be stop and exit using the break keyword on some condition
for i in range(1, 5):
if i > 2:
break
print(i)
Continue Next Iteration
Use the continue keyword to skip the current execution and continue on the next
iteration using the continue keyword on some condition
for i in range(1, 5):
if i == 3:
continue
print(i)
For Loop with Else Block
The else block can follow the for loop, which will be executed when the for loop ends.
for i in range(2):
print(i)
else:
print('End of for loop')
Nested for loop:
for x in range(1,4):
for y in range(1,3):
print('x = ', x, ', y = ', y)
for x in range(2,10,2):
print(x)
for x in range(5,0,-1):
print(x)