160 likes | 173 Views
Learn how to make decisions in Python using if, elif, and else statements, and how to repeat code using while and for loops. Understand the concepts of indentation and statement suites.
E N D
Topics to cover • Decisions: Decision is how programs make choices • if , elif, and else • Repetition: Repeat until a condition is met • while loop • for loop (optional) condition ends all headers key condition : Keyword Statement “suite” of statements Indentation Statement
Decisions with if grade = 3.5 if grade >= 1.0: print(“Passed”) #What gets printed? Passed
Decisions with if grade = 0.5 if grade >= 1.0: print(“Passed”) #What gets printed? #nothing
Decisions with if/else grade = 3.5 if grade >= 1.0: print(“Passed :) ”) else: print(“Not Passed :’( ”) #What gets printed? Passed :)
Decisions with if/else grade = 0.5 if grade >= 1.0: print(“Passed :) ”) else: print(“Not Passed :’( ”) #What gets printed? Not Passed :’(
Decisions with if/elif/else grade = 0.5 if grade >= 3.0: print(“Excellent”) elif grade >= 2.0: print(“Good”) else: print(“Not so good”) #What gets printed? Not so good
Decisions with if/elif/else #What gets printed? grade = 3.5 if grade >= 3.0: print(“Excellent”) elif grade >= 2.0: print(“Good”) else: print(“Not so good”) Excellent ...but why not “Good” as well, since 3.5 >= 2.0?
Decisions with if/elif/else grade = 2.5 if grade >= 3.0: print(“Excellent”) elif grade >= 2.0: print(“Good”) else: print(“Not so good”) #What gets printed? Good
Repetition with while answer = input(“Please say Hi:”) while answer != “Hi”: print(“You do not want to say Hi ?”) answer = input(“Let’s try again… Say Hi:”) print(“Cool, let’s continue then.”) • >Please say Hi: no • >You do not want to say Hi ? • >Let’s try again… Say Hi: no • >You do not want to say Hi ? • >Let’s try again… Say Hi: Hi • >Cool, let’s continue then.
Repetition with for n = 3 for i in range(n): print(i) #What gets printed? 0 1 2 why not 3?
Repetition with for for i in range(2,5): print(i) #What gets printed? 2 3 4 why start at 2?
Repetition with for for i in range(1,9,2): print(i) #What gets printed? 1 3 5 7 why only odds?
Should I use while or for for i in range(5): print(“hello”) for: use this when you know how many times you want to iterate something e.g., to print “hello” 5 times while: when you want to continue looping until some condition is met when we do not know how many times we need to loop e.g.,continue program until user inputs “Hi” answer = input() while answer != “Hi”: answer = input()
Break vs Continue statement #What gets printed? for letter in 'Python': if letter == 'h': break print('Current Letter :', letter) Current Letter : P Current Letter : y Current Letter : t for letter in 'Python': if letter == 'h': continue print('Current Letter :', letter) Current Letter : P Current Letter : y Current Letter : t Current Letter : o Current Letter : n
Break vs continue The break statement in Python terminates the current loop and resumes execution at the next statement, The continue statement in Python returns the control to the beginning of the while loop.