FLOW OF CONTROL-NESTED IFS IN PYTHON

vikrammahendra3 727 views 34 slides Nov 09, 2020
Slide 1
Slide 1 of 34
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34

About This Presentation

HOW TO USE NESTED IFS IN PYTHON


Slide Content

Python Flow Control Class: XI Computer Science-083 Nested If..else conditions Part-3

So first we discuss Decision-making Statements i f Statement if - else Statement elif statements Nested if-else statements elif Ladder

i f Statement The if statement 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" n o=5 If no<10: p rint(“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

Let us understand how 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” a ge= int (input (“Enter the age:”)) if (age>=18): p rint(“Valid to vote”) p rint(“Valid to vote”) else: Enter the age: 20 Valid to vote If the condition is TRUE If the condition is False

Let us understand how 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): p rint(“It’s a Even number”) p rint(“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

Let us understand how 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): p rint(“It’s a positive value”) p rint(“It’s a negative value”) else: Enter the no: -9 It’s a negative value True False

Let us understand how 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 n o=3 True False

Use of And with 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): p rint(“Number in range”) p rint(“Not in range”) else: Enter the no: 55 N umber in range True False

Use of And with 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): p rint(“Number in range”) p rint(“Not in range”) else: Enter the no: 555 N umber in range True False

Use of And with 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>= 5 0 and sal >=40000): sal =sal+2000 print(“Salary with bonus=”, sal ) p rint(“No bonus”) else: Enter the age: 56 Enter the salary:50000 Salary with bonus=52000 sal = int (input (“Enter the salary:”)) True False

Use of And with 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

Let us understand how 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): p rint(“You selected RED”) 1- RED Colour 2-Yellow Colour 3-Green Colour Enter the choice: 2 You selected YELLOW print(“1- RED C olour ”) print(“2-Yellow Colour ”) print(“3-Green Colour ”) if (no==2): p rint(“You selected YELLOW”) if (no==3): p rint(“You selected GREEN”)

Use of And with 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

Nested i f .. 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

a= int (input (“Enter first side a:”)) 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”) True True True

Use of And with 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

Step by step descriptive 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.

Program to check whether 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

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

Use of And with 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.

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

Use of if…else Program to 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:”))

Program to accept two 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”) True False True True False False A B

Program to accept no 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”) True False True False

Nested i f .. 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- i f .. e lif ..else Statement False

Program to accept the 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!!”)

Program to colour numbers 1-RED , 2-BLUE 3- GREEN , 4-YELLOW and other number invalid number. print(“1- SELECT RED ”)) print(“2- SELECT B LUE”)) 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!!!!!!”)

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)

Program to accept a 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”)

Program to accept a 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”)

Program to ACCEPT PERCENTAGE 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+”)

Program to accept a 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 o r 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 ”)

Write a program to 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 h ra =0 d a=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 )

Write a program to 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