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 December 22 2024 4:57 am

attachments [paper clip]

  last modified size
TXT bad.py Sun Dec 22 2024 04:57 am 233B
TXT closure.py Sun Dec 22 2024 04:57 am 165B
TXT one.py Sun Dec 22 2024 04:57 am 405B
TXT returns.py Sun Dec 22 2024 04:57 am 197B