CSE 160 Section 2
Data Types and Conditionals
CSE 160
CSE 160: Section 2
Logistics
CSE 160: Section 2
HW 1 Tips
CSE 160: Section 2
DiffChecker Example
Quarter: 26au
Quarter :26au
CSE 160: Section 2
DiffChecker Example
Quarter: 26au
Quarter :26au
CSE 160: Section 2
Data Types
CSE 160: Section 2
Data Types
CSE 160: Section 2
If / Else Overview
CSE 160: Section 2
If/ Else Structure
is_raining = True
is_sprinkling = False
if is_raining:
print("Bring an umbrella")
elif is_sprinkling:
print("Bring a raincoat")
else:
print("Bring sunglasses")
CSE 160: Section 2
If/ Else Structure
is_raining = True
is_sprinkling = False
if is_raining:
print("Bring an umbrella")
elif is_sprinkling:
print("Bring a raincoat")
else:
print("Bring sunglasses")
Output:
Bring an umbrella
CSE 160: Section 2
Section Handout Problems
CSE 160: Section 2
Warm-Up Problem
What is the difference between the behavior of the two conditionals provided?
if (0 <= x and x <= 9): print ("single digits") else: print("double digits") | if (0 <= x and x <= 9): print ("single digits") elif (x > 9 and x < 100): print("double digits") |
CSE 160: Section 2
Warm-Up Problem
What is the difference between the behavior of the two conditionals provided?
if (0 <= x and x <= 9): print ("single digits") else: print("double digits") | if (0 <= x and x<= 9): print ("single digits") elif (x > 9 and x < 100): print("double digits") |
The code on the left side counts any number larger than 9 or less than 0 to be in the double digits (negative numbers and numbers in the triple digits) while the code on the right side checks that x is greater than 9 and less than 100 to be in the double digits.
CSE 160: Section 2
Problem 1: Data Types
Evaluate each expression as if you were to enter it into a Python interpreter. What is the data type (e.g., integer, string) of each resulting value? If there is an error, write "Error" in the value column.
CSE 160: Section 2
Problem 1: Data Types
| Value | Type |
25 + 1 | 26 | int |
37 + 33 / 3.0 | 48.0 | float |
42 / 5 + 2.0 | 10.4 | float |
"May the force be with you." | May the force be with you. | String |
"CSE" + "160" | CSE160 | String |
"CSE" + 160 | Error | |
CSE 160: Section 2
Problem 2: Booleans
Evaluate each expression as if you were to enter it into a Python interpreter. (Hint: The answers are all booleans)
CSE 160: Section 2
Problem 2: Booleans
| Value |
True | True |
42 < 45 | True |
not 16 < 91 | False |
not 18 > 25 | True |
float(3) < 9 | True |
CSE 160: Section 2
Problem 3: Conditionals
For the following code, predict what gets printed
if temperature < 10:
print("Cold")
elif temperature < 20:
print("Cool")
elif temperature < 30:
print("Warm")
else:
print("Hot")
temperature | What gets printed? |
5 | |
10 | |
18 | |
20 | |
29 | |
31 | |
CSE 160: Section 2
Problem 3: Conditionals
For the following code, predict what gets printed
if temperature < 10:
print("Cold")
elif temperature < 20:
print("Cool")
elif temperature < 30:
print("Warm")
else:
print("Hot")
temperature | What gets printed? |
5 | Cold |
10 | Cool |
18 | Cool |
20 | Warm |
29 | Warm |
31 | Hot |
CSE 160: Section 2
Problem 4: More Conditionals
Your friend is bored and wants to try something new. You want to help them decide what to do by creating a program to decide between three possible activities:
The program should take three user inputs using the input() function:
CSE 160: Section 2
Problem 4: More Conditionals
if not in_seattle:
print("CAN")
CSE 160: Section 2
Problem 4: More Conditionals
b) If your friend wants to do an indoor activity, recommend SBP. If they want an outdoor activity, recommend GP. If they want an activity that can be indoors or outdoors, recommend CAN
CSE 160: Section 2
Problem 4: More Conditionals
b) If your friend wants to do an indoor activity, recommend SBP. If they want an outdoor activity, recommend GP. If they want an activity that can be indoors or outdoors, recommend CAN
if indoors == "Yes":
print("SBP")
elif indoors == "No":
print("GP")
elif indoors == "Maybe":
print("CAN")
CSE 160: Section 2
Problem 4: More Conditionals
c) If your friend wants to spend 3 hours or less on the activity, the program should recommend GP. If they want to spend between 4-15 hours, recommend SBP. Otherwise, recommend CAN.
CSE 160: Section 2
Problem 4: More Conditionals
c) If your friend wants to spend 3 hours or less on the activity, the program should recommend GP. If they want to spend between 4-15 hours, recommend SBP. Otherwise, recommend CAN.
if time <= 3:
print("GP")
elif time >= 4 and time <= 15:
print("SBP")
else:
print("CAN")
CSE 160: Section 2
Exit Ticket
CSE 160: Section 2
Exit Ticket
What is the output when the following program is run?
a = "rattlesnake"
b = "python"
c = "cobra"
x = int(10 / 3)
z = True
print("I")
print ("<" + str(x))
if z == True:
print (b)
else:
print (a)
CSE 160: Section 2
Exit Ticket
What is the output when the following program is run?
a = "rattlesnake"
b = "python"
c = "cobra"
x = int(10 / 3)
z = True
print("I")
print ("<" + str(x))
if z:
print (b)
else:
print (a)
I
<3
python
CSE 160: Section 2
Extra Time?
Get started on HW 1!
CSE 160: Section 2
Thanks for Coming!!
CSE 160: Section 2