Booleans and flow control.
Maria João Mira Paulo
Variable Types
Ready?...😎
Can only be one of two values, either:
True or False.
| == | Equal to |
|---|---|
| != | Not equal to |
| < | Less than |
| > | Greater than |
| <= | Less than or equal to |
| >= | Greater than or equal to |
x = 5
y = 8
print("x == y:", x == y)
print("x != y:", x != y)
print("x < y:", x < y)
print("x > y:", x > y)
print("x <= y:", x <= y)
print("x >= y:", x >= y)Let's try... 💻
Can you explain the results?
print((9 > 7) and (2 < 4)) # Both original expressions are True
print((8 == 8) or (6 != 6)) # One original expression is True
print(not(3 <= 1)) # The original expression is FalseLet's try... 💻
Can you understand the results?
(OR, AND, NOT)
Take two inputs from user and check whether they are equal or not.
1.
Take two number and check whether the sum is greater than 5, less than 5 or equal to 5.
2.
Judge the following expressions :
- not(True and True)
- True or False
- not(False and True)
- False and False
3.
(Booleans and Logic Operations)
Evaluates an if statement and if True, the code associated with it is executed.
age = 32
if age >= 35:
print('You are old enough to be the President.')
print('Have a nice day!')INDENTATION CHANGES EVERYTHING!! So be careful 🧐
Evaluates an if statement and if True, the code associated with it is executed.
age = 32
if age >= 35:
print('You are old enough to be the President.')
print('Have a nice day!')INDENTATION CHANGES EVERYTHING!! So be careful 🧐
"Have a nice day" will always appear right? What could we do send a message only to the people that are not old enough to be President?
age = 32
if age >= 35:
print('You are old enough to be the President.')
print('Have a nice day!')Exactly ✅ Using the else statement.
age = 32
if age >= 35:
print('You are old enough to be the President.')
else: print('You are not old enough to be the President.')
print('Have a nice day!')To evaluate multiple conditions we use elif, aka else if.
age = 103
if age >= 35:
print('You are old enough to be a Representative, Senator, or the President.')
elif age >= 30:
print('You are old enough to be a Senator.')
elif age >= 25:
print('You are old enough to be a Representative.')
else:
print('You are not old enough to be a Representative, Senator, or the
President.')
print('Have a nice day!')Try creating a program that will ask the user how far they wish to travel. If they express a desire to
travel less than three miles, have the program tell them to walk. If they desire to travel more than three
miles, but less than three hundred miles, advise them that they should drive. In any instance where
they want to travel three hundred or more miles, tell them to fly.
1.
(Conditional Structures)
1. To create a function, use the def keyword followed by the name of the function.
2. Always follow the function name with a set of parentheses.
3. If your function accepts parameters you may include the
names of those parameters within the parentheses, separating them with commas.
def say_hello(name):
print('Hello ' + name)
def say_hello(name = 'there'):
print('Hello ' + name)To create optional parameters,
set a default value by using the equals sign.
def say_hello(name):
return 'Hello ' + name
greeting = say_hello("John")The return statement will exit the function and pass back anything that follows return.