0
, 1
, 2
, 3
, ...1.23
, 2.45
, 5.0
, ...'hello'
, 'x'
, ...True
, False
True
and False
1 < 0
False
1 < 2
True
if
Statements¶if
statement syntax in Python isn't quite as easy as our examples, but it's pretty close...if
statements begin with the if
keyword and are followed by a logical test and a colonif tired == 'Yes':
if
statement as an indented blockif tired == 'Yes':
go_to_bed()
Note that the actual word then is never used - it's implied by the indent
Interpret as:
if condition_holds:
execute_code()
if tired == 'Yes':
brush_teeth()
turn_off_lights()
go_to_bed()
tired == 'Yes'
is True
x = 'tired'
if x == 'tired':
print('Go to bed!')
Go to bed!
Describe what this statement is doing.
if denominator != 0:
quotient = numerator / denominator
Fill in the blanks to print the statement if x
is a negative number. Re-run multiple times.
import random
x = random.randint(-10, 10)
__ x _ __:
print(f'x = {x}, which is negative')
else
Statement¶else
statement must follow an if
statement and its indented then-blockif
statement, it begins with the else
keyword and is followed by a colon (:
)if tired == 'Yes':
go_to_bed()
else:
read_a_book()
if
's then-block, there's also an indented block to be run if the else
path is takenelse
path be taken?
if tired == 'Yes':
go_to_bed()
else:
read_a_book()
else
path is taken in all cases when tired
is not equal to 'Yes'
Fill in the blanks to print the relevant statement for x
. Re-run multiple times.
import random
x = random.randint(-10, 10)
__ x _ __:
print('x is negative')
__:
print('x is positive')
elif
Statement¶elif
(else-if) statement can be used to add another if
statement in the execution pathif tired == 'Yes':
go_to_bed()
elif tired == 'A little':
rest_eyes()
else:
read_a_book()
elif
statements as you wantswitch
statement in other languages, this is Python's closest relativeFill in the blanks to print the relevant statement for x
.
import random
random.seed(7)
x = random.randint(-10, 10)
__ x _ __:
print('x is negative')
__ x _ __:
print('x is positive')
__:
print('x is zero')
if
-elif
-else
statements can be nested or combined to account for more complex logictired == 'Yes'
AND the time > 20:00
, you can nest if
statementsif tired == 'Yes':
if time > 20:00:
go_to_bed()
else
if tired == 'Yes':
if time > 20:00:
go_to_bed()
else:
take_a_nap()
if tired == 'Yes' and time > 20:00:
go_to_bed()
if tired == 'Yes' and time <= 20:00:
take_a_nap()
You will see a few common variants of our conditional test statement
All our examples thus far fall into this category:
x = 7
if x > 0:
print('x is positive')
x is positive
y = 'Ethan'
if y == 'Ethan':
print('y is not Brad')
y is not Brad
Sometimes we just want to know if a particular value exists in an object:
email = ['Ethan', 'Brad', 'spam']
if 'spam' in email:
print('You have spam in your email!')
You have spam in your email!
Sometimes we only want to operate on a particular type of object:
isinstance(email, list)
True
if isinstance(email, list):
print(f'You have {len(email)} email')
You have 3 email
x = 3
if isinstance(x, (int, float)):
print(x * 4)
else:
print('x is not a number')
12
Sometimes we want to operate on an object if all or any of the values are True
:
email_is_spam = [False, False, True]
# use `all()` or `any()`
if all(email_is_spam):
print('All your emails are spam!')
elif any(email_is_spam):
print('At least one of your emails is spam!')
else:
print('No spam!')
At least one of your emails is spam!
There's a lot of flexibility!
Are there any questions before we move on?