Python is a versatile programming language, and conditional statements are the cornerstone of any programming language. In Python, the
elif statements are used to control the flow of your program. Let us deep dive into how to use Python if else.
What is an If Statement in Python?
if statement in Python is used to check whether a condition is true or false. If the condition is true, the indented block of code under the
if statement is executed.
city = "New York" if city == "New York": print("Welcome to New York!")
In this example, the
if statement checks whether the variable
city is equal to “New York”. Since it is true, the message “Welcome to New York!” is printed. You can see the below output:
The Elif Statement in Python
elif statement in Python is short for “else if”. It allows you to check multiple expressions for truth and execute a block of code as soon as one of the conditions is true.
Elif in Python Example:
city = "Los Angeles" if city == "New York": print("Welcome to New York!") elif city == "Los Angeles": print("Welcome to Los Angeles!")
In this example, since
city is not “New York”, the condition in the
if statement is false. The program then checks the
elif condition, and since
city is “Los Angeles”, it prints “Welcome to Los Angeles!” Check the output below:
The Else Statement in Python
else statement in Python captures anything that isn’t captured by the preceding conditions. It doesn’t require a condition. The block of code following an
else statement is executed if none of the conditions above it are true.
city = "Chicago" if city == "New York": print("Welcome to New York!") elif city == "Los Angeles": print("Welcome to Los Angeles!") else: print("Welcome to", city + "!")
In this example, since the
city is neither “New York” nor “Los Angeles”, the
else block is executed, printing “Welcome to Chicago!”. Here is the output.
Combining If, Elif, and Else in Python
Now let’s look at a more comprehensive example:
city = "Miami" if city == "New York": print("Welcome to the Empire State!") elif city == "Los Angeles": print("Welcome to the City of Angels!") elif city == "Chicago": print("Welcome to the Windy City!") elif city == "Miami": print("Welcome to the Magic City!") else: print("Welcome to", city + "!")
In this example, the variable
city is assigned the value “Miami”. The program checks through the
elif statements and finds that the
city is “Miami”, so it prints “Welcome to the Magic City!”
When using conditional statements, remember to:
- Keep your conditions simple and readable.
- Always indent the code inside the
elsestatements, as Python relies on indentation.
- Use comments to make your code more understandable for others (and for yourself when you look at it later).
Conditional statements like
else are powerful tools in Python that help in decision-making within your code? Here we discussed examples of Python if else.
You may also like:
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.