Intro to
Programming
(with Python)

Fall 2019
course
site

Tue Oct 1

Questions on anything?

We'll spend the first part of class going over the homework due today : practice with strings, lists, and files. I've posted my example code answers and will go over them in class.

Upcoming schedule: no class Thursday ... so the next homework, on functions, will be due Thursday next week rather than Tuesday.

aside: a code smell I'm seeing this week

point1 = Point(10, 10, 5)  # SMELLY : sequential numbered variable names
point2 = Point(20, 20, 5)  #
point3 = Point(30, 30, 5)  #
point4 = Point(40, 40, 5)  #
point1.draw(window)

Instead use a list. Access one of 'em with an index, or loop over all of 'em.

points = [Point(10, 10, 5),  # BETTER : define them in a list, or one at a time with .append().
          Point(20, 20, 5),
          # and so on
         ]
points[0].setFill('red')   # change one of them
for p in points:           # loop over all of them
    p.draw(window)

chap 5 - functions

This is IMHO some of the most important material of the term.

And I want to add a bit more stuff : doc strings & doc tests.

Topics to discuss :

We'll look at lots of examples in class ... and with the online "visualize python" tool.


# An optional argument with a default value. 
def scale(x, multiplier=2):
    """ Return x * multiplier """   #  <= doc string
    return multiplier * x
print("twice 10 is {}".format( scale(10) ))
print("triple 10 is {}".format( scale(10, 3) ))
# Is x modified ?
def double(y):
    y = 2*y
    return y
x = 10
double_x = double(x)
print("x is ", x)
print("double_x is ", double_x)
# Is numbers modified ?
def double_all(them):
  for i in range(len(them)):
      them[i] = 2 * them[i]
  return them
numbers = [10, 11, 12]
double_numbers = double_all(numbers)
print("numbers is ", numbers)
print("double_numbers is ", double_numbers)

Hmmm. Run these in pythontutor.com to see exactly what's going on ...


In class I showed the attached example.py doctest thingy ...

https://cs.marlboro.college /cours /fall2019 /python /notes /chap6
last modified Thu November 21 2024 3:50 pm

attachments [paper clip]

  last modified size
TXT example.py Thu Nov 21 2024 03:50 pm 443B