#!/usr/bin/env python # coding: utf-8 # # Review of control flow / basic for loops # In[1]: # Fill in based off of confusion from yesterday # # Advanced for loop keywords # In[6]: # This is a reminder of what a loop looks like for i in range(20): print(i) print('Loop completed!') # In[7]: # There are two keywords for loops that you will need to know # The first is "break" # Notice "break" is in green # Break will exit a for loop once it is executed for i in range(20): if i == 10: break print(i) print('Loop completed!') # In[8]: # The other keyword you need to know is "continue" # "continue" will make it so that the rest of the loop body is not executed # but ONLY for the time it is called # Notice that "10" is not printed for i in range(20): if i == 10: continue print(i) print('Loop completed!') # In[9]: # You can have continue and break in the same loop for i in range(20): if i == 10: continue if i == 15: break print(i) print('Loop completed!') # # While loops # In[ ]: # This is the generic syntax for a while loop while CONTIDION: LOOP_BODY # In[10]: # A "while loop" is a loop that executes while CONDITION is true # The CONDITION is evaluated once the loop body finishes, and only then i = 0 while i < 10: print(i) i += 1 print('Loop completed!') # In[1]: # If CONDITION is False at the start of the loop, the loop body will not execute! i = 0 while i < 0: print(i) i += 1 print('Loop completed!') # In[12]: # What is different about this and the one two above? i = 0 while i < 10: i += 1 print(i) print('Loop completed!') # In[14]: # WARNING!!!! # You can have loops that run forever. # These are really really bad. # Make sure the loop terminates. while True: print('hi!') # In[15]: # WARNING!!!!! # Even if you don't mean to, the loop can run forever. # DO NOT do this. i = 0 while i < 10: print(i) print('Loop completed!') # This will never execute! # In[18]: # You can use "continue" in while loops # The syntax and behavior is exactly the same as in for loop i = 0 while i < 10: i += 1 if i == 5: continue print(i) print('Loop completed!') # In[19]: # WARNING!!!! # This loop will also run forever. Why? # Don't do this. i = 0 while i < 10: if i == 5: continue print(i) i += 1 print('Loop completed!') # In[20]: # You can also use "break" in while loops # The syntax and usage is exactly the same as in for loops i = 0 while True: if i > 10: break print(i) i += 1 # In[21]: # What will happen? i = 0 while True: i += 1 if i > 10: break print(i) # # Functions (variable scoping) # In[2]: # WARNING!!! # Variables are "scoped" # This is a complex topic, and we won't have time to cover all of it in lecture # We will only cover the basics: variables inside a function # Variables inside a function have no relation to variables outside the function # See what happens when "i" is printed inside and outside the function i = 0 def func(x): i = 10 print(i, x) func(5) print(i) # In[23]: # Variables across functions have no relation to each other # A variable declared inside a function will have no relation to outside the function # Why is there an error? def func1(x): j = 10 print(j, x) def func2(x): j = 5 func1(x) print(x) func2(3) print(j) # In[24]: # WARNING: Indentation is not the same scoping # Scoping only applies to functions # If blocks, for loops, and while loops ARE NOT functions i = 0 for i in range(3): print(i) # In[25]: # Why is this the output? k = 0 for i in range(3): print(k) # In[26]: # How about here? k = 0 for i in range(3): k = 6 print(k) # In[ ]: