if else-if in python [With Examples | Simulations]

Sword Code
0

The one commonly used function in programming is the if else-if statement. There are many types of functions, each with its importance and characteristics.

Note: To see all the Python-related posts, go to the main page on your right side where you can see Tag Names Python Programming click on it and see all the posts related to Python programming that we have posted so far.

If else-if is a conditional statement used when among many choices one is to be selected while having an else as default if any of the given conditions are not met.

The structure of the if-else statement is given below.

Structure:

if condition:
{
     Block of statements
}

elif condition:
{
    Block of statements
}

elif condition:
{
      Block of Statements
}

else:
{
      Block of statements
}
There is no limit on how many conditions you can apply, the more the condition the more the number of else if the condition increases. 
In condition, you have to write the condition/conditions on which the Block of statements are meant to be executed in which condition. A practical example of an ATM has multiple options, each with a different process. Like the change of PIN, account balance, funds transfer, and withdrawal.

Examples:
Example 01:

This code checks the age and categorizes it into "Child", "Teenager", or "Adult". If "-ve" print a random message.
age = int(input("Enter age: "))

if age >0 and age <13:
    print("Child")
elif age >13 and age >= 18:
    print("Teenager")
elif age >18:
    print("Adult")
else:
    print("Your are born born already\n-ve age don't make any sense")

Example 02:

This code assigns a grade based on the score. When the total Score is 500, also tell the Score.
num = 500
print("Total score is 500" )
num1 = int(input("Enter the Rewarded score: "))
score = (num/num1)*100
if score >= 90:
    print(score," %\nGrade: A")
elif score >= 80:
    print(score," %\nGrade: B")
elif score >= 70:
    print(score," %\nGrade: C")
elif score >= 60:
    print(score," %\nGrade: D")
else:
    print(score," %\nGrade: F")

Example 03:

This code describes the weather based on the temperature.
temp = 5

#temperature is referred as temp
if temp > 30:
    print(temp,"F\nHot")
elif temp > 20 and temp < 30:
    print(temp,"F\nWarm")
elif temp > 5 and temp<20:
    print(temp,"F\nCool")
else:
    print(temp,"F\nFreezing cold")

Example 04:

This code checks if a number is even or odd.
number = 7

if number % 2 == 0:
    print("Even")
else:
    print("Odd")

Example 05:

This code checks if a year is a leap year.
year = 2024

if year % 4 == 0 :
    print("Leap Year")
else:
    print("Not a Leap Year")

Example 06:

This code categorizes BMI into different weight categories.
bmi = 22

if bmi < 18.5:
    print("Underweight")
elif bmi < 25:
    print("Normal weight")
elif bmi < 30:
    print("Overweight")
else:
    print("Obesity")

Example 07:

This code prints the day of the week based on a number.
day = 3

if day == 1:
    print("Monday")
elif day == 2:
    print("Tuesday")
elif day == 3:
    print("Wednesday")
elif day == 4:
    print("Thursday")
elif day == 5:
    print("Friday")
elif day == 6:
    print("Saturday")
else:
    print("Sunday")

Example 08:

This code finds the largest of three numbers.
a = 5
b = 10
c = 15
if a > b and a > c:
    print(a,",a is the largest")
elif b > a and b>c:
    print(b,",b is the largest")
else:
    print(c,",c is the largest")


Example 09:

This code checks login credentials and prints a welcome message or an error.
username = "user"
password = "pass"

if (username == "admin" and password == "admin123"):
    print("Welcome, admin!")
elif (username == "user" and password == "pass"):
    print("Welcome, user!")
else:
    print("Invalid credentials")


Example 10:

This code simulates a traffic light system, providing instructions based on the signal color.
signal = "green"

if signal == "red":
    print("Stop")
elif signal == "yellow":
    print("Slow down")
elif signal == "green":
    print("Go")
else:
    print("Invalid signal")

This is the simple pattern of the if else-if statement and some examples. It works simply as explained in the image.
to check the example click on the Button to see the execution with examples

Interactive Button



Post a Comment

0Comments

If you have any type of issue or request leave an comment so that we can handle it

Post a Comment (0)