1 of 30

CSE 160 Section 2

Data Types and Conditionals

CSE 160

CSE 160: Section 2

2 of 30

Logistics

  • How was HW0? Any issues?

​

  • HW1 (due Tuesday Oct 20 @ 11:59pm)
    • Submitting file on Gradescope - don’t change the file name
    • Submit reflections.py
    • Wait for autograder

​

  • Week 2 Section Exit Ticket (turn in before you leave)

​

​

​

​

CSE 160: Section 2

3 of 30

HW 1 Tips

  • Reminders:
    • To run the file: python FILE_NAME.py
    • Check for formatting errors - flake8: flake8 FILE_NAME.py
    • Don’t change the filenames!
  • Your output should exactly match the expected output.
    • blanks should be replaced with your answers

​

Diffchecker.com

​

​

​

CSE 160: Section 2

4 of 30

DiffChecker Example

Quarter: 26au

​

Quarter :26au

​

CSE 160: Section 2

5 of 30

DiffChecker Example

Quarter: 26au

​

Quarter :26au

​

CSE 160: Section 2

6 of 30

Data Types

CSE 160: Section 2

7 of 30

Data Types

  • Integers (int)
    • Whole numbers (including negatives)
    • 1, 2, 3, 0, -1, -100, 1000
  • Real numbers (float)
    • Decimal numbers, float “floating point”
    • 3.14, 2.718, 1.9999, 4.0
  • Strings (str)
    • Represent characters and text
    • "Hello", "Python", "3", "I love Python!", ""
  • Booleans (bool)
    • Only two values: True or False
    • Use boolean operators: <, <=, >, >=, ==, not, and, or

​

CSE 160: Section 2

8 of 30

If / Else Overview

CSE 160: Section 2

9 of 30

If/ Else Structure

  • Checks that condition is True/False, and executing code based on that condition

​

​

​

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

10 of 30

If/ Else Structure

  • Checks that condition is True/False, and executing code based on that condition

​

​

​

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

11 of 30

Section Handout Problems

CSE 160: Section 2

12 of 30

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

13 of 30

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

14 of 30

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.

​

  1. 25 + 1
  2. 37 + 33 / 3.0
  3. 42 / 5 + 2.0
  4. "May the force be with you."
  5. "CSE" + "160
  6. "CSE" + 160

CSE 160: Section 2

15 of 30

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

16 of 30

Problem 2: Booleans

Evaluate each expression as if you were to enter it into a Python interpreter. (Hint: The answers are all booleans)

​

  • True
  • 42 < 45
  • not 16 < 91
  • not 18 > 25
  • float(3) < 9

CSE 160: Section 2

17 of 30

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

18 of 30

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

19 of 30

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

20 of 30

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:

  • Get a monthly membership at Seattle Bouldering Project (SBP)
  • Go shopping in Canada for the weekend (CAN)
  • Take a walk to Gasworks Park (GP)

​

The program should take three user inputs using the input() function:

  • time: A numerical response to the prompt “How many hours do you want to engage in the activity?”
  • in_seattle: A boolean response to the prompt “Is the activity in Seattle?”
  • indoors: A string response to the prompt “Is this activity indoors?” [Yes, Maybe, No]

CSE 160: Section 2

21 of 30

Problem 4: More Conditionals

  1. If your friend wants an activity outside of Seattle, recommend CAN.

if not in_seattle:

print("CAN")

​

CSE 160: Section 2

22 of 30

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

23 of 30

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

24 of 30

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

25 of 30

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

26 of 30

Exit Ticket

CSE 160: Section 2

27 of 30

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

28 of 30

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

29 of 30

Extra Time?

Get started on HW 1!

CSE 160: Section 2

30 of 30

Thanks for Coming!!

CSE 160: Section 2