#!/usr/bin/env python # coding: utf-8 # # Welcome back # # How was lab? Was anything confusing? # In[ ]: if : elif : else: while : # list, string, dictionary, or range for : # # Review # # Last time, we covered: # - For loop # - Basic functions # # While loop syntax # # The syntax for while loops is: # ```python # while : # # ``` # # As with `if` statements, the `` can be anything that evaluates to a boolean. The `` can be complex. # In[28]: num = 0 while num < 4: print(num) num += 1 print("Done!") # In[ ]: a = 1 b = 1 num = 0 while num < 8: print(a) c = a + b a = b b = c num += 1 # print(a, b, c, num) # print() # # Basic syntax for `for` loops # # The basic syntax for `for` loops is # ```python # for in : # # ``` # # Don't worry too much about what an `` is for the time being. For this lecture, it will always be a list, string, dictionary, or `range`. We will cover `range` later. # # As with `if`s and `whiles`, the `` can be complex. # In[1]: for x in [10, 9]: # [10, 9] print(x) # In[2]: x = [1, 2, 3, 4, 5, 6] for y in x: # [x[0]] print(y) # In[3]: x = [4, 3, 8] sum_of_squares = 0 for y in x: sum_of_squares += y * y # 25 + 64 = 89 sum_of_squares # In[4]: y = "" z = "hello" for s in z: y += s # y = "hello" print(y) # In[19]: x = { "key": "value", 4: 10, } for key in x: # key = 4 print(x[key]) # In[6]: print(1, 4) # In[7]: for x in [1, 2, 3]: # x = 3 for y in [4, 5]: # y = 4 print(x, y) print("Done!") # 1 4 # 1 5 # 2 4 # 2 5 # 3 4 # 3 5 # Done! # In[8]: for x in range(3): # [0, 1, 2] print(x) # In[17]: # x = [0, 1, 2, 3, 4, ....] # x[0:3] -> [x[0], x[1], x[2]] -> [0, 1, 2] # x[3:5] -> [x[3], x[4]] -> [3, 4] # x[3:8:2] -> [x[3], x[5], x[7]] -> [3, 5, 7] list(range(3)) # [0, 1, 2] list(range(3, 8)) # [3, 4, 5, 6, 7] list(range(3, 8, 2)) # [3, 5, 7] # [0, 2, 4, 6] list(range(0, 7, 2)) # [0, 2, 4, ..., 100] n = 101 list(range(0, n, 2)) # [1, 3, 5, ..., 99] n = 100 list(range(1, n, 2)) # In[ ]: for x in [1, 2, 3]: y = 0 while y < x: print(y) y += 1 print() # In[ ]: for x in range(5): print(x) # In[22]: x = [4, 3, 2, 1] # len(x) = 4 # list(range(len(x))) # list(range(4)) # [0, 1, 2, 3] for ind in range(len(x)): # ind = 0 print(ind, x[ind]) # ind = 0, x[0] = 4 # In[23]: x = [1, 2, 3, 4] y = [5, 6, 7, 8] mul_sum = 0 # list(range(len(x))) # [0, 1, 2, 3] for ind in range(len(x)): # ind = 3 # x[3] -> 4 # y[3] -> 8 # x[3] * y[3] -> 32 # mul_sum = 38 + 32 = 70 mul_sum += x[ind] * y[ind] mul_sum # In[24]: x = [1, 4, 7, 9, 11, 15] # Compute the squared sum of the values # of x until we reach 7 squared_sum = 0 for y in x: # y = x[2] = 7 if y == 7: # (7 == 7) = True break # squared_sum = 1 + 16 = 17 squared_sum += y * y squared_sum # In[25]: # Breaks can come anywhere in the # body of a for loop for x in [1, 2, 3]: # x = 1 break print(x) print("Done!") # In[26]: x = [1, 4, 7, 9, 11, 15] # Compute the squared sum of # the _even_ values of x squared_sum = 0 for y in x: # y = x[1] = 4 if y % 2 == 1: # False continue # squared_sum = 0 + 4 * 4 = 16 squared_sum += y * y squared_sum # # Review redux: syntax for functions # # The general syntax for functions is: # ```python # def function_name(): # # return # ``` # # There are several things to note about the syntax (we will go through examples): # - You can have zero, one, or more arguments. # - You need the `:` after the function # - The body can be complex! # - You technically don't need a return statement at the end, but we will almost alway have a return at the end # In[ ]: def fahrenheit_to_celsius(f): # ??? return 0 # In[ ]: def celsius_to_fahrenheit(f): # ??? return 0 # In[29]: def add_two(x, y): # x = 6, y = 3 # add x and y z = x * y # z = 6 * 3 = 18 z += x # z = z + 6 = 18 + 6 = 24 z /= y # z = z / y = 24 / 3 = 8.0 return x + y # 9 # Calling a function a = add_two(6, 3) # 9 a # In[30]: def mystery(x, y): # x = 6, y = 3 return x + y x = 1 y = 2 return x + y a = mystery(6, 3) # 9 a # In[31]: def mystery(x, y): # x = 7, y = 4 if x % 2 == 0: return x + y else: return x - y mystery(7, 4) # In[32]: int("1") # # Using other concepts in functions # # We can use other concepts we learned within functions, including `if`s, `while`s, and `for`s. # In[33]: def minimum_two(x, y): # x = 1, y = 2 if x < y: # (1 < 2) -> True return x else: return y minimum_two(1, 2) # In[35]: def min_list(l): # l = [57, 391, 21, 458] minimum = l[0] # minimum = 57 # l[1:len(l)] = [391, 21, 458] for x in l[1:len(l)]: # x = 458 # minimum_two(21, 458) minimum = minimum_two(minimum, x) # minimum = 21 return minimum # min_list([6, 5, 4, 3, 2, 1]) min_list([57, 391, 21, 458]) # In[36]: def contains_pineapple(dictionary): if 'pineapple' in dictionary: return True else: return False x = { 'apple': 1, 'cherry': 2, 'pineapple': 3, } contains_pineapple(x) # In[38]: contains_pineapple(x) # # Reusing functions # # Once we define a function once, we can use it anywhere. We can: # - Use functions within expressions # - Call functions within functions # In[39]: if 'apple' in x: y = contains_pineapple(x) else: y = not contains_pineapple(x) y # In[42]: def minimum_two(x, y): if x < y: return x else: return y minimum_two(1, 2) # In[43]: # minimum_two(1, 2) minimum_two(1, minimum_two(2, 3)) # In[44]: def minimum_three(x, y, z): # x = 1, y = 2, z = 3 min_xy = minimum_two(x, y) # min_xy = 1 return minimum_two(min_xy, z) minimum_three(1, 2, 3) # # Variable scope # # So far (up until functions), we've used variables that can be used anywhere in Jupyter. With functions, we will start to use variables that can only be used in certain places. # # Where variables can be used is called _variable scope_. # In[ ]: def variable_scope_example(): scoped_variable = 0 variable_scope_example() scoped_variable # In[ ]: # This is called using a global variable. Avoid using global variables whenever possible! x = 0 def variable_scope_example_two(): print(x) variable_scope_example_two() # # Built in functions # # Python provides us _built in functions_, which are functions that we can use for free! # # We've already seen some examples: `type`, `int`, etc. # In[ ]: type(1) # In[ ]: int(True) # In[ ]: min(1, 2) # In[ ]: max(1, 2) # In[ ]: