Control Statements
#Python Code
n=12
if(n>0):
print(n,"is natural number")
print("The End")
Practice Questions:
1. Write python program to check whether a desired value is multiple of 5 and display “Multiple of 5”, if yes.
Hint: n%5==0
2. Write python code to accept age from the user and display “You are eligible for voting” when his age is greater than 18.
Hint: age>18
3. Write python program to accept two numbers from the user and display which is big
Hint:
a>b
b>a
4. Write python program to accept a number and check and display whether it is even or odd
Hint:
x%2==0
x%2!=0
5. Write Python code to accept a number from the user and display whether it is positive, negative or equal to zero.
Hint:
n>0
n<0
n==0
6. Write Python program to accept any number from 2,4,6,8 from the user and display their Words Format
Example Output:
Enter any number from 2,4,6,8: 4
FOUR
#Example of Multiple if
num=int(input("Enter any number from 2,4,6 or 8:"))
if(num==2):
print("TWO")
if(num==4):
print("FOUR")
if(num==6):
print("SIX")
if(num==8):
print("EIGHT")
if(num!=2 and num!=4 and num!=6 and num!=8):
print("Invalid Input")
print("Rest of the program")
#Example of if...else
first=int(input("Enter your first number:"))
second=int(input("Enter your second number:"))
if(first > second):
print(first,"is big")
else:
print(second,"is big")
print("Rest of the program")
Practice Questions:
1. Write a program to accept a number from the user and determine whether it is even or odd.
Hint: x%2==0 then it is even
2. Write a program to accept two numbers and one arithmetic operator from the user and return result of the operation based on the operator entered
Example output:
Enter first number: 4
Enter second number: 5
Enter operator: +
Sum=9
3. Write python program to accept any letter from English Vowels and display corresponding message.
Sample output1:
Enter any English Vowel: a
Your Vowel is : a
Sample output2:
Enter any English Vowel: x
x is not Vowel
Nested if
One if statement under the control of another if. This is used when we want to specify some code to be executed when sequential true returning of two conditions.
>>>Execution flow diagram
#Example of nested if
nat="India"
age=21
if nat=="India":
if age>18:
print("You are eligible for voting")
print("Rest of the program")
Outer if can also have other program statements along with inner if.
#Example of nested if
a=int(input("Enter any number:"))
if(a>0):
print(a,"is natural number")
if(a%2==0):
print(a,"is even number too")
print("other statements under outer if")
print("Rest of the program")
Outer if can have else part
Inner if can have else part
#Example of nested if
a=int(input("Enter any number:"))
if(a>0):
print(a,"is natural number")
if(a%2==0):
print(a,"is even number too")
else:
print(a,"is odd number too")
print("other statements under outer if")
else:
print(a,"is not natural number")
print("Rest of the program")
else…if
inner if appear inside the false part (else block) of outer if.
>>>Execution Flow Diagram
#Example
a=int(input("Enter any number:"))
if(a>0):
print("Positive")
else:
if(a<0):
print("Negative")
else:
print("Equal to zero")
#Example of else.....if
a=int(input("Enter any number from 2,4,6 or 8 only:"))
if(a==2):
print("TWO")
else:
if(a==4):
print("FOUR")
else:
if(a==6):
print("SIX")
else:
if(a==8):
print("EIGHT")
else:
print("Invalid input")
print("The End")
The elif
It is used when we want check further condition when a specific condition is returned false.
#Example of elif
a=int(input("Enter any number from 2,4,6 or 8 only:"))
if(a==2):
print("TWO")
elif(a==4):
print("FOUR")
elif(a==6):
print("SIX")
elif(a==8):
print("EIGHT")
else:
print("Invalid input")
print("The End")
Practice:
1. Write python program to accept any three numbers from the user and display which is big.
Logic1: nested if and else…if combination
Logic2: ‘and’ operator with elif
2. Write a program to accept marks of a student in an examination and display his Grade based on the following criteria:
Marks Grade
>=0 and <35 Fail
>=35 and <55 C
>=55 and <75 B
>=75 and <95 A
>=95 and <=100 A+