Getting Started with Python — if, else, and elif Statements

TechwithJulles
3 min readFeb 22, 2023

In Python, the if, else, and elif statements are used to control the flow of execution of a program based on certain conditions. In this article, we'll take a look at how to use these statements to create conditional logic in your Python programs.

If Statement

The if statement is used to execute a code block if a certain condition is true. Here's an example:

x = 10
if x > 5:
print("x is greater than 5")

In this example, the code block under the if statement is executed only if the variable x is greater than 5. If x is less than or equal to 5, the code block is not executed.

Else Statement

The else statement is used to execute a code block if the condition of an if statement is not true. Here's an example:

x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")

In this example, the code block under the if statement is not executed because x is less than or equal to 5. Instead, the code block under the else statement is executed.

Elif Statement

The elif statement is used to execute a code block if a certain condition is true and the condition of the preceding if statement is not true. Here's an example:

x = 3
if x > 5:
print("x is greater than 5")
elif x > 0:
print("x is greater than 0 but less than or equal to 5")
else:
print("x is less than or equal to 0")

In this example, the first condition of the if statement is not true, so the condition of the elif statement is checked. Since x is greater than 0, the code block under the elif statement is executed.

Using Multiple Conditions

You can also use logical operators to combine multiple conditions in an if statement. Here are some examples:

x = 10
y = 5
if x > 5 and y < 10:
print("Both conditions are true")

if x > 5 or y > 10:
print("At least one condition is true")

if not x == y:
print("x is not equal to y")

In the first example, the and operator is used to combine two conditions: x > 5 and y < 10. Both conditions must be true for the code block to be executed.

In the second example, the or operator is used to check if at least one of the conditions is true.

In the third example, the not operator is used to negate the condition x == y. If the condition is false, the code block is executed.

Conclusion

The if, else, and elif statements are essential building blocks of conditional logic in Python. By combining these statements with logical operators, you can create complex conditional statements that can handle a wide range of scenarios. With practice, you'll become proficient at using these statements to create powerful Python programs.

Part 4: Getting Started with Python: Operators | by TechwithJulles | Medium | Medium

Part 6: Getting Started with Python — Looping | by TechwithJulles | Feb, 2023 | Medium

Article Notebook — Google Drive

If you enjoyed this article and would like to show your support, feel free to buy me a coffee! Your support is greatly appreciated and it helps me continue creating content that you love. You can make a contribution by following this link: Buy Me a Coffee. Thank you for your generosity and for being a part of this journey!

--

--

TechwithJulles

I'm a software developer who enjoys teaching people about programming and the tech world. #TechWithJulles