In this guide, I’ll explain how to perform IF, ELIF and ELSE in Python.
To start, here is a template that you may reference when performing IF, ELIF and ELSE in Python:
if condition1: perform an action if condition1 is met elif condition2: perform an action if condition2 is met else: perform an action if neither condition1 nor condition2 is met
Next, I’ll use simple examples to illustrate how to execute:
- IF and ELSE
- IF, ELIF and ELSE
- IF, ELIF and ELSE with input() function
- Nested IF
- IF using a tkinter graphical user interface (GUI)
By the end of this guide, you’ll be able to create the following GUI that will execute an IF, ELIF and ELSE in Python:
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 condition1: perform an action if condition1 is met else: perform an action if condition1 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:
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 condition1: perform an action if condition1 is met elif condition2: perform an action if condition2 is met else: perform an action if neither condition1 nor condition2 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:’
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:
And the result would be ‘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 set to 65, and the Membership is equal to ‘Yes.’ Once you run the code, you’ll get:
Example 5: IF using a tkinter graphical user interface (GUI)
In the final section of this guide, I’ll share the code to create a tkinter GUI that will execute the IF conditions based on the value typed within the GUI.
The conditions are:
- If the Age is equal or above 60, the person is eligible for a ‘Senior Discount’
- If the Age is from 18 to 59, there is ‘No discount’
- If the age is below 18, the person is eligible for a ‘Junior Discount’
Here is the full Python code:
import tkinter as tk root= tk.Tk() canvas1 = tk.Canvas(root, width = 500, height = 450) canvas1.pack() label1 = tk.Label(root, text='Type your Age: ') canvas1.create_window(100, 100, window=label1) entry1 = tk.Entry (root) canvas1.create_window(270, 100, window=entry1) def values(): global Age Age = float(entry1.get()) if Age >= 60: Discount = ('Discount Status: ','Senior Discount') label_Discount = tk.Label(root, text= Discount, bg='yellow') canvas1.create_window(270, 200, window=label_Discount) elif 18 <= Age < 60: Discount = ('Discount Status: ','No Discount') label_Discount = tk.Label(root, text= Discount, bg='yellow') canvas1.create_window(270, 200, window=label_Discount) else: Discount = ('Discount Status: ','Junior Discount') label_Discount = tk.Label(root, text= Discount, bg='yellow') canvas1.create_window(270, 200, window=label_Discount) button1 = tk.Button (root, text='Find your Discount Status',command=values, bg='orange') # button to call the 'values' command above canvas1.create_window(270, 150, window=button1) root.mainloop()
And when you run the code, you’ll see this GUI:
For example, type the Age of 17 in the entry box, and then press on the button ‘Find your Discount Status.’ The Discount status of ‘Junior Discount’ would appear in yellow:
Try different values to see the results…