Introduction: Lists and strings 🧵

Finally something other than numbers!

So far all of your programs have had a mathematical, or at least arithmetic, feel to them. That's because the main data type we've had to work with has been numbers, and single numbers at that. This week we will look at compound datatypes that let us represent words and lists of things. Together they vastly expand the types of things we can represent, and thus work with, in our programs. We will work with code like,

grades = [78, 91, 84] # a list containing three numbers
name = 'Tim Topper' # a string, which is a sequence of characters
names = ['Tim', 'Anne', 'Mary', 'Geoff'] # a list containing 4 strings

We cover lists and strings in the same week because they are both sequence types that share a lot of common operations.

Lists and strings are also both object types and to work with them you will have to get comfortable with some new-ish notation, and a bit of jargon that goes with it. The notation isn't that hard to follow, for example,

>>> s = 'Topper'
>>> s.count('p')
2
>>> s.endswith('er')
True
>>> s.upper()
'TOPPER'
>>>

This notation features the name of an object s, a period ., the method to perform count, and then any information the method requires in parentheses ('p'): s.count('p'). (This notation is not entirely new, e,g, we have seen and used random.randint(1,6).)

The list and string object types support a lot of methods. Some are shown in the resources and the complete lists of them in the official Python documentation are linked to. To prepare for the assignment problems you will want to familiarize yourself with the operations available to you (so you don't reinvent the wheel!), and get a little practice using them in the Python shell, like the excerpt above.

I hope you find lists and strings interesting!

  1. Object-based programming
  2. Sequence Types
  3. What can you do with a list?
  4. What can you do with a string?
  5. Example: CD Shuffle
  6. Dice Odds
  7. Bar Graphs
  8. Representing Playing Cards
  9. Poker hands
  10. Playing Cards: An alternative representation
  11. What is this [ [ 'X', 'O', '' ], [ 'O', 'X', 'O' ], [ '', '', 'X'] ] ?
  12. Palindrome
  13. Built-in Type Conversions
  14. The map() function
  15. Tuples
  16. Exercise 4
  17. Assignment 5