# Python If-else Statements with Examples

• By Deepali Shinkar
• April 1, 2024
• Python

# Python If-else Statements with Examples

Master Python If-else statements with examples. Learn how to control program flow, make decisions, and execute conditional logic effectively in Python. Join Python classes in Pune to explore more.

# 1. Write a Python program to find the maximum between two numbers.

Solution:

Suppose num1=4 num2=8

Maximum number is 8.

Algorithm:

Step 1– Take two numbers as an input.

Step 2– Use if condition to compare two numbers.

Step 3– If the condition is true return the number which is compared. Step 4- Else return the other number.

Step 5– Print max of two numbers.

Follow the above algorithm to write a code:

2. Write a Python program to find the maximum among three numbers. Solution :

Suppose num1=4 num2=8 num3=12

Maximum number is 12.

Algorithm:

Step 1– Take three numbers as an input.

Step 2– Use if condition,nested if else condition to compare one number with other two numbers.

Step 3– If the condition is true return the number which is compared.

Step 4-Elif condition is true return the number which is compared.

Step 5- Else return the third number.

Step 6– Print max of three numbers.

Follow the above algorithm to write a code:

3. Write a Python program to check whether a number is divisible by  5 and 11 or not.

Solution :

Suppose num=22

Output:Num 22 is divisible by11

Suppose num=25

Output:Num 25 is divisible by 5

Suppose num=55

Output: Num 55 is divisible by 5 and 11.

Suppose num=49

Output: Num 49 is not divisible by 5 and 11.

Algorithm:

Step 1– Take one number as an input.

Step 2– Use if the condition is true, the given number is divisible by 5 and 11 Step 3– Nested if the condition is true return the number is divisible by 5 Step 4- Nested if the condition is true return the number is divisible by 11 Step 5- Else return the number is not divisible by 5 and 11.

Step 6– Print the number divisible or not.

Follow the above algorithm to write a code:

4. Write a Python program to check whether a year is a leap year or not.

5. Write a Python program to check whether a character is alphabetical or not.

In programming language a string-type variable can store many different types of  characters-

• Alphabets (a, b, c… )
• Digits(1, 2, 3…)
• Special characters(@, %, &…)

These characters are differentiated on the basis of ASCII values :

• between 65 and 90 for upper case(A, B, C…)
• between 97 and 122 for lower case(a, b, c…)

Method 1:

Method 2:

6. Write a Python program to input any alphabet and check whether it  is vowel or consonant.

7. Write a Python program to check whether a character is uppercase or lowercase alphabet.

8. Write a Python program to input angles of a triangle and check whether a triangle is valid or not.

Solution:

When the three angles of the triangle are entered through the  keyboard. A triangle is valid if the sum of all three angles is equal to 180 degrees.

Registration Link: Online Python Training in Pune!

9. Write a Python program to check whether the triangle is equilateral,  isosceles, or scalene triangle.

10. Write a Python program to calculate profit or loss. Input is selling cost and actual cost.

11. Write a Python program to input marks of five subjects Physics, Chemistry,  Biology, Mathematics, and Computer. Calculate percentage and grade  according to following: Percentage >= 90% : Grade A Percentage >= 80% :  Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade D Percentage

12. Write a Python program to input the basiPython salary of an employee and calculate its Gross salary according to the following: Basic Salary <= 10000: HRA  = 20%, DA = 80% Basic Salary <= 20000: HRA = 30%, DA = 90% Basic Salary >  20000: HRA = 35%, DA = 95%.

13. Write a Python program to input electricity unit charges and calculate total electricity bill according to the given condition: For the first 50 units, Rs. 0.50/unit  For next 100 units Rs. 0.75/unit For the next 100 units Rs. 1.25/unit For unit above  250 Rs. 1.50/unit An additional surcharge of 17% is added to the bill.