lecture 8
play

Lecture 8: Optional 1 on 1 with a staff member to help just you - PDF document

No-laptop front Announcements http://www.cs.cornell.edu/courses/cs1110/2020sp zone on your left ok Lecture 8: Optional 1 on 1 with a staff member to help just you Conditionals & Control Flow with course material. Sign up for a


  1. No-laptop front Announcements http://www.cs.cornell.edu/courses/cs1110/2020sp zone on your left ok Lecture 8: • Optional 1 ‐ on ‐ 1 with a staff member to help just you Conditionals & Control Flow with course material. Sign up for a slot on CMS under (Sections 5.1 ‐ 5.7) “SPECIAL: one ‐ on ‐ ones“. CS 1110 • A1 first submission due Feb 19 Wedn at 11:59pm Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Fan, D. Gries, L. Lee, 2 S. Marschner, C. Van Loan, W. White] Swap (Question) What is in p.x at the end of this code? import shapes p = shapes.Point3(1,2,3) A: 1 q = shapes.Point3(3,4,5) B: 2 C: 3 def swap_x(p, q): D: I don’t know 1 t = p.x Review: Objects are referenced Heap ap Space ace 2 p.x = q.x 3 q.x = t id1 id2 ‐ Must call constructor function to create object swap_x(p, q) Point3 Point3 ‐ Object variable stores ID of object Global al Space ace x 1 x 3 ‐ Multiple variables can reference same object p id1 y 2 y 4 z 3 q id2 z 5 4 Global p (Question) Methods: Functions Tied to Classes import shapes • Method : function tied to object What is in global p after calling swap? p = shapes.Point3(1,2,3) u id3  Method call looks like a function q = shapes.Point3(3,4,5) A: id1 call preceded by a variable name: B: id2 def swap(p, q): ⟨ variable ⟩ . ⟨ method ⟩ ( ⟨ arguments ⟩ ) C: I don’t know id3 1 t = p Heap ap Space ace 2 p = q Example : Point3 4 x 3 q = t import shapes id1 id2 2 y swap(p, q) u = shapes.Point3(4,2,3) Point3 Point3 u.greet() Global al Space ace 3 z x 1 x 3 “Hi! I am a 3-dimensional point p id1 y 2 located at (4,2,3)” y 4 z 3 q id2 z 5 Where else have you seen this?? 7 10 1

  2. Example: String Methods Built-in Types vs. Classes • s 1 .upper() • s 1 .index(s 2 ) Built-in types Classes  Returns returns an upper case  Returns position of the first version of s 1 instance of s 2 in s 1 • Built-into Python • Provided by modules  error if s 2 is not in s 1 • s.strip() • Refer to instances as values • Refer to instances as objects • s 1 .count(s 2 )  Returns a copy of s with • Instantiate with literals • Instantiate w/ constructors white-space removed at ends  Returns number of times s 2 • Can ignore the folders • Must represent with folders appears inside of s 1 So far only about understanding objects ; later will create your own classes 11 12 Big Picture Big Picture Conditionals: Co nditionals: I If-Statements -Statements Format For Example Exampl Statements either affect data or control • DATA: change the value of a variable, create a # is there a new high score? if < boolean-expression >: variable, etc. < statement > if curr_score > high_score: if … Examples: high_score = curr_score < statement > print(“New high score!”) x = x + 1 name = “Alex” • CONTROL: tell python what line to execute next Execution : Examples: if ⟨ boolean-expression ⟩ is true, then execute all of the statements greet(name) indented directly underneath (until first non-indented statement) if name == “Alex”:  today’s Lecture 13 14 What are Bool What are Boolean expre expressions ons? What g What gets pr ts printed inted, Ro Round 1 und 1 Expressions that evaluate to a Boolean value. a = 0 a = 0 a = 0 a = 0 a = 0 print(a) a = a + 1 if a == 0: if a == 1: if a == 0: Boolean olean oper operati ations ns: is_student = True print(a) a = a + 1 a = a + 1 a = a + 1 is_senior = False if is_s is_student nt and and is is_sen _senior ior: print(a) print(a) a = a + 1 print(“Hi senior student!”) num_credits = 25 print(a) Boo Boolean var variables: Comp mparison arison o operations: erations: if is_st is_studen dent: if num_c num_credits dits > 24 24: print(“Hi student!”) print(“Are you serious?”) 15 16 2

  3. What g What gets pr ts printed inted? ( (Question) uestion) Conditionals: If-Else-Statements Format Example a = 0 if a == 0: # new record? if < boolean-expression >: A: 0 a = a + 1 < statement > if curr_score > high_score: if B: 1 … if a == 0: print(“New record!”) C: 2 else : a = a + 2 else: el D: 3 < statement > print(“Try again next time”) a = a + 1 E: I do not know … Execution : print(a) if ⟨ boolean-expression ⟩ is true, then execute statements indented under if ; otherwise execute the statements indented under else 18 20 Conditionals: Co nditionals: “Co “Control F trol Flow” S ” Statements ements What g What gets pr ts printed inted, Ro Round 2 und 2 Branch Point: b a = 0 a = 0 a = 0 a = 0 if b : True Evaluate & Choose if a == 0: if a == 1: if a == 1: if a == 1: Statements: False s1 # statement s1 Execute a = a + 1 a = a + 1 a = a + 1 a = a + 1 s3 # statement else: else: else: else: s3 a = a + 2 a = a + 2 a = a + 2 a = a + 1 if b : b a = a + 1 a = a + 1 Flow False s1 Program only print(a) print(a) print(a) a = a + 1 True takes one path else : s1 s2 print(a) during an execution s2 (something will s3 s3 not be executed!) 21 22 Pr Progr ogram F m Flow ( ow (car lo ar locked cked, 0) , 0) Pr Progr ogram F m Flow ( ow (car no ar not lo t locked cked, 0) , 0) if determines which statement is executed next if determines which statement is executed next if if Global al Space ace Global al Space ace def get_in_car(car_locked): def get_in_car(car_locked): 1 if car_locked: 1 if car_locked: 2 print(“Unlock car!”) 2 print(“Unlock car!”) print(“Open the door.”) print(“Open the door.”) 3 3 car_locked = True car_locked = False get_in_car(car_locked) get_in_car(car_locked) 24 30 3

  4. What does What does the the call frame call frame look look like next? (Q) like next? (Q) Progr Pr ogram F m Flow and Var ow and Variable ables Variables created inside if continue to exist past if : def max(x,y): if x > y: 1 a = 0 2 return x if a == 0: b = a + 1 3 return y print(b) max(0,3) Current call frame: max 1 …but are only created if the program actually executes that line of code x 0 y 3 35 41 Co Control F ntrol Flow a ow and V Variables ables ( (Q1) 1) Co Control F ntrol Flow a ow and V Variables ables ( (Q2) 2) def max(x,y): Value of maximum? def max(x,y): Value of maximum? """Returns: max of x, y""" """Returns: max of x, y""" A: 3 A: 3 # note: code has a bug! # note: code has a bug! B: 0 B: 0 # check if x is larger # check if x is larger C: Error! C: Error! if x > y: if x > y: D: I do not know D: I do not know bigger = x bigger = x return bigger return bigger maximum = max(3,0) maximum = max(0,3) 44 46 Progr Pr ogram F m Flow and Var ow and Variable ables Conditionals: If-Elif-Else-Statements def zero_or_one(a): Format Example if a == 1: if < Boolean expression >: # Find the winner make sure that ALL b = 1 < statement > if score1 > score2: if … if branches create if else: winner = “Player 1” elif < Boolean expression >: the variable elif score2 > score1: el < statement > b = 0 … winner = “Player 2” print(b) … el else: else : winner = “Players 1 and 2" < statement > … 48 49 4

  5. If-E -Elif-El lif-Else ( e (Ques uestion) ion) Conditionals: If-Elif-Else-Statements Format Notes on Use a = 2 What gets printed? • No limit on number of elif if < Boolean expression >: if a == 2: A: 2 < statement >  Must be between if , else a = 3 B: 3 … • else is optional elif a == 3: C: 4 elif < Boolean expression >:  if - elif by itself is fine a = 4 D: I do not know < statement > • Booleans checked in order … print(a)  Once Python finds a true … < Boolean-expression >, skips else : over all the others < statement >  else means all < Boolean- … expression > are false 50 51 What g What gets pr ts printed inted, Ro Round 3 und 3 Ne Nested Co ed Conditionals nditionals a = 2 a = 2 def what_to_wear(raining, freezing): if raining: if if a == 2: if a == 2: if if freezing: a = 3 a = 3 print(”Wear a waterproof coat.”) elif a == 3: el if if a == 3: else: a = 4 a = 4 print(”Bring an umbrella.") pr print(a) a) pr print(a) a) else: if freezing: print(”Wear a warm coat!") else: print(”A sweater will suffice.") 53 55 Pr Progr ogram F m Flow and Testing ow and Testing Can use print statements # Put max of x, y in z to examine program flow print('before if’) if if x > y: 'before if’ print(‘inside if x>y’) “traces” or z = x ‘inside if x>y‘ “breadcrumbs” 'after if' el else: x must have been greater print(‘inside else (x<=y)’) than y z = y print('after if’) 57 5

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