Python Conditions
Python conditions, in essence, are the decision-making tools of your code. They allow you to control the flow of your program based on whether certain statements are true or false. This makes your code more dynamic and responsive to different situations.
Now, let’s take a look at some breakdown of the key aspects of Python conditions.
Building Blocks
Python conditions, as we just learned, are the backbone of making decisions in your code. They rely on three essential building blocks to function.
Boolean Values
These are the foundation of logical expressions in Python and represent the outcome of comparisons or logical operations. They can be either…
True : Represents a condition being met or a statement being valid.
False : Represents a condition not being met or a statement being invalid.
Think of Boolean values as binary switches – they are either “on”(True) or “off”(False).
Comparison Operators
These operators allow you to compare values and evaluate their relationship. They return True or False based on the comparison.
- == (equal to) : Checks if two values are equal(ex : 5 == 5 is True)
- != (not equal to) : Checks if two values are not equal(ex : 10 != 5 is True)
- < (less than) : Checks if one value is strictly less than another(ex : 3 < 5 is True)
- > (greater than) : Checks if one value is strictly greater than another(ex : 8 > 4 is True)
- <= (less than or equal to) : Checks if one value is less than or equal to another(ex : 7 <= 7 is True)
- >= (greater than or equal to) : Checks if one value is greater than or equal to another(ex : 2 >= 1 is True)
Logical Operators
These operators combine multiple Boolean expressions into more complex conditions.
- and : Both conditions must be True for the combined conditions to be True(ex : (age >= 18) and (has_license) checks if both age is 18 or over and a license is present, returning True only if both conditions are True)
- or : At least one condition must be True for the combined condition to be True.(ex : (is_student) or (is_teacher) checks if either the person is a student or a teacher, returning True if either condition is True)
- not : Inverts the truth value of a condition(ex : not (is_night), if is_night is False, this expression becomes True).
age = 25
is_citizen = True
# Combining building blocks
if age >= 18 and is_citizen:
print("Eligible to vote")
else:
print("Not eligible to vote")
In this example, age >= 18 and is_citizen are Boolean expressions created using comparison operators. The and operator combines them, so the if condition only becomes True if both expressions are True.
Example uses of conditions
We have learned how conditions are being used in Python. Now let’s take a look at some examples and increase your skill level in using conditions in Python.
Checking a number
number = 10
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
This code checks if a number is even by using the modulo operator % to see if the remainder when divided by 2 is zero. If it is, the number is even and the first message is printed. Otherwise, the number is odd and the second message is printed.
The number is even
Age restriction
age = 18
if age >= 18:
print("You are allowed to enter.")
else:
print("You are not old enough.")
This code checks if someone is old enough to enter(replace the condition with your specific requirement). It uses a greater than or equal to(>=) operator to see if the age is 18 or more.
You are allowed to enter
Login validation
username = "admin"
password = "secret"
user_input_username = input("Enter username: ")
user_input_password = input("Enter password: ")
if user_input_username == username and user_input_password == password:
print("Login successful!")
else:
print("Invalid username or password.")
This code simulates a simple login by checking if the username and password entered by the user match the predefined values. It uses the “and” keyword to ensure both conditions are met.
Using strings
name = "Choi"
if name.lower() == "Jack": # convert name to lowercase for case-insensitive check
print("Hello Jack!")
else:
print("Hello", name)
This code checks if a name matches a specific string. Here, it converts the name to lowercase using the .lower() method for a case-insensitive comparison.
Helloe Choi