Building on our previous lesson about conditional statements in Python, where we explored how to make decisions in code based on conditions, we will now delve into the world of comparisons and Boolean logic. These concepts are the foundation of creating conditions and controlling the flow of your programs, allowing you to create dynamic and responsive code. Let's dive into the realm of comparisons and Boolean logic to further enhance your coding skills.

Comparisons operators

In Python, comparisons are used to compare values or expressions and determine whether they are true or false. Comparisons are typically made using comparison operators. There are six main comparison operators, each serving a specific purpose:

Checking for Equality
The == operator checks if two values or expressions are equal. It returns True if they are equal and False otherwise.
x = 5
y = 5
print(x == y) # Output: True
Checking for Inequality
The != operator checks if two values or expressions are not equal. It returns True if they are not equal and False otherwise.
x = 5
y = 5
print(x == y) # Output: True
Greater than Comparison
The > operator checks if the left operand is greater than the right operand. It returns True if the left operand is greater and False otherwise.
x = 7
y = 5
print(x > y) # Output: True
Less than Comparison
The < operator checks if the left operand is less than the right operand. It returns True if the left operand is less and False otherwise.
x = 3
y = 5
print(x < y) # Output: True
Greater Than or Equal To
The >= operator checks if the left operand is greater than or equal to the right operand. It returns True if the left operand is greater or equal and False otherwise.
x = 5
y = 5
print(x >= y) # Output: True
Less Than or Equal To
The <= operator checks if the left operand is less than or equal to the right operand. It returns True if the left operand is less or equal and False otherwise.
x = 3
y = 5
print(x <= y) # Output: True

Quiz Question

What will be the result of this statement?

if 8 > 3
print(True)

Boolean logic involves using logical operators to combine or manipulate Boolean values (True or False). Python provides three main logical operators that are essential for creating complex conditional expressions.

Logical operators in python

The AND Operator
The and operator returns True if both operands are True. If either operand is False, it returns False.
x = True
y = False
print(x and y) # Output: False
The OR Operator
The or operator returns True if at least one of the operands is True. If both operands are False, it returns False.
x = True
y = False
print(x or y) # Output: True
The NOT Operator
The not operator returns the opposite Boolean value of the operand. If the operand is True, not returns False, and vice versa.
x = True
print(not x) # Output: False

Combining comparisons with boolean logic

You can combine comparisons and Boolean logic to create complex conditional expressions. This is especially useful when you need to check multiple conditions at once.

Example of Combined Conditions
age = 25
if age >= 18 and age <= 60:
print("You are eligible for work.")
else:
print("You are not eligible for work.")

In this example, we combine two comparisons (age >= 18 and age <= 60) using the and logical operator to check if the age falls within a certain range. The message "You are eligible for work." is printed if both conditions are true.

Another Example with Temperature
temperature = 30
if temperature > 0 and temperature < 100:
print("The temperature is within a normal range.")
else:
print("The temperature is outside the normal range.")

In this example, we use the and operator to ensure that the temperature is both above 0 and below 100.

Quiz Question

Match the following logical operator to their respective descriptions.

In short…

Understanding comparisons and Boolean logic is crucial for writing effective, decision-making code in Python. These concepts are used extensively in conditional statements, loops, and many other aspects of programming. By mastering comparison operators and logical operators, you can create more dynamic and responsive programs that handle a wide range of scenarios and inputs.