Intro to
Programming
(with Python)

Fall 2018
course
site

Tue Sep 18

Graphics homework ... how did it go?

My examples are in code/art.

Aside : mod arithmetic in python i.e. time = (17 + 10) % 24

chapter 5 - strings, lists, files

This week we're doing chapter 5. The assignment for next Tuesday is posted.

There's a lot of material in this one - I encourage you to get started early.

I'll introduce the ideas today, and we'll continue the discussion Thursday after you've had a chance to read it in the text.

Here's a summary of the topics to cover :

Rather than post detailed notes here, I'll either wing it or pull up the text's powerpoint slides for chap5 .

Be clear that some methods modify objects in place, while others return a new object.

>>> numbers = [10, 15, 12]   
>>> numbers.append(20)       # modify in place; nothing returned
>>> numbers
[10, 15, 12, 20]

>>> word = "cat"
>>> word.upper()             # new uppercase word returned
'CAT'
>>> word                     # original unchanged
'cat'

Also notice how we can still use the accumulator patter to build up a result.

# reverse a word using the accumulator pattern
# to build it up letter by letter.
word = 'cat'
result = ''            # empty string to store result
for letter in word:
    # build reversed word letter by letter
    result = reversed_word + letter  # put each letter on the right
print(result)   # 'tac'

This works too : ''.join(list('cat')[::-1]) ... see if you can figure out why.

https://cs.marlboro.college /cours /fall2018 /python /notes /chap5
last modified Thu November 21 2024 12:08 pm