Booleans and flow control.
Maria João Mira Paulo

Python - Module 3

- String
- Integer
- Float

- Lists

Tuples

- Dictionaries

 
 

- Boolean ...coming...

 
 
Variable Types

Recapping 

Ready?...😎

Booleans

Can only be one of two values, either:
True or False. 

- Booleans are used to make comparisons.

- False can also be written as 0.

- True can also be written as 1.

Comparison Operators

== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to

So... Let's put this into practice?

Booleans

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? 

Logical Operators

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 False

Let's try... 💻

Can you understand the results? 
(OR, AND, NOT)

- AND: true if both statements are true

- OR: true if one of the statements is true

- NOT: the opposite of the statement

True and True?

True and False?

False and True?

False and False?

And Truth Tables

True

False

False

False

True or True?

True or False?

False or True?

False or False?

Or Truth Tables

True

True

True

False

not True?

Not Truth Tables

False

not False?

True

Exercises

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)

Conditional Structures

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 🧐

Conditional Structures

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 🧐

Conditional Structures

"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!')

Conditional Structures

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!')

Multiple conditions

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!')

Exercise

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)

Walk, Drive, or Fly

Functions

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. 

Return a value

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.

Python - Module 3

By Maria João Mira Paulo

Python - Module 3

  • 417