grapefruit print("grapefruit") C. grapefruit else: - - - PowerPoint PPT Presentation

grapefruit print grapefruit c grapefruit else d
SMART_READER_LITE
LIVE PREVIEW

grapefruit print("grapefruit") C. grapefruit else: - - - PowerPoint PPT Presentation

DS 2001 CS Programming Practicum Fall 2020 Felix Muzny mins ! a few we'll start in Practicum 2: Conditionals, For Loops, etc x = 12 if x > 10: What is the output of the given code? print("orange") - xouihftexs # to


slide-1
SLIDE 1 DS 2001 — CS Programming Practicum Fall 2020 — Felix Muzny

Practicum 2: Conditionals, For Loops, etc

1

What is the output of the given code?

  • A. orange
  • B. orange

grapefruit

  • C. grapefruit
  • D. grapefruit

lemon

  • E. lemon

x = 12 if x > 10: print("orange") elif x > 0: print("grapefruit") else: print("lemon")

we'll start in

a few

mins !

  • xouihftexs#to
slide-2
SLIDE 2

Logical Expressions

2
  • why "==" and not "=" to test for equality?
  • A. python is just trying to be tricky
  • B. "==" will test to see if two values are the same and "=" will test to see if the

values stored in two variables are the same

  • C. the premise of this question is wrong—you can use "=" to test for equality
  • D. "=" is has a different meaning in python code

5=-5

X==5

s

  • S

x

  • S

x Is

y

  • Yg

yet

  • O
  • assignment operator
slide-3
SLIDE 3

Logical Expressions

3
  • What are the results of evaluating the following logical expressions?
  • 5 != 7
  • 13.2 > 6.6 and 6.6 < 3
  • 13.2 > 6.6 or 6.6 < 3
  • 'a' == 'apple'
  • 'a' < 'g'
  • 'A' < 'g'
  • D True

(

T

)

(

F

) → False

T

F

→ True

  • D False

{

→ True

  • b tent out
  • n your
  • wn !
slide-4
SLIDE 4

Conditionals: in "pseudocode"

  • We want to write a program that:
  • asks the user for a number
  • if the number is larger than 1000, tell them that they picked a big

number

  • when the program finishes, displays a message telling the user that the

program is done

4

e,

if

2)

3)

slide-5
SLIDE 5

Conditionals: in "pseudocode"

  • We want to write a program that:
  • asks the user for a number
  • if the number is larger than 1000, tell them that they picked a big

number

  • otherwise, tells them that they did not pick a big number
  • when the program finishes, displays a message telling the user that the

program is done

5

iflela

:

3

4

slide-6
SLIDE 6

Conditionals: in "pseudocode"

6

ask the user for a number tell them that it's big tell them that the program has finished running tell them that it's not big

else

slide-7
SLIDE 7

Conditionals

  • We incorporate logical expressions into code with conditionals (also called "selection

statements")

  • Our first example of a conditional is an "if" statement
  • All conditionals evaluate logical expressions to a boolean value (True or False)
7

if logical expression: # code to run if the logical expression # evaluates to True # notice indent!

T

slide-8
SLIDE 8

Conditionals

  • If we want to run certain code in one case and other code in all other cases, we will you an "if/else"

statement

8

if logical expression: # code to run if the logical expression # evaluates to True # notice indent! else: # code to run if the logical expression # evaluates to False

O

slide-9
SLIDE 9

Conditionals

  • If we have more than two branches, we'll use an if/elif/else structure
9

if logical expression 1: # code to run if the logical expression 1 # evaluates to True # notice indent! elif logical expression 2: # code to run if the logical expression 1 evaluates to False # and logical expression 2 evaluates to True else: # code to run if the logical expression # evaluates to False

if

688

  • n:&

lot

slide-10
SLIDE 10

For Loops

  • All loops are used to repeat code. We may want to do the same task over

and over, and we may want to do a very similar task over and over.

  • Flip a coin 10 times
  • Remind your roommate to do the dishes once for each dish left in the

sink

  • Count from 1 to 100
10
slide-11
SLIDE 11

For loops: in "pseudocode"

  • We want to write a program that:
  • asks the user for their age
  • prints out "happy birthday" the same number of times as their age
  • when the program finishes, displays a message telling the user that the

program is done

11

H

2) 3)

slide-12
SLIDE 12

For loops: in "pseudocode"

12

ask the user for their age print out "happy birthday" tell them that the program has finished running

1)

2)

x the user's age

3)

slide-13
SLIDE 13

For Loops

  • For loops are used (as opposed to while loops) when we know ahead of

time how many times the loop needs to repeat:

13

for variable_name in something_we_can_loop_over: # code that we want repeated # code to run when we're done

times = 5 # I know now how many times to repeat for time_number in range(times): print("repetition!") print("all done!")

→ &

S

slide-14
SLIDE 14

For Loops

  • The number of times might be hardcoded, come from the user, be a

programmatically generated value, or be the length of a list (as a few examples)

14

times = int(input("times? ")) # I know now how many times to repeat for time_number in range(times): print("repetition!") print("all done!")

Eri

to investigate

: yhiatu.is

no

thebe!got

slide-15
SLIDE 15

For loops

15
  • How many times does the given for

loop run?

  • A. 10
  • B. 5
  • C. 2
  • D. -3

x = 10 y = -3 if x < 100 and y > 0: x = 5 elif x > 0 or y > 0: x = 2 for time_number in range(x): print("repetition!")

x D2

T

F

slide-16
SLIDE 16

Practicum today

  • You'll be working with random numbers today
  • This is how we can simulate a wide variety of coin, dice, and card games!
  • (you also won't know what value they have until you run your programs!)
  • Fun facts about random numbers:
  • Truly random numbers are surprisingly difficult for computers to generate
  • the random module in python generates "pseudorandom" numbers
  • It uses the current time of your computer as a starting "seed" number to

generate what appears to be a sequence of random numbers.

16
slide-17
SLIDE 17

Practicum today

  • We'll code up one of the problems together around 15 minutes before the

end of practicum today!

  • Those of you who are remote—you'll be in breakout rooms. Anuj, Satya

and myself will be rotating between the rooms.

  • Use the "ask for help" button if one of us isn't there and you need some

help

  • Please choose one person to screen share in your breakout room. You'll

all submit code individually, but we want you to discuss the problems and work together.

17
  • 1:10