“elif” Keyword in Python: Using Else If in Your Code

"elif" Keyword in Python: Using Else If in Your Code

In Python, you can use the “elif” keyword to evaluate multiple conditional statements, rather than just two with the “if” and “else” keywords. This allows you to write more complex programs with more advanced decision-making capabilities.

This tutorial will walk you through how to use the “elif” keyword in Python to create programs that can handle multiple conditional statements. By the end, you should have a good idea of how to use “elif” and be able to implement it in your own code.

Using “Else If” in Your Code

In many programming languages, including Python, you can use the “if” keyword to test some condition and execute some code if that condition is true. Here’s an example:

mood = "happy"

if mood == "happy":
    print("Hooray!")

In this example, the program checks if the variable “mood” is equal to the string “happy”, and if it is, it prints “Hooray!”. However, in real-world programming, you often need to test multiple conditions, each with their own set of code to execute if they are true. This is where “elif” comes in.

The “elif” keyword stands for “else if”. It allows you to evaluate additional conditional statements in a single block of code. Here’s an example:

mood = "sleepy"

if mood == "happy":
    print("Hooray!")
elif mood == "sleepy":
    print("Yawn.")

In this example, there are two conditional statements in the same block of code. First, the program checks if the variable “mood” is equal to the string “happy”. If it is, it prints “Hooray!”. If it isn’t, it moves on to the next line, which uses “elif” to check if “mood” is equal to the string “sleepy”. If it is, it prints “Yawn.”.

You can use as many “elif” statements as you need to test as many conditional statements as you want. Here’s an example with three “elif” statements:

mood = "angry"

if mood == "happy":
    print("Hooray!")
elif mood == "sleepy":
    print("Yawn.")
elif mood == "angry":
    print("Take a deep breath.")
elif mood == "sad":
    print("Cheer up!")
else:
    print("I don't recognize that mood.")

In this example, there are four conditional statements. If “mood” is equal to “happy”, it prints “Hooray!”. If “mood” is equal to “sleepy”, it prints “Yawn.”. If “mood” is equal to “angry”, it prints “Take a deep breath.”. Finally, if “mood” is equal to “sad”, it prints “Cheer up!”. If none of those conditions are met, it prints “I don’t recognize that mood.”.

Using “Elif” in Loops

“Elif” can be used in for, while, and other types of loops to evaluate conditional statements at each iteration of the loop. Here’s an example:

for i in range(1, 6):
    if i == 1:
        print("One.")
    elif i == 2:
        print("Two.")
    elif i == 3:
        print("Three.")
    else:
        print("A lot.")

In this example, we’re using a “for” loop to print numbers from 1 to 5. At each iteration of the loop, the program checks the value of “i” and prints a different message depending on its value. If “i” is equal to 1, it prints “One.”. If “i” is equal to 2, it prints “Two.”. If “i” is equal to 3, it prints “Three.”. If “i” is any other value, it prints “A lot.”.

You can also use “elif” in while loops. Here’s an example:

x = 0

while x < 10:
    if x <= 5:
        print("Low")
    elif x <= 8:
        print("Medium")
    else:
        print("High")
    
    x += 1

In this example, we’re using a while loop to print different messages depending on the value of “x”. If “x” is less than or equal to 5, it prints “Low”. If “x” is between 6 and 8, it prints “Medium”. If “x” is 9 or 10, it prints “High”. The loop continues until “x” is greater than or equal to 10.

Conclusion

In Python, the “elif” keyword allows you to evaluate multiple conditional statements in a single block of code. You can use “elif” to write more complex programs with more advanced decision-making capabilities. Use “elif” in loops to evaluate conditional statements at each iteration of the loop. With these techniques, you can create more robust and flexible programs that can handle a wide range of situations.

I hope this tutorial has been helpful in understanding how to use “elif” in Python. If you have any questions or comments, feel free to leave them below!

Leave a Reply

Your email address will not be published. Required fields are marked *