booleaN
and
conditional logic
Objectives
- Learn how to use comparison operators to make a basic program
- Learn how to get user input in Python
- Learn about "Truthiness"
There is a built-in function in Python called "input" that will prompt the user and store the result to a variable.
User Input
name = input("Enter your name here: ")Enter your name here: Tyrion Lannistername'Tyrion Lannister'BOOLEAN EXPRESSIONS
Conditional logic using if statements represents different paths a program can take based on some type of comparison of input.
Conditional Statements
if some condition is True:
do something
elif some other condition is True:
do something
else:
do something
Conditional logic using if statements represents different paths a program can take based on some type of comparison of input.
Conditional Checks
if name == "Arya Stark":
print("Valar Morghulis")
elif name == "Jon Snow":
print("You know nothing")
else:
print("Carry on")name
"Jon Snow"
*
"Arya Stark"
"You know nothing."
"Valar Morghulis."
"Carry on."
In Python, all conditional checks resolve to True or False.
Truthiness
x = 1
x is 1 # True
x is 0 # FalseWe can call values that will resolve to True "truthy", or values that will resolve to False "falsy".
Besides False conditional checks, other things that are naturally falsy include: empty objects, empty strings, None, and zero.
Here is a list of comparison operators.
In the examples, a = 1 and b = 1
Comparison Operators
| Op | What it does | Example |
|---|---|---|
| == | Truthy if a has the same value as b | a == b # True |
| != | Truthy if a does NOT have the same value as b | a != b # False |
| > < |
Truthy if a is greater than b Truthy if a is less than be b |
a > b # False a < b # False |
| >= <= |
Truthy if a is greater than or equal to b Truthy if a is less than or equal to b |
a >= b # True a <= b # True |
In Python, the following operators can be used to make Boolean Logic comparisons or statements:
Logical Operators
| Op | What it does | Example |
|---|---|---|
| and | Truthy if both a AND b are true (logical conjunction) |
if a and b: print(c) |
| or | Truthy if either a OR b are true (logical disjunction) |
if am_tired or is_bedtime: print("go to sleep") |
| not | Truthy if the opposite of a is true (logical negation) |
if not is_weekend: print("go to work") |
"It depends upon what the meaning of the word 'is' is."
is vs. "=="
- Bill Clinton
In python, "==" and "is" are very similar comparators, however they are not the same.
a = 1
a == 1 # True
a is 1 # Truea = [1, 2, 3] # a list of numbers
b = [1, 2, 3]
a == b # True
a is b # Falsec = b
b is c # True"is" is only truthy if the variables reference the same item in memory
BOUNCER CODE-ALONG
(with a nested conditional)
The following program, bouncer.py, determines whether the user can enter the club or not.
Bouncer Solution 1
age = input("How old are you: ")
if age:
age = int(age)
if age >= 18 and age < 21:
print("You can enter, but need a wristband!")
elif age >= 21:
print("You are good to enter and can drink!")
else:
print("You can't come in, little one! :(")
else:
print("Please enter an age!")It also checks to make sure the user entered an age
The following program, bouncer.py, determines whether the user can enter the club or not.
Bouncer Solution 2
age = input("How old are you: ")
if age:
age = int(age)
if age >= 21:
print("You are good to enter and can drink!")
elif age >= 18:
print("You can enter, but need a wristband!")
else:
print("You can't come in, little one! :(")
else:
print("Please enter an age!")Slightly refactored conditional logic
Recap
- Conditional logic evaluates whether statements are truthy or not
- Conditional logic can control the flow of a program
- We can use comparison and logical operators to make conditional if statements
ROCK 🤜🏻
PAPER ✋🏽 SCISSORS ✌🏾
"Try this on your own or treat it as a code-along"
THe "Basic" Version
...rock...
...paper...
...scissors...
(enter Player 1's choice): rock
(enter Player 2's choice): paper
SHOOT!
player2 winsIt may not be the most fun game ever made
THe "Less Basic" Version
...rock...
...paper...
...scissors...
(Enter your choice): paper
The computer plays: scissors
Computer wins!Featuring the dumbest "AI" ever
HINT!
Research randint
You will need to import something
🙀
YOUR TURN
Boolean and Conditional Logic
By colt
Boolean and Conditional Logic
- 10,652