grapefruit print grapefruit c grapefruit else d
play

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


  1. ⇒ 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 ← A. orange elif x > 0: B. orange → grapefruit print("grapefruit") C. grapefruit else: - D. grapefruit lemon print("lemon") - E. lemon 1

  2. Logical Expressions • why "==" and not "=" to test for equality? X==5 5=-5 x Is - S - S x - s - - Yg yet - y 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 - assignment operator O D. "=" is has a di ff erent meaning in python code 2

  3. Logical Expressions • What are the results of evaluating the following logical expressions? - D True • 5 != 7 F T ) → False • 13.2 > 6.6 and 6.6 < 3 ) ( ( T F → True • 13.2 > 6.6 or 6.6 < 3 D False { • 'a' == 'apple' - - - → True • 'a' < 'g' own ! • 'A' < 'g' - b tent out on your 3

  4. Conditionals: in "pseudocode" ⑦ if • We want to write a program that: e , • asks the user for a number • if the number is larger than 1000, tell them that they picked a big 2) number 3) • when the program finishes, displays a message telling the user that the program is done 4

  5. Conditionals: in "pseudocode" if lela • 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 3 4 • when the program finishes, displays a message telling the user that the program is done 5

  6. Conditionals: in "pseudocode" ask the user for a number else tell them that it's big tell them that it's not big tell them that the program has finished running 6

  7. Conditionals • We incorporate logical expressions into code with conditionals (also called "selection statements") • Our first example of a conditional is an "if" statement if logical expression: T # code to run if the logical expression # evaluates to True # notice indent! • All conditionals evaluate logical expressions to a boolean value ( True or False ) 7

  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 if logical expression: O # code to run if the logical expression # evaluates to True # notice indent! else: # code to run if the logical expression # evaluates to False 8

  9. Conditionals • If we have more than two branches, we'll use an if/elif/else structure if 688 if logical expression 1: # code to run if the logical expression 1 # evaluates to True lot # notice indent! on :& elif logical expression 2: # code to run if the logical expression 1 evaluates to False # and logical expression 2 evaluates to True 0¥ else: # code to run if the logical expression # evaluates to False 9

  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

  11. For loops: in "pseudocode" • We want to write a program that: H • asks the user for their age 2) • prints out "happy birthday" the same number of times as their age 3) • when the program finishes, displays a message telling the user that the program is done 11

  12. For loops: in "pseudocode" 1) ask the user for their age print out "happy birthday" 2) x the user 's age 3) tell them that the program has finished running 12

  13. For Loops for variable_name in something_we_can_loop_over: → # code that we want repeated # code to run when we're done • For loops are used (as opposed to while loops) when we know ahead of time how many times the loop needs to repeat: times = 5 # I know now how many times to repeat for time_number in range(times): & print("repetition!") S print("all done!") 13

  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) times = int(input("times? ")) # I know now how many times to repeat for time_number in range(times): Eri print("repetition!") print("all done!") : yhiatu.is to investigate thebe ! got no 14

  15. For loops • How many times does the given for x D2 x = 10 loop run? y = -3 F T if x < 100 and y > 0: A. 10 x = 5 B. 5 elif x > 0 or y > 0: C. 2 x = 2 D. -3 → for time_number in range(x): print("repetition!") 15

  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 di ffi cult 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

  17. Practicum today • We'll code up one of the problems together around 15 minutes before the - 1 : 10 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend