5.3 Logical operators
5.3a What are logical operators?
Logical operators in Python are used to combine conditional statements and produce Boolean values (True
or False
). They help in creating complex conditions. Here are the primary logical operators:
and |
or |
not |
and - Returns True if both statements are true.
Condition 1 | Logical operator | Condition 2 | Returns |
---|---|---|---|
True | and | True | True |
True | and | False | False |
False | and | True | False |
False | and | False | False |
Example 5.3.1 - Demonstrating the logical and
operator.
Example 5.3.1 - Output
True
or - Returns True if at least one of the statements is true.
Condition 1 | Logical operator | Condition 2 | Returns |
---|---|---|---|
True | or | True | True |
True | or | False | True |
False | or | True | True |
False | or | False | False |
Example 5.3.2 - Demonstrating the logical or
operator.
Example 5.3.2 - Output
True
not - Reverses the result; returns True if the statement is false.
Logical operator | Condition | Returns |
---|---|---|
not | True | False |
not | False | True |
Example 5.3.3 - Demonstrating the logical not
operator.
Example 5.3.3 - Output
False
5.3b Why do we use logical operators?
Logical operators are used to build more complex conditional statements that require multiple conditions to be evaluated. They are essential for creating more sophisticated decision-making structures in your programs.
5.3c Interpret a program snippet that includes logical operators
Example 5.3.4 - Here’s a simple example to demonstrate how logical operators can be used in Python.
age = 16
has_ticket = True
# Using 'and' to check if both conditions are true
if age >= 18 and has_ticket:
print("You can enter the movie.")
else:
print("You cannot enter the movie.") # This will be printed
# Using 'or' to check if at least one condition is true
if age >= 18 or has_ticket:
print("You can enter the movie.") # This will be printed
else:
print("You cannot enter the movie.")
# Using 'not' to reverse the condition
if not has_ticket:
print("You need a ticket to enter.")
else:
print("You have a ticket, you can enter.") # This will be printed
Example 5.3.4 - Output
You cannot enter the movie. You can enter the movie. You have a ticket, you can enter.
Example 5.3.4 - Explanation
-
and:
-
Condition:
age >= 18 and has_ticket
-
Explanation: Checks if both
age
is greater than or equal to 18 andhas_ticket
isTrue
. Sinceage
is 16, the condition isFalse
and it prints "You cannot enter the movie."
-
-
or:
- Condition:
age >= 18 or has_ticket
- Explanation: Checks if either
age
is greater than or equal to 18 or has_ticket
isTrue
. Sincehas_ticket
isTrue
, the condition isTrue
and it prints "You can enter the movie."
- Condition:
-
not:
- Condition:
not has_ticket
- Explanation: Reverses the value of has
_ticket
. Since has_ticket
isTrue
,not has_ticket
isFalse
and it prints "You have a ticket, you can enter."
- Condition:
Exercise 5.3.1 - Write a Python program that determines if someone can drive based on their age and whether they have a driving license. Use if
, elif
, and else
statements along with logical operators (and
, or
, not
) to implement the following rules:
- If the person is 18 years old or older and has a driving license, print "You can drive."
- If the person is 18 years old or older or has a driving license, print "You are either old enough or have a license to drive."
- If the person does not have a driving license, print "You need a license to drive."
The program should ask the user to input their age and whether they have a driving license (yes or no), then print out the appropriate message based on the inputs.
Exercise 5.3.1 - Model Answer - Make sure to work out the exercise before checking the model answer.