2.3 - Logical Operators

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Decisions

Logical Operators
ICS 3U0
Today’s Agenda

Making more challenging conditions using


AND, OR and NOT.

Learn about the Boolean datatype.


Example – Combining Conditions
Let’s write a program to see if a person is eligible
to vote. There are two conditions:

1. Canadian citizen.
2. At least 18 years old.

In plain language, you might say: “If you are a


Canadian citizen and at least 18 years old, then you
can vote.”
Logical Operators
Logical operators allow us to combine multiple
conditions into a single statement
code can be made shorter (more efficient)
in some ways, these conditions are more like our natural
way of thinking

We are going to learn three ways to combine


comparisons
AND (all conditions must be true)
OR (at least one condition must be true)
NOT (negates the condition)
Example … using AND
# Get country of origin and age
country = input("What is your country of
origin? ")
age = int(input("What is your age? "))

# If country is Canada and age is greater


than 18, then vote!
if country == "Canada" and age >= 18:
print("Don't forget to vote!")
Logical Operators - AND
 When using AND, we require that all conditions be true at
the same time

 This is the "picky" logical operator

 For example: "I like movies that have action and comedy“. To
a computer, this person only likes movies that include both
action and comedy
(movie == action) and (movie == comedy)
AND Examples
Example 1:
1 < 2 and 5 > 3
(1 < 2) is TRUE, (5 > 3) is TRUE, so overall the
statement evaluates to TRUE What does this
mean? Evaluates
to true? Evaluates
Example 2: to false?

5 == 5 and "up" == "down"


(5 = 5) is TRUE, but (“up” = “down”) is FALSE, so
overall the statement evaluates to FALSE
Example … using OR
Write a program that requires an input to be less than
5 or greater than 20.
#Get number
number = int(input("Please enter a number: "))

#Check if the number meets requirements


if number > 20 or number < 5:
print(number, "is ok.")
else:
print(number, "needs to be greater than 20
or less than 5.")
Logical Operators - OR
When using OR, we require that only ONE of the conditions
be true

This logical operator is less stringent than AND

For example: "I like movies that have action or comedy“. To a


computer, this person likes movies that have action, or
comedy, or both
(movie == action) or (movie == comedy)
OR Examples
Example 1:
1 < 2 OR 5 > 3
(1 < 2) is TRUE, (5 > 3) is TRUE, so the overall statement
is TRUE. What does this
mean? Evaluates
Example 2: to true? Evaluates
to false?
5 == 5 OR "up" == "down"
(5 = 5) is TRUE, (“up” = “down”) is FALSE. The OR
operator only requires that at least one condition is
TRUE, so the overall statement is TRUE.
Logical Operators - NOT
When using NOT, the condition becomes TRUE if
FALSE and FALSE if TRUE

In general, NOT operators are often quite difficult


to read in computer code, and our intentions will
usually be clearer if we can eliminate them
NOT Examples
Example:
if not(age >= 17):
print("Hey, you're too young to get a
licence!") Is there an easier
way to write this
code?
Code rewritten more logically:
if age < 17:
print("Hey, you're too young to get a
licence!")
Example … using NOT
Write a program that requires a particular user name to
be valid before continuing. Loopy is the user name.
#Get username
user_name = input("Please enter your name: ")

#Check if name is valid


if not(user_name == "Loopy"):
print("Sorry, you cannot continue!")
else:
print("You can continue!")
You Try – All Together
Suppose we can slay the dragon only if our magic lightsabre
sword is charged to 90% or higher, and we have 100 or more
energy units in our protective shield.

Write the code for this ... which logical operator would you
use?

Can you write the code with a different logical operator?


Which one?
Exercises – Logical Operators

Complete Exercise 2.3 – Day 1


Logical Operators
Boolean DataType
We have been working with the following datatypes
throughout this course:

• int
• float
• string

We have also been sorta, kinda,


half-heartedly danced around
this datatype as well:

• boolean
Boolean DataType
Boolean variables are assigned one of two values
• Either a boolean variable is True
• Or a boolean variable is False

A boolean variable is assigned exactly the same way as an


integer, float or string variable.
Note that there are no
quotes – if you put
next_num = False quotes, you have created
a STRING variable ...
done = True without the quotes, you
have created a BOOLEAN
variables
Boolean DataType Examples
Example:
good_num = True
if good_num:
print("Hey, you're a good number!")

Example:
bad_num = False
if not bad_num:
print("Hey, you're a good number!")
Exercises – Logical Operators

Complete Exercise 2.3 – Day 2


Logical Operators

You might also like