The document discusses if-else statements in Python. It provides the syntax for if-else statements and nested if-else statements (if-elif-else). Examples are given to check if a number is positive/negative, even/odd, and to sort 3 numbers in ascending order. Finally, it provides practice problems involving if-else statements to check voting eligibility and calculate grades based on subject marks and percentages.
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
44 views
If Else
The document discusses if-else statements in Python. It provides the syntax for if-else statements and nested if-else statements (if-elif-else). Examples are given to check if a number is positive/negative, even/odd, and to sort 3 numbers in ascending order. Finally, it provides practice problems involving if-else statements to check voting eligibility and calculate grades based on subject marks and percentages.
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10
IF….
ELSE STATEMENT IF….ELSE STAEMENT
If-else statement executes some code
if the test expression is true (nonzero) and some other code if the test expression is false. Syntax: if(condition): statements else: statements e.g. a=10 if(a < 100): print(‘less than 100’) else: print(‘more than equal 100') OUTPUT less than 100 Nested if-else statement or if…elif…else statement
The nested if...else statement allows
you to check for multiple test expressions and execute different codes for more than two conditions. Example num= float(input("Enter a number: ")) if num>= 0: if num== 0: print("Zero") else: print("Positive number") else: print("Negative number") OUTPUT Enter a number: 5 Positive number #sort 3 numbers first = int(input("Enter the first number: ")) second = int(input("Enter the second number: ")) third = int(input("Enter the third number: ")) small = 0 middle = 0 large = 0 if first < third and first < second: small = first if second < third and second < first: small = second else: small = third eliffirst < second and first < third: middle = first if second > first and second < third: middle = second else: middle = third eliffirst > second and first > third: large = first if second > first and second > third: large = second else: large = third print("The numbers in accending order are: ", small, middle, large) *Write a program in python to check that entered number is even or odd
* Write a program in python to check that
entered number is positive or negative
* Write a program to enter your age and
to find out whether you can vote or not *Write a program in python to enter the marks of five subjects and to find the total and percentage and to find grade according to following conditions NOTE Please Type all the examples and do the practice in Computer. Write all the program in your notebook.