Here is a template that you may reference when performing IF, ELIF and ELSE in Python:
if condition_1:
perform an action if condition_1 is met
elif condition_2:
perform an action if condition_2 is met
else:
perform an action if neither condition_1 nor condition_2 is met
Next, you’ll see the following 4 examples:
- IF and ELSE
- IF, ELIF and ELSE
- IF, ELIF and ELSE with input() function
- Nested IF
Example 1: IF and ELSE
Suppose that you want to determine whether a person is eligible for a senior discount.
Here are the conditions that you decided to use:
- If the person’s age is equal or above 60, then the person is eligible for a ‘senior discount‘
- Else, there should be ‘no discount‘
To apply IF and ELSE in Python, you can utilize the following generic structure:
if condition_1:
perform an action if condition_1 is met
else:
perform an action if condition_1 is not met
And for our example, let’s say that the person’s age is 65. You may then use the Python code below to determine the person’s eligibility for the discount:
age = 65
if age >= 60:
print('Senior Discount')
else:
print('No Discount')
Once you run the code, you’ll get ‘Senior Discount,’ since the age of 65 meets the condition of age >= 60:
Senior Discount
Example 2: IF, ELIF and ELSE in Python
Now let’s add another layer. Specifically, let’s say that you want to include another discount group – the ‘Junior Discount‘ group – for people who are below the age of 18.
In that case, you may use the IF, ELIF and ELSE in Python:
if condition_1:
perform an action if condition_1 is met
elif condition_2:
perform an action if condition_2 is met
else:
perform an action if neither condition_1 nor condition_2 is met
And for our example, suppose that the person’s age is 27. You may then run the following Python code to determine the person’s eligibility for the discount:
age = 27
if age >= 60:
print('Senior Discount')
elif 18 <= age < 60:
print('No Discount')
else:
print('Junior Discount')
If you run the code, you’ll get ‘No Discount:’
No Discount
Example 3: IF, ELIF and ELSE with input() function
In the previous examples, we set the ‘age’ variable to a given value (e.g., age = 27).
But what if you want more flexibility to type any age, in order to determine the discount eligibility?
In that case, you may use the input() function in Python like this:
x = input()
age = float(x)
if age >= 60:
print('Senior Discount')
elif 18 <= age < 60:
print('No Discount')
else:
print('Junior Discount')
Once you run the code, you’ll be able to type any age. For example, type the age of 72, and then press Enter.
You’ll then get the result of ‘Senior Discount:’
72
Senior Discount
Example 4: Nested IF
Now let’s look at a Nested IF example, where you’ll have 2 variables:
- a variable for ‘age’; and
- a variable for ‘membership’
Here are the conditions to be used:
- IF the person’s age is equal or above 60, then the person is eligible for a ‘Senior Discount.’ Additionally, IF the senior person has membership (i.e., membership = ‘Yes’), then the person is eligible for a ‘Membership Gift.’ ELSE the person is not eligible for a ‘Membership Gift.’
- ELSE the person is not eligible for a Discount (in the case the person’s age is below 60).
Here is the complete Python code:
age = 65
membership = 'Yes'
if age >= 60:
print('Senior Discount')
if membership == 'Yes':
print('Membership Gift')
else:
print('No Gift')
else:
print('No Discount')
The ‘age’ in the Python code above is equal to 65, and the membership is equal to ‘Yes.’ Once you run the code, you’ll get:
Senior Discount
Membership Gift