spis2022.github.io

SPIS 2022, FOCS, 08/05 (08:45-10AM)

Announcements

Overview of today’s calendar:

Today’s breadth lecture is by Niema, and discussses Bioinformatics

Today’s depth lecture is by Phill, and will provide a review and more practice on the Python skills and concepts we’ve already learned.

What is Iteration

Iteration is just a fancy computer science word for ‘repeated steps’

or “looping”

We “iterate” over something by repeating the same steps on it multiple times.

Each time we go through the process, that’s one “iteration”

We use this work iteration in connection with loops.

Iterating over strings and lists

We can use a for loop to iterate over many kinds of collections in Python.

We’ve already seen iterating over a list:

Repl

colleges = ["Revelle", "ERC", "Marshall", "Sixth", "Warren", "Muir"]

for college in colleges: 
  print(college)

Output:

Revelle
ERC
Marshall
Sixth
Warren
Muir

We can also iterate over the letters of a string:

Repl

campus = "UC San Diego"

for letter in campus:
  print(letter)

Output:

U
C
 
S
a
n
 
D
i
e
g
o

If we want to do this with a number, i.e. iterate over all of the digits of a number, we can convert it to a string:

number = 8675309

number_as_string = str(number)

for c in number_as_string:
  print(c)

Output:

8
6
7
5
3
0
9

Another example

number_as_string = str(number)
for c in number_as_string:
     print(c)

Result:

8
6
7
5
3
0
9

The code above treats each c as a character. But we can convert those back to numbers like this:

for c in number_as_string:
     new_number=int(c)
     print(new_number + 1)

Output:

9
7
8
6
4
1
10 

Data types


# list of integers
enrollments = [4766, 4683, 4451, 4665, 4702, 4860]

# list of mixed data types
mixed_list = ["UC San Diego", 3.14, True, ["Tritons","Sun God"], 28127 ]

# string (collection of characters)
campus = "UC San Diego"

# iterating over list of ints
for e in enrollments:
  print(e)
  

We can take the len (length) of lists and strings, but not int or float:

image

A few basic algorithms

There a few basic algorithms that are helpful to know

One of them is called the “accumulator pattern”, and it’s how you add things up.

Suppose you have a list of numbers. From this document I got the enrollments by college from 2016. (If someone can find more up to date data, let me know)

Here’s that data as a table:

College Enrollment
Revelle 4766
John Muir 4683
Thurgood Marshall 4451
Earl Warren 4665
Eleanor Roosevelt 4702
Sixth 4860

We can make a list of these numbers:

enrollments = [4766, 4683, 4451, 4665, 4702, 4860]

We then might want to add all of those up. According to the document, the total should be 28,127.

Full disclosure: there’s an easy way repl


enrollments = [4766, 4683, 4451, 4665, 4702, 4860]
total = sum(enrollments)
print(total)

Output:

28127

However, we want to show another way. Why?

Accumulator pattern for a sum

Repl

enrollments = [4766, 4683, 4451, 4665, 4702, 4860]

total = 0 # accumulator variable
for e in enrollments:
  total = total + e  # add up each enrollment
  print("this enrollment:", e, "total so far: ", total)

print("total enrollment = ", total)

Dictionary

In Python, a dictionary is a collection “key/value” pairs, inside curly braces, like this:

# Data from: https://blink.ucsd.edu/instructors/courses/enrollment/week3.html

ucsd_college_dictionary = {
  "Marshall": 5335,
  "Muir": 5181,
  "Revelle": 5431,
  "Roosevelt": 5220,
  "Sixth": 5235,
  "Warren": 5355,
  "Seventh": 1586
}


We can get a collection of the keys by using .keys()

We can get a collection of the values by using .values()

image

You don’t have to end the name with dictionary. A better practice is to name it something that makes it clear what is being “mapped to what”.

For example, if we are “mapping college name to enrollment”, we might call the dictionary:

image

List vs Tuples

# tuple
colleges = ("Marshall", "Muir", "Revelle", "Roosevelt", "Sixth", "Warren",
            "Seventh")

# list
schools = ["UCSD", "UCSB", "UC Irvine"]


# Tuples and lists have many things in common
# for loops work the same

Examples of things that are the same:

image

Lists are mutable.

image

But tuples are “immutable”. We cannot change them after they are defined.

We use tuples for things that we should not change once the program has started, that we want to define once and keep the same:

image

Why do we even have tuples if they are less capable than lists?

It’s a matter of keeping things tidy, and working for efficiency: