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 Sun April 28 2024 10:54 pm

attachments [paper clip]

  last modified size
TXT bad.py Sun Apr 28 2024 10:54 pm 233B
TXT closure.py Sun Apr 28 2024 10:54 pm 165B
TXT one.py Sun Apr 28 2024 10:54 pm 405B
TXT returns.py Sun Apr 28 2024 10:54 pm 197B