What is elif in Python example?

Lauren 116 Published: 06/24/2024

What is elif in Python example?

I'm happy to help you with your question!

In Python, elif stands for "else if", which is a conditional statement used to test another condition after an earlier condition has been tested and found false.

The general syntax of elif in Python is as follows:

if condition1:

code block

elif condition2:

code block

Let's consider an example to better understand how elif works:

Suppose we want a program that checks if a person is eligible for a loan. The eligibility criteria are as follows:

Age must be at least 18 years old. Income should be at least $30,000 per year.

Here's an example code using elif:

age = 22

income = 25000

if age >= 18:

if income > 30000:

print("You are eligible for a loan.")

else:

print("Your income is too low. You may need to save more or improve your credit score.")

elif income > 30000:

print("You do not meet the age criteria, but your income is sufficient. You still need to be at least 18 years old to apply for a loan.")

else:

print("You do not meet either the age or income criteria.")

In this example:

The first if condition checks if the person's age is at least 18 years old. If the person meets the age criterion, the program then checks their income. If their income is at least $30,000 per year, they are eligible for a loan and the message "You are eligible for a loan." is printed. If the person does not meet the income criteria (i.e., their income is less than or equal to $30,000), the program prints "Your income is too low. You may need to save more or improve your credit score."

The elif clause checks if the condition after the comma is true. In this case, it's income > 30000. If this condition is true, the code within that block will execute.

If neither of these conditions is met (i.e., the person is less than 18 years old or their income is too low), the program prints "You do not meet either the age or income criteria."

In summary, elif allows you to test multiple conditions and execute different code blocks based on whether those conditions are true or false.

If-else statement in Python example

I'm glad you asked! Here's an example of using if-else statements in Python:

# Example of using if-else statements in Python
Define a variable for user input age

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

Use if-else statement to check the age and print out different messages

if age < 18:

print("You are still young! You are", age, "years old.")

elif 18 <= age < 65:

print("You are an adult now! You are", age, "years old.")

else:

print("You are a senior! You are", age, "years old.")

Now, let's try to use if-else statement for more complex logic

score = int(input("Enter your score (out of 100): "))

if score < 60:

grade = "F"

elif 60 <= score < 70:

grade = "D"

elif 70 <= score < 80:

grade = "C"

elif 80 <= score < 90:

grade = "B"

else:

grade = "A"

print("Your grade is:", grade)

Another example using if-else statement with multiple conditions

x, y = int(input("Enter x: ")), int(input("Enter y: "))

if x > y and x % 2 == 0 or y % 3 == 1:

print("Both conditions are true!")

elif x < y and y % 4 == 2:

print("First condition is false, second condition is true!")

else:

print("None of the conditions are true!")

In this example, we use if-else statements to check different age ranges and print out corresponding messages. We also show more complex examples where if-else statements are used with multiple conditions.

Here's a summary of how to use if-else statements in Python:

Start with an if statement followed by a condition (or conditions) enclosed in parentheses. The condition can be any valid expression using comparison operators like ==, <, >, !=, etc. If the condition is true, the code indented under the if block will be executed. You can use an else clause to specify what code should run if the original if condition is false. You can also add multiple conditions using the and and or logical operators.

I hope you find this example helpful!