1) The document discusses various Python flow control statements including if, if-else, nested if-else, and elif statements with examples of using these to check conditions and execute code blocks accordingly.
2) Examples include programs to check number comparisons, even/odd numbers, positive/negative numbers, and using nested if-else for multi-level checks like checking triangle validity.
3) The last few sections discuss using if-else statements to classify triangles as equilateral, isosceles, or scalene and to check if a number is positive, negative, or zero.
So first wediscuss Decision-making Statements
if Statement
if - else Statement
elif statements
Nested if-else statements
elif Ladder
3.
if Statement
The ifstatement is used to test a specific condition. If the condition is true only, a block
of code or block of statements will be executed.
Syntax:
if condition or expression:
-----Statements-------
-----Statements-------
Example: Program to check number is less than 10 , if yes
then display message "Number is smaller than 10"
no=5
If no<10:
print(“Number is smaller than 10”)
---Output----
Number is smaller than 10
This is indentation. Python relies on indentation (whitespace
at the beginning of a line) to define scope in the code
4.
Let us understandhow the if..else work
If the question is to check the user input age is more than equal to 18 or not, if yes
than display “Valid to Vote” , otherwise display “Not valid to vote”
age=int(input(“Enter the age:”))
if (age>=18):
print(“Valid to vote”)
print(“Valid to vote”)
else:
Enter the age: 20
Valid to vote
If the condition is TRUE
If the condition is False
5.
Let us understandhow the if..else work
Program to accept number from user and check that number is even or
odd.
no=int(input(“Enter the no:”))
if (no%2==0):
print(“It’s a Even number”)
print(“It’s a Odd number”)
else:
Enter the no: 5
It’s a Odd number
Enter the no: 2
It’s a Even number
True
False
6.
Let us understandhow the if..else work
Program to accept number from user and check that number is positive or
negative.
no=int(input(“Enter the no:”))
if (no>0):
print(“It’s a positive value”)
print(“It’s a negative value”)
else:
Enter the no: -9
It’s a negative value
True
False
7.
Let us understandhow the if..else work with OR
Program to accept number from user and check that number is divisible by
3 or 5 then add +2 to a number otherwise -2.
no=int(input(“Enter the no:”))
if (no%3==0 or no%5==0):
no=no+2
print(“no=”,no)
no=no-2
print(“no=”,no)
else:
Enter the no: 5
no=3
True
False
8.
Use of Andwith if..else
Program to accept number between 40 to 80, and if number is between the
range the print “Number in range”, otherwise “not in range”
no=int(input(“Enter the no:”))
if (no>=40 and no<=99):
print(“Number in range”)
print(“Not in range”)
else:
Enter the no: 55
Number in range
True
False
9.
Use of Andwith if..else
Program to accept number 3 to 4 digits only , and if number is between that
range than print “Number in range”, otherwise “not in range”
Example:
If number is 152 , then its in a range, if number is 1545 it in a range.[Note :
number should be from 100 to 9999]
no=int(input(“Enter the no:”))
if (no>=100 and no<=9999):
print(“Number in range”)
print(“Not in range”)
else:
Enter the no: 555
Number in rangeTrue
False
10.
Use of Andwith if..else
Program to accept age and salary of an employee , if age is more than 50
and salary is more than 40000 than add bonus of 2000 to its salary
otherwise display message “No bonus”
age=int(input(“Enter the age:”))
if (age>=50 and sal>=40000):
sal=sal+2000
print(“Salary with bonus=”,sal)
print(“No bonus”)
else:
Enter the age: 56
Enter the salary:50000
Salary with bonus=52000
sal=int(input(“Enter the salary:”))
True
False
11.
Use of Andwith if..else
Program to no 1 , 2 ,3 and print message “you choice is different”, next message
“you selected 1 and 2”, another message you selected 1,2,3”
n1=int(input(“Enter the no 1:”))
n2=int(input(“Enter the no 2:”))
n3=int(input(“Enter the no 3:”))
if (n1==1 and n2==2 and n3==3):
print(“you selected 1,2,3”)
else
if(n1==1 and n2==2):
print(“you selected 1 and 2”)
else
print(“your choice is different”)
Enter the no1: 1
Enter the no 2: 2
Enter the no3: 4
You selected 1and 2
True
False
True
False
12.
Let us understandhow use multiple if’s work
Program to accept number and print “red” for 1 , “yellow” for 2 , “Green” for 3
…….
no=int(input(“Enter the choice”))
if (no==1):
print(“You selected RED”)
1- RED Colour
2-Yellow Colour
3-Green Colour
Enter the choice: 2
You selected YELLOW
print(“1- RED Colour”)
print(“2-Yellow Colour”)
print(“3-Green Colour”)
if (no==2):
print(“You selected YELLOW”)
if (no==3):
print(“You selected GREEN”)
13.
Use of Andwith nested if’s
Program to check whether triangle is valid or not if sides are given.
Explanation:
A triangle is valid if sum of its two sides is greater than the third side. Means if
a, b, c are three sides of a triangle. Then the triangle is valid if all three
conditions are satisfied
a + b > c
a + c > b and
b + c > a
Property of triangle
14.
Nested if ..else Statement You can have if statements inside if statements, this is
called nested if statements. It allow us to check for
multiple test expressions and execute different codes
for more than two conditions.
Syntax:
if condition or expression:
if condition:
-Statements-
-Statements-
else:
-Statements-
-Statements-
else:
if condition:
-Statements-
-Statements-
else:
-Statements-
-Statements-
True
False
True
True
False
False
Outer if
Outer else
if condition or expression:
if condition:
-Statements-
else:
if condition:
-Statements-
True
False
Inner True
Inner True
15.
a=int(input(“Enter first sidea:”))
b=int(input(“Enter second side b:”))
c=int(input(“Enter third side c:”))
a=int(input(“Enter first side a:”))
b=int(input(“Enter second side b:”))
c=int(input(“Enter third side c:”))
if ((a+b)>c and (a+c)>b and (b+c)>a):
print(“triangle is valid ”)
else
print(“Invalid triangle”)
if ((a+b)>c):
if((a+c)>b):
if((b+c)>a):
print(“triangle is valid ”)
else
print(“Invalid triangle”)
else
print(“Invalid triangle”)
else
print(“Invalid triangle”)
T
r
u
e
T
r
u
e
T
r
u
e
16.
Use of Andwith nested if’s
Program to check whether triangle is valid or not if angles are given.
Write a python program to check whether a triangle is valid or not if angles are
given using if else. How to check whether a triangle can be formed or not, if its
angles are given using if else in python programming. Logic to check triangle
validity if angles are given in python program.
Example
Input
Input first angle: 60
Input second angle: 30
Input third angle: 90
Output
The triangle is valid
Property of a triangle
A triangle is said to be a valid triangle if and only if sum of its angles is 180
17.
Step by stepdescriptive logic to check whether a triangle can be formed
or not, if angles are given.
• Input all three angles of triangle in some variable say angle1, angle2
and angle3.
• Find sum of all three angles, store sum in some variable say :
• sum = angle1 + angle2 + angle3.
• Check if(sum == 180) then, triangle can be formed otherwise not.
18.
Program to checkwhether triangle is equilateral, scalene or isosceles. Write a python
program to input sides of a triangle and check whether a triangle is equilateral, scalene or
isosceles triangle using if else. How to check whether a triangle is equilateral, scalene or
isosceles triangle in python programming. Logic to classify triangles as equilateral, scalene
or isosceles triangle if sides are given in python program.
Example
Input
Input first side: 30
Input second side: 30
Input third side: 30
Output
Triangle is equilateral triangle
19.
Properties of triangle
•A triangle is said Equilateral Triangle, if all its sides are equal. If a, b, c are
three sides of triangle. Then, the triangle is equilateral only if a == b == c.
• A triangle is said Isosceles Triangle, if its two sides are equal. If a, b, c are
three sides of triangle. Then, the triangle is isosceles if either a == b or a == c
or b == c.
• A triangle is said Scalene Triangle, if none of its sides are equal.
Step by step descriptive logic to classify triangle as equilateral, scalene or isosceles
triangle.
Input sides of a triangle from user. Store it in some variables say side1, side2 and side3.
Check if(side1 == side2 && side2 == side3), then the triangle is equilateral.
If it is not an equilateral triangle then it may be isosceles.
Check if(side1 == side2 || side1 == side3 || side2 == side3), then triangle is isosceles.
If it is neither equilateral nor isosceles then it scalene triangle
20.
Use of Andwith nested if’s
program to check positive, negative or zero using simple if or if else. Write a
python program to input any number from user and check whether the given
number is positive, negative or zero. Logic to check negative, positive or zero
Steps to check negative, positive or zero.
• Input a number from user in some variable say num.
• Check if(num < 0), then number is negative.
• Check if(num > 0), then number is positive.
• Check if(num == 0), then number is zero.
21.
no=int(input(“Enter no”))
if (no>0):
print(“positive”)
if((no)>0):
print(“positive”)
else:
if(no<0):
print(“Negative ”)
else
print(“equal to zero”)
if (no<0):
print(“Negative”)
if (no==0):
print(“zero”)
no=int(input(“Enter no”))
True
False
True
False
22.
Use of if…else
Programto accept two numbers in a variable A, B and print A is greater
than B or print B is greater than A.
if (A>B):
print(“A is greater than B”)
else:
print(“B is greater than A”)
A=int(input(“Enter value A:”))
B=int(input(“Enter value B:”))
23.
Program to accepttwo numbers in a variable A, B and C.Find the largest number
from these three variables.
if (A>B):
else:
A=int(input(“Enter value A:”))
B=int(input(“Enter value B:”))
if (A>C):
print(“A is greater”)
else:
print(“B is greater”)
if (B>C):
print(“A is greater”)
else:
print(“B is greater”)
T
r
u
e
F
a
l
s
e
True
True
False
False
A
B
24.
Program to acceptno of days in a leap year , if its is correct display “you passed
first level” otherwise display “ You faild in first level” if the days are correct than
ask for number of days in a February , if user enter correct days then display “You
cleared the test”, “You cleared first test but failed in second level”,
if (level1==365):
else:
level1=int(input(“Enter no of days in a leap year:”))
level2=int(input(“Enter number of days in February:”))
print(“you clear the level1”))
print(“You failed in first level”))
if(level2==29):
print(“You cleared the test Well Done”)
else:
print(“You cleared first test but failed in second test”)
T
r
u
e
False
True
False
25.
Nested if ..else Statement
Syntax:
if condition or expression:
if condition:
-Statements-
-Statements-
else:
-Statements-
-Statements-
else:
if condition:
-Statements-
-Statements-
else:
-Statements-
-Statements-
True
False
True
True
False
False
Outer if
Outer else
Syntax:
if condition or expression:
-Statements-
elif condition:
-Statements-
elif condition:
-Statements-
elif condition:
-Statements-
elif condition:
-Statements-
else:
-Statements-
if .. elif..else Statement
False
26.
Program to acceptthe no 1 , 2, 3 from the user and print for 1 “you selected 1”,
for 2 “you selected 2”, for 3 “you selected 3” otherwise print” Invalid value!!”
n=int(input(“Enter the no”))
if (n==1):
print(“You selected 1”)
elif (n==2):
print(“You selected 2”)
elif (n==3):
print(“You selected 3”)
else:
print(“Invalid Value!!”)
27.
Program to colournumbers 1-RED , 2-BLUE 3- GREEN , 4-YELLOW and other
number invalid number.
print(“1- SELECT RED”))
print(“2- SELECT BLUE”))
print(“3- SELECT GREEN”))
print(“4- SELECT YELLOW”))
no=int(input(“Enter the choice”))
if(no==1):
print(“YOU SELECTED RED”)
elif(no==2):
print(“YOU SELECTED BLUE”)
elif(no==3):
print(“YOU SELECTED GREEN”)
elif(no==4):
print(“YOU SELECTED YELLOW”)
else:
print(“Invalid!!!!!!”)
28.
Create a Calculator,ask user to enter two number and one operator
(+,-,*,/,//) and display output.
val1=int(input(“Enter value1:”))
val2=int(input(“Enter value2:”))
op=int(input(“Enter operator(+,*,-,/,//):”))
if(op==‘+’):
print(val1+val2)
elif(op==‘-’):
print(val1-val2)
elif(op==‘+’):
print(val1*val2)
elif(op==‘/’):
print(val1/val2)
elif(op==‘//’):
print(val1//val2)
29.
Program to accepta number from 1 to 7
and print day .Example: 1 “MONDAY”,2
“TUESDAY”,3 “WEDNESDAY”, …………7 “
SUNDAY” otherwise “INVALID VALUE!!!”
no=int(input(“Enter no 1-7”))
if(no==1):
print(“MONDAY”)
elif(no==2):
print(“TUESDAY”)
elif(no==3):
print(“WEDNESDAY”)
elif(no==4):
print(“THURSDAY”)
elif(no==5):
print(“FRIDAY”)
elif(no==6):
print(“SATURDAY”)
elif(no==7):
print(“SUNDAY”)
else:
print(“INVALID NO”)
30.
Program to accepta number from 1 to 12 and print Months
.Example: 1 “JAN”,2 “FEB”,3 “MAR”, …………12 “DEC” otherwise
“INVALID VALUE!!!”
no=int(input(“Enter no 1-12”))
if(no==1):
print(“JAN”)
elif(no==2):
print(“FEB”)
elif(no==3):
print(“MAR”)
elif(no==4):
print(“APR”)
elif(no==5):
print(“MAY”)
elif(no==6):
print(“JUN”)
elif(no==7):
print(“JUL”)
elif(no==8):
print(“AUG”)
elif(no==9):
print(“SEP”)
elif(no==10):
print(“OCT”)
elif(no==11):
print(“NOV”)
elif(no==12):
print(“DEC”)
else:
print(“INVALID VALUE”)
31.
Program to ACCEPTPERCENTAGE print
grade :
Per Grade
<33 FAIL
33- 44 C
45-59 B
60-75 A
75 above A+
per=int(input(“Enter percentage”))
if(per<33):
print(“FAIL”)
elif(per>=33 and per<=44):
print(“C”)
elif(per>=45 and per<=59):
print(“B”)
elif(per>=60 and per<=75):
print(“A”)
elif(per>75):
print(“A+”)
32.
Program to accepta no and print as shown below: if user
input: 1 , 21 , 31 , output : 1st , 21st , 31st
If user input: 2, 22 Output: 2nd , 22nd
If user input: 3 , 23 Output: 3rd and 23rd
Other left numbers : Output: 4,5,…….. With th
no=int(input(“Enter any no from 1 to 31”))
if(no==1 or no==21 or no==31):
print(no+“st”)
elif(no==2 or no==22):
print(no+“nd”)
elif(no==3 or no==23):
print(no+“rd”)
else:
print(no+“th”)
33.
Write a programto input basic salary of an
employee and calculate its Gross salary
according to following:
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%
sal=int(input(“Enter Basic salary:”))
if(sal<=10000):
hra=sal*20/100
da=sal*80/100
netsal=sal+hra+da
print(“Netsal=”,netsal)
netsal=0
hra=0
da=0
elif(sal>20000):
hra=sal*30/100
da=sal*95/100
netsal=sal+hra+da
print(“Netsal=”,netsal)
elif(sal<=20000):
hra=sal*20/100
da=sal*80/100
netsal=sal+hra+da
print(“Netsal=”,netsal)
34.
Write a programto input electricity unit charges and calculate total
electricity bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill