Python Language Intro (Part 3)

Agenda

  1. Language overview
  2. White space sensitivity
  3. Basic Types and Operations
  4. Statements & Control Structures
  5. Functions
  6. OOP (Classes, Methods, etc.)
  7. Immutable Sequence Types (Strings, Ranges, Tuples)
  8. Mutable data structures: Lists, Sets, Dictionaries

8. Mutable data structures: Lists, Sets, Dicts

Lists

This list supports the mutable sequence operations in addition to the common sequence operations.

Mutable list operations

List comprehensions

Sets

A set is a data structure that represents an unordered collection of unique objects (like the mathematical set).

Dictionaries (AKA Maps or HashTables)

A dictionary is a data structure that contains a set of unique key → value mappings.

Dictionary comprehensions

Multiples of 3 or 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Complete the following function, which finds the sum of all the multiples of 3 or 5 below the argument n.

Simple ASCII Art

For this next exercise, you'll need to complete the function gen_pattern, which, when called with a string of length $\ge$ 1, will print an ASCII art pattern of concentric diamonds using those characters. The following are examples of patterns printed by the function (note the newline at the end of the last line!):

> gen_pattern('X')

X

> gen_pattern('XY')

..Y..
Y.X.Y
..Y..

> gen_pattern('WXYZ')

......Z......
....Z.Y.Z....
..Z.Y.X.Y.Z..
Z.Y.X.W.X.Y.Z
..Z.Y.X.Y.Z..
....Z.Y.Z....
......Z......

You ought to find the string join and center methods helpful in your implementation. They are demonstrated here:

> '*'.join('abcde')

'a*b*c*d*e'

> 'hello'.center(11, '*')

'***hello***'

Complete the gen_pattern function, below: