Python allows you to combine multiple conditions using logical operators.
The three commonly used logical operators are:
Operator
Meaning
and
True when all conditions are true
or
True when at least one condition is true
not
Reverses the Boolean result
Using and
The and operator requires both conditions to be true in order for the overall expression to yield a true result. It is crucial to understand how this operator functions within logical expressions and how it can affect the outcome of decision-making processes in programming.
Input
Output
age = 25
has_license = True
if age >= 18 and has_license:
print("You can drive.")
You can drive.
Using or
The or operator returns true when at least one condition is true.
Input
Output
day = "Sunday"
if day == "Saturday" or day == "Sunday":
print("It is the weekend.")
It is the weekend.
Using not
The not operator reverses a condition, which can often be very useful in programming. It allows us to check for the opposite of a specified condition, making our code more flexible and helping to control the flow based on different scenarios.
Input
Output
is_raining = False
if not is_raining:
print("You can go outside.")
You can go outside.
Regular if
Short-Hand if
age = 20
if age >= 18:
print("Adult")
age = 20
if age >= 18: print("Adult")
Output
Adult
Short-hand if statements can be useful for very simple conditions. However, for longer or more complex logic, the normal multi-line format is usually easier to read.

