Intro
Programming
with
Python

Spring 2018
course
site

Feb 21

We worked through several examples of functions, talked about global & local variables, print vs return. I've attached the code snips that we looked at in class, including the (very tricky and outside the depth of an intro class) idea of "closures".

For a good time, run "closure.py" with the online "Visualize Python" tool.

From the 2016 class, see these notes (which we went over) and these other notes (which we didn't).

"""
 A class example of functions:
 the "happy birthday song"
"""

first_line = "hi there"

def happy_birthday(name):
  """ Input:  a person's name.
      Output: the happy birthday song as a string. """
  first_line = "Happy Birthday to you"
  song = first_line + ",\n" + \
         first_line + ",\n" + \
         "Happy Birthday dear " + name + ",\n" + \
         first_line + "!\n"
  return song

def main():
  print happy_birthday("Peter")
  print
  print happy_birthday("Mary")
  print
  print "first_line is " + first_line

main()
https://cs.marlboro.college /cours /spring2018 /python /notes /feb21
last modified Fri March 29 2024 3:53 pm

attachments [paper clip]

  last modified size
TXT bad.py Fri Mar 29 2024 03:53 pm 233B
TXT closure.py Fri Mar 29 2024 03:53 pm 165B
TXT one.py Fri Mar 29 2024 03:53 pm 405B
TXT returns.py Fri Mar 29 2024 03:53 pm 197B