1 of 45

CS10: The Beauty and Joy of Computing - Su22

Lecture 13: Python II: Data Structures

  • Object Oriented Programming Preview
  • Lists
  • List Comprehensions
  • Other Python Sequences: Lists, Strings, Tuples, and Ranges
  • Sets and Dictionaries

2 of 45

Announcements

  • HW3 was due yesterday- use slip days if you need!
  • Starting python!
    • Python lab 1 was released today and is due monday
    • HW4 will be released later today and due Tuesday 7/19
    • Heavier lectures/labs will be a bit more spaced out moving forward
  • If you are feeling sick at all, please don’t come to class!
    • Stay home to recover, and email cs10@berkeley.edu for instructions to make up attendance points
  • Late add deadline for assignments is this upcoming tuesday 7/19
  • Midterm scores released!

3 of 45

Midterm Scores (graded out of 140 points)

  • Mean: 88.47 → ~63%
  • Median: 87.0
  • Standard Deviation: 29.58

4 of 45

Midterm Scores

  • Grading
    • The exam was graded out of 140 points
    • Your midterm category of your grade is worth 80 points
      • 20 points from in-lab midterm
      • 60 points from paper midterm
  • A couple notes
    • Don’t panic! Your score is not the sole reflection of all the work you have done this summer
    • The exam was hard and we are proud of you for doing as well as you did
    • There are still lots of points to account for when determining your grade
    • Clobber policy- final will replace midterm
    • You can earn 10 points back on your exam score by completing the midterm redo assignment
    • Appointment-based instructor office hours to discuss exam/study strategies for next time- sign-ups will be posted on Ed

5 of 45

Object Oriented Programming

6 of 45

Code Translations (finishing up last time)

Concepts that don’t translate well between Snap! and Python:

  • Slice notation.
  • Explicit variable types.
  • Object oriented programming.
    • Adding to a list.
    • String operations.

7 of 45

Object Oriented Programming

Recall from the programming paradigms lecture the definition of object-oriented-programming:

  • In object programming, we organize our thinking around objects, each containing its own data, and each with its own procedures that can be invoked.

In Snap! we may have had sprites representing objects interacting with each other

  • In Python every variable has a type which we can think of as an object
  • Each type has its own set of functions that it can perform
  • We’ll talk a lot more about this in Tuesday’s lecture

8 of 45

Object Oriented Programming- dot notation

The syntax for adding an item to a list is quite different from Snap!

  • Instead of calling a function that acts on the list, we instead tell the list to add them using one of its abilities.
    • Example below: Among many other skills, Python lists have the ability to “append” items to themselves.
  • Uses special “dot notation”.
    • Simple but powerful idea that is ubiquitous in text based languages.

some_list = [2, 4, 7, 11]

some_list.append("ketchup")

9 of 45

Object Oriented Programming

This is the Object Oriented Programming paradigm!

  • Functions in Python can “belong” to objects.
  • Later, we’ll see that data can also belong to objects.

some_list = [2, 4, 7, 11]

some_list.append("ketchup")

ketchup

2 4 7 11

ketchup

The list itself does the work! (See 61A)

add __ to __

10 of 45

Types and Functions

Each type has its own special set of functions.

Examples:

https://docs.python.org/3/library/stdtypes.html#str

11 of 45

Example of a String Skill

numbers_text = input("Gimme: ")

numbers_list = numbers_text.split(" ")

print(numbers_list)

$ python3 split_user_numbers.py

Gimme: 1 2 3

[‘1’, ‘2’, ‘3’]

12 of 45

Many String Functions Don’t Exist in Snap!

print("cat".upper())

print("abracadabra".count("a"))

print("cool boot".replace("oo", "el"))

$ python3 string_examples.py

CAT

5

cell belt

13 of 45

Even More String Functions

Join is a particular interesting string ability.

  • Takes a list of strings and combines them using itself.
  • Example above: The string ‘, ‘ does the joining.

', '.join(['apple', 'banana', 'cow'])

‘, ‘

['apple', 'banana', 'cow']

14 of 45

List Basics

15 of 45

List Basics

  • We make a list in Python by putting the items in square brackets and separating them with commas
    • We can save them into a variable by setting the list equal to its name

  • We can always get the length of the list by calling the len function

  • We can access values in our list by indexing into the list using square brackets

some_list = [0, 10, 20, 30, 40, 50]

some_list[0] → 0

some_list[4] → 40

some_list[len(some_list)-1] → 50

some_list[-1] → 50

len(some_list) → 6

16 of 45

List Basics

  • We can slice our list to grab a subset of items in our list by using square brackets with a colon between the start and end indices
    • The left side is inclusive and the right side is exclusive

    • If you leave on of the endpoints off, by default it will go to the end of the list, depending on which side is left off

  • You can change the value of an item in a list by accessing that item using indexing and then re-assigning it to the new value

some_list = [0, 10, 20, 30, 40, 50]

middle_items = some_list[1:5]

print(middle_items)

some_list = [0, 10, 20, 30, 40, 50]

some_list[1:5] → [10, 20, 30, 40]

some_list[:3] → [0, 10, 20]

some_list[2:] → [20, 30, 40, 50]

some_list[:] → [0, 10, 20, 30, 40, 50]

some_list[3] = 35

print(some_list) → [0, 10, 20, 35, 40, 50]

17 of 45

List Basics

  • There are many other built-in functions in python that can be used to change a list
  • These mostly use dot notation and you can find a list of them in the python documentation here: Data Structures — Python 3.10.5 documentation
  • We do not expect you to memorize all of these
  • You should be familiar with the ones you use often for lab/other assignments
  • Most of these functions will actually mutate or change the list itself rather than return a new list
    • Refer to documentation to confirm

some_list = [0, 10, 20, 30, 40, 50]

middle_items = some_list[1:5]

print(middle_items)

some_list.pop()

some_list.append(4)

some_list.remove(3)

18 of 45

List Comprehensions

19 of 45

Defining Functions Review

Recall from yesterday’s lecture

def is_even(x):

if x % 2 == 0:

return True

else:

return False

20 of 45

In Snap!

As mentioned in the previous lecture, Python also has a map function:

This is the equivalent of the Snap! program below:

def reverse(s):

return s[::-1]

L = ["i", "am", "josh", "hug"]

map(reverse, L)

reverse is a function which reverses a string. The exact details of how it does this are not important.

21 of 45

In Snap!

As mentioned in the previous lecture, Python also has a map function:

One annoyance is that the map function in Python returns a “map object”, not a list. If we want our answer to be of type list, we have to convert it into a list as follows:

def reverse(s):

return s[::-1]

L = ["i", "am", "josh", "hug"]

map(reverse, L)

reverse is a function which reverses a string. The exact details of how it does this are not important.

def reverse(s):

return s[::-1]

L = ["i", "am", "josh", "hug"]

Lr = list(map(reverse, L))

22 of 45

List Comprehensions

Few people use “map” today. Instead, it is much more popular to use a list comprehension.

  • All three of the programs below are equivalent.
  • Note: A list comprehension is NOT A FOR LOOP. It uses a special syntax that resembles a for loop, but it is entirely distinct.

Lr = [reverse(x) for x in L]

Lr = list(map(reverse, L))

List Comprehension

Map Invocation

23 of 45

List Comprehensions

The syntax for a list comprehension (as we’ve seen it so far) has 3 components:

  • Square brackets surrounding the list comprehension.
  • for some_var in some_list
    • some_var is the “temporary” variable representing each item in some_list
  • Can apply arbitrary functions to some_var

Lr = [reverse(x) for x in L]

Lr = list(map(reverse, L))

List Comprehension

Map Invocation

some_var

some_list

24 of 45

Map and Filter

Python also has a version of keep, called filter, which can of course be combined with map.

def reverse(s):

return s[::-1]

def longer_than_2(s):

return len(s) > 2

L = ["i", "am", "josh", "hug"]

map(reverse, filter(longer_than_2, L))

25 of 45

List Comprehensions That Also Filter

Can also create a list comprehension which filters out unwanted items (similar to keep).

Note: As before, the syntax inside a list comprehension is very special. The if here is NOT a normal conditional. Don’t try to read it like normal python code!

def reverse(s):

return s[::-1]

Lr = [reverse(x) for x in L if len(x) > 2]

26 of 45

List Comprehension Summary

map

[f(element) for element in iterable]

keep

[element for element in iterable if cond(element)]

map+keep

[f(element) for element in iterable if cond(element)]

[f(element) if cond(element) else other for element in iterable]

27 of 45

Idiomatic Python

While equivalent, most Python programmers use list comprehensions instead of explicit HoFs like map and filter.

  • There is no direct translation of a list comprehension in Snap!
  • idiom: “a style or form of expression that is characteristic of a particular person, type of art, etc.”
  • Related to idioms in spoken language:
    • “To set the world on fire”: To surprise people with how good something is.
    • “一鸣惊人”: To surprise people with how good something is.

When programming, you should strive to adopt the idioms of the language you’re using.

28 of 45

Python Sequences: Lists, Strings, Tuples, Ranges

(and more)

29 of 45

Strings in Python

Similar to a text in Snap!, Python allows us to create strings. Three ways to specify a String:

  • Single quotes and double quotes (same thing)
  • Triple quotes: Preserves formatting

Can access characters using bracket notation, just like a list.

Strings are immutable- we cannot change/edit them

s1 = 'hello bob'

s2 = "hello alice"

s3 = """this

is a string

on many lines"""

print(s2[1:4])

s2[0] = 'y'

ell

TypeError:

'str' object does not support item assignment

30 of 45

String Functions

As mentioned before, there are many handy functions for working with strings.

  • Most (but not all) are functions of the strings themselves.

"this is a sentence".split()

len('kaimuki')

"the biggest pig".title()

“The Biggest Pig”

31 of 45

Python Lists vs. Strings

Lists and Strings are both sequences.

  • Support slice notation (e.g. x[2:5])
  • Support the in keyword.
  • Can be iterated over using a for loop or list comprehension.
  • Items can be sorted using sorted keyword (more later).
  • And more...

��

See official Python documentation on sequences for more.

if "dog" in x[2:10]:

print("woof")

Works if x is any kind of sequence, e.g. string or list.

32 of 45

Ranges

We saw in lab that we can iterate over a range of numbers:

A range is just an object like any other.

  • Also a sequence (so can do all the sequence stuff)
  • Not quite a list: Actual values don’t exist until needed (saves time and memory).
  • Like strings, ranges are immutable!
  • Let’s try playing around with a range object...

total = 0

for x in range(0, 4):

total = total + x

print(total)

6

33 of 45

Converting Between Types

As we saw last time, Python is picky about types.

number = 5

text = “6”

print(number + text)

number = 5

text = “6”

print(number + int(text))

11

34 of 45

Converting a Range to a List

Can convert any sequence into a list using the list function:

x = range(0, 4)

x.append(4)

listX = list(x)

listX.append(4)

works fine

35 of 45

Tuples

Tuples are almost exactly like lists except:

  • You create them using comma separated lists inside parentheses rather than square brackets
  • They are immutable.
  • You can access values at specific indices with square brackets just like with lists, you just can’t change the values

Question: What good is a less powerful list?

some_tuple = (1, 5, 10, 4, 7, 16, 2)

some_list = [1, 5, 10, 4, 7, 16, 2]

36 of 45

Programming Language Design and Good Coding Style

Modern programming languages are designed to be beautiful, expressive, and simple.

Keeping things simple means restricting the space of things the programmer needs to think about. Examples:

  • Use inputs and return values instead of global variables.
  • Use immutable sequences instead of mutable sequences.

37 of 45

Sets and Dictionaries

38 of 45

Sets

Sets are kind of like lists or tuples, except:

  • Only one instance of any item may be present in the set.
  • Sets are not sequences (cannot use bracket notation to access items).
  • No magic symbols for creating a list. Instead, use set function to convert a sequence into a set.
  • Support normal set operations (union, difference, etc.)

set_one = set([1, 3, 5])

set_two = set([3, 4, 5, 6, 7])

set_one = set_one.union(set_two)

print(set_one)

39 of 45

Counting Unique Items Using a Set

One trick you can do with a set is count unique items.

words = ["Can","you","can","a","can","as","a","canner","can","can","a","can?"]

unique_words = set(words)

print(unique_words)

print(len(unique_words))

40 of 45

Dictionaries

Can think of dictionaries as a more powerful kind of list.

  • Instead of just using numbers to index items, can use ANY immutable value.
  • Create a dictionary using the magic symbol {} (curly braces).
  • Incredibly powerful and useful data structure (as you’ll see in this week’s labs).

  • Dictionaries give us a way to store data in key-value pairs

phonebook = {}

phonebook["Josh"] = "713-474-2731"

phonebook["Board X-5"] = "713-474-3750"

phonebook["Tom Bates"] = "510-981-7100"

print(phonebook["Tom Bates"])

41 of 45

Dictionaries

  • Unlike lists, dictionaries do not have any pre-defined order
  • We should just think of dictionaries as a collection of un-ordered key-value pairs
    • The key points us to the value
  • We can have repeated values but not repeated keys
    • Values can be mutable but keys must be immutable

phonebook = {}

phonebook["Alonzo"] = "713-474-2731"

phonebook["Oznola"] = "713-474-3750"

phonebook["Tom Bates"] = "510-981-7100"

print(phonebook["Tom Bates"])

42 of 45

Dictionaries

Some handy dictionary functions:

  • .keys(): Returns a sequence of the keys in a dictionary
    • In no particular order.
  • .values(): Returns a sequence of the values in a dictionary.
    • In no particular order.
  • .get(): Alternative to bracket notation for retrieval.

numerals = {'I': 1, 'V': 5, 'X': 10}

print(list(numerals.keys()))

print(list(numerals.values()))

43 of 45

Dictionary Syntax Summary

  • Dictionary Creation: D1 = {“key”: “value”, 1: “one”}
  • Assign keys to values: D1[“hello”] = “world”
  • Access a value:

> D1[1]

“One”

dict.get(key, value=None)

> D1.get(2, 0) # 2 not a key in D1, returns given value

0

> D1.get(2) # returns nothing or errors

  • Updating a value is the same as just assigning it to a different value
    • You are just changing the value that the key is pointing to

D1[“hello”] = “Alonzo”

44 of 45

Iterating through a Dictionary

  • Iterate through keys

for k in my_dictionary.keys():

for k in my_dictionary:

  • Iterate through values

for v in my_dictionary.values():

  • Iterate through keys and values

for k,v in my_dictionary.items():

  • Check if k is a key in dictionary

k in my_dictionary.keys()

k in my_dictionary

  • Check if v is a value in dictionary

v in my_dictionary.values()

45 of 45

Summary

Today we saw a lot of things:

  • Dot notation for using functions that belong to data types.
    • Example: “hello”.upper()
  • List comprehensions.
    • The idiomatic way of doing map/keep.
    • Code that obeys Python idioms is said to be ”Pythonic”.
  • Sequences: Lists, Strings, Tuples, Ranges.
    • Tuples are almost exactly like lists, but cannot be modified.
    • Immutability makes it easier to reason about code.
  • Sets and Dictionaries
  • Lists and dictionaries will be the most relevant data structures for CS10