Branching
- so far: sequential execution of statemetns
- branch
- a group of statements are executed only if some something condition is true
- boolean expression: evaluates to True or False
if
is a reserved word
- if else branches:
else
is a reserved word - if elseif else:
elseif
is a reservednum = int(input("Enter an integer: ")) if num < 100: num = abs(num) * 10 else: num = num // 10 print(f'the value of num is {num}')
Boolean expressions¶
- Boolean
- is a type
- two possible values: True or False
- Boolean expression: evaluales to True or False
==
- equality operator returns True or False
!=
- not equal operator returns True or False
user_num = int(input('Enter a number: ')) div_remainder = user_num % 2 if div_remainder == 0: print(f'{user_num} is even.') else: print(f'{user_num} is odd.')
- not equal operator returns True or False
Multiple if-else branches¶
if dollars == 200:
print "You have enough to go"
elif dollars == 150:
print "You need 50 more"
elif dollars == 100:
print "You have half of what you need"
else:
print "You are not close"
print "Good bye!"
¶
if dollars == 200:
print "You have enough to go"
elif dollars == 150:
print "You need 50 more"
elif dollars == 100:
print "You have half of what you need"
else:
print "You are not close"
print "Good bye!"
Relational operators¶
<
less than<=
less than or equal>
greater than>=
greater than or equal
each returns True or False
number = int(input("Enter an integer: "))
if number < 0:
print(number, "is negative")
else:
print(number, "is not negative")
print("Until next time...")
Logical operators¶
and
or
not
each returns True or False
- Precedence
(
)
- artihmetic as learned already
<
<=
>
>=
==
!=
not
and
or
note: left to right when equal precedence
- what is the value of each expression?
a = 3 b = 4 c = 5 print(a < b and b < c) print(a < b or b < c) print(a < b and b > c) print(a < b or b > c) print(not a < b) print(a > b or not a > c and b > c)
Check understanding¶
- how to check if
cost
is within the range of 200 to 2000. What is the boolean expression? cost >= 200 and cost <= 2000
* want to determine if it is good walking weather. Assume two variable temperature and perecentage chance of rain. What is an expression to determine if a good time to walk?¶
rain_chance = int(input("what is percentage chance of rain? "))
temperature= int(input("What is the current temperature? "))
if rain_chance <= 40 and (temperature > 32 and temperature < 90):
print("enjoy your walk")
else:
print("wait for another day")
¶
rain_chance = int(input("what is percentage chance of rain? "))
temperature= int(input("What is the current temperature? "))
if rain_chance <= 40 and (temperature > 32 and temperature < 90):
print("enjoy your walk")
else:
print("wait for another day")
- what is wrong with this?
grade = 98 if (grade >= 90): print("You got an A!") if (grade >= 80): print("You got a B!") else: print("You got something else")
- nested if statemetns
if num >= 0: if num == 0: print("Zero") else: print("Positive number") else: print("Negative number")
Code Blocks and Indentation¶
- identation matters
funds = int(input("Enter your account balance: "))
withdraw = int(input("How much cash do you need? "))
if funds > withdraw:
print("You have the funds to continue")
funds = funds - withdraw
print(f'funds left {funds}')
¶
funds = int(input("Enter your account balance: "))
withdraw = int(input("How much cash do you need? "))
if funds > withdraw:
print("You have the funds to continue")
funds = funds - withdraw
print(f'funds left {funds}')
- why do we care about whitespace vs tabs?
- autograders and testcases
- precision is critical
=
and==
if cost < 100:
vsif cost <= 100: