What is elif Statement in Python With Example

Here’s a simple example of an if-else statement in Python:

# Example code to check if a number is even or odd

# User input
num = int(input("Enter a number: "))

# Check if the number is even or odd
if num % 2 == 0:
    print(f"{num} is an even number.")
else:
    print(f"{num} is an odd number.")Code language: Python (python)

In this example, the user is prompted to enter a number, and then the program checks if the number is even or odd using the % operator (which gives the remainder of the division).

If the remainder is zero (i.e., the number is divisible by 2), it means the number is even, and the program prints that it is even. Otherwise, it prints that the number is odd.

What does Elif mean in Python?

In Python, elif is a keyword that stands for “else if”. It is used in conditional statements to provide an alternative condition to check when the initial if condition evaluates to False. Essentially, elif allows you to chain multiple conditions together and handle different cases based on the evaluation of these conditions.

The syntax for using elif is as follows:

if condition1:
    # Code block to be executed if condition1 is True
elif condition2:
    # Code block to be executed if condition1 is False and condition2 is True
elif condition3:
    # Code block to be executed if condition1 and condition2 are False and condition3 is True
else:
    # Code block to be executed if none of the above conditions are TrueCode language: Python (python)

The elif statements are optional and can appear zero or more times in an if-elif-else block. When the if condition is evaluated to False, Python checks the subsequent elif conditions one by one until it finds a True condition or reaches the else block (if provided). If none of the if or elif conditions are True, the code inside the else block (if present) will be executed.

Here’s an example:

# Example code to determine the grade based on the score

score = int(input("Enter your score: "))

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
elif score >= 60:
    grade = "D"
else:
    grade = "F"

print(f"Your grade is: {grade}")Code language: Python (python)

In this example, we use elif statements to evaluate different score ranges and assign corresponding grades to the students based on their scores.’

How do you use Elif and or in Python?

You can use elif and or in combination to create more complex conditional statements that check multiple conditions. The or operator is used to combine two or more conditions, and the elif keyword allows you to specify an alternative condition to check if the previous if or elif conditions are not met.

Here’s the syntax for using elif and or together:

if condition1:
    # Code block to be executed if condition1 is True
elif condition2 or condition3:
    # Code block to be executed if condition1 is False and either condition2 or condition3 is True
elif condition4:
    # Code block to be executed if condition1, condition2, and condition3 are all False and condition4 is True
else:
    # Code block to be executed if none of the above conditions are True
Code language: Python (python)

Let’s see an example of using elif and or together:

# Example code to check if a number is positive, negative, or zero

num = float(input("Enter a number: "))

if num > 0:
    print("The number is positive.")
elif num < 0:
    print("The number is negative.")
elif num == 0:
    print("The number is zero.")
else:
    print("Invalid input.")Code language: Python (python)

In this example, we use elif and or to check whether the number entered by the user is positive, negative, or zero.

The first if condition checks if the number is greater than 0, then the subsequent elif condition with or checks if the number is less than 0, and finally, the last elif condition checks if the number is equal to 0.

The else block is used for any other invalid input that doesn’t fall into the specified conditions.

Read More;

  • Yaryna Ostapchuk

    I am an enthusiastic learner and aspiring Python developer with expertise in Django and Flask. I pursued my education at Ivan Franko Lviv University, specializing in the Faculty of Physics. My skills encompass Python programming, backend development, and working with databases. I am well-versed in various computer software, including Ubuntu, Linux, MaximDL, LabView, C/C++, and Python, among others.

    View all posts

Leave a Comment