Fall 2018

# 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 :

• strings
• strings as sequences of characters (letters) with an index 0,1,2,3,...
• substrings with indeces word[4] , word[3:7]
• string operations : + , *
• ASCII and the ord() and chr() built-in functions
• many string methods ; dir("hello")
• string formatting : "The answer is {:0.3}".format(1.23998)
• lists
• sublists, indeces, mutable
• methods e.g. numbers.append(3)
• files
• open, close, read, write ... similar to input() and print()

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