Conditions in Python — if/else (5/100 Days of Python)
In some cases, we might want to execute a certain piece of code only in case a condition holds. This can be achieved with if
statements.
if statements
If statements in Python allow you to execute certain code only if a certain condition is met. They are an important part of any programming language and are used frequently in Python. The basic syntax is as follows:
# The start of the program...
if condition:
# code to be executed if the condition is True
# The rest of the program...
Here is an example of an if statement in Python:
x = 10
if x > 5:
print('x is greater than 5')
In this example, the code inside the if statement will only be executed if the condition x > 5
is true. Since x
is equal to 10, the code will be executed and x is greater than 5
will be printed to the console.
You can also add an else
statement to your if
statement, which will be executed if the condition is not met
If-else statements
An if-else statement in Python is used to execute a block of code if a condition is True, or a different block of code if the condition is False. Here is the basic syntax of an if-else statement in Python:
if condition:
# code to be executed if the condition is True
else:
# code to be executed if the condition is False
The condition in the if statement is a boolean expression that evaluates to either True or False. If the condition is True, the code in the if block will be executed. If the condition is False, the code in the else block will be executed. Here is an example of an if-else statement in Python:
x = 10 # Or get x from input with `x = int(input())`
if x > 5:
print('x is greater than 5')
else:
print('x is not greater than 5')
In this example, the condition x > 5
is evaluates to True
because x
is 10, which is greater than 5. Therefore, the code in the if block will be executed, and x is greater than 5
will be printed to the console.
Comparison operators
Comparison operators in Python allow you to compare two values and return a boolean value (True or False) based on the comparison. They are an important part of any programming language and are used frequently in Python.
Here are the comparison operators available in Python:
- Equal to (
==
): Returns True if the values on either side of the operator are equal. - Not equal to (
!=
): Returns True if the values on either side of the operator are not equal. - Less than (
<
): Returns True if the value on the left side of the operator is less than the value on the right side. - Greater than (
>
): Returns True if the value on the left side of the operator is greater than the value on the right side. - Less than or equal to (
<=
): Returns True if the value on the left side of the operator is less than or equal to the value on the right side. - Greater than or equal to (
>=
): Returns True if the value on the left side of the operator is greater than or equal to the value on the right side.
They can be used in the code as any other operation like addition, multiplication, subtraction, etc:
x = 5
y = 10
print(x == y) # False -> check if x is equal to y
print(x != y) # True -> check if x is different from y
print(x > y) # False -> check if x is greater than y
print(x < y) # True -> check if x is smaller than y
print(x >= y) # False -> check if x is greater than or equal to y
print(x <= y) # True -> check if x is smaller than or equal to y
Comparison operators are most usually used in if
statements. Note that the equality is checked with a double ==
as a single equality would mean an assignment of a value in Python.
What’s next?
- If you found this story valuable, please consider clapping multiple times (this really helps a lot!)
- Hands-on Practice: Free Python Course — if statements
- Full series: 100 Days of Python
- Previous topic: Boolean variables and boolean arithmetic in Python
- Next topic: Nested conditions in Python