""" homework_examples.py some of the things I'm seeing in the homework ... """ # 1 ------------- # The "i for i in x" is a part of a "list comprehension" # ... which in this case is just giving us a copy of the list x. # So the work here is in sum(). def f(x): return sum(i for i in x) print(f([1,2,3,4])) # 2 -------------------- # For functions that don't return anything # but instead modify an object (like a list) in place, # make sure that you clearly document the behavior. def square_list(the_list): """ modify the_list in place by squaring each element """ for i in range(len(y)): y[i] = y[i]**2 stuff = [1,2,3,4] g(stuff) # 3 ---------------------- # Avoid using the same variable name in several different # scopes - that can lead to confusion. Here we have "nums" # in three different scopes: file-global, h-local, doctest-global. nums = [1,2,3,4] def h(nums): """ ... >>> nums = [10,11,12] >>> result = h(nums) >>> result 10 """ return sum(nums) print(h(nums))