Tuples

Tuples are like lists, but are immutable, meaning that once you've made them they can't be changed.

Predict what this code will do.


In [ ]:
some_tuple = (10,20,30)
print(some_tuple[1])

In [ ]:
some_tuple = (10,20,30)
print(some_tuple[:])

Predict what this code will do.


In [ ]:
some_tuple = (10,20,30)
some_tuple[1] = 50

In [ ]:
some_tuple = (10,20,30)
some_tuple.append(10)

Summarize

How are tuples like lists? How are they different?

Geeky note: tuples are useful because they are "hashable" and can be used as keys for dictionaries, while lists are not hashable and can't be dictionary keys.

Strings

Strings hold text. They are actually tuple-like under the hood.

Predict what this code will do.


In [ ]:
a_string = "This is a string"
print(a_string[0])
print(a_string[5:])
print(a_string[:-3])

Predict what this code will do.


In [ ]:
a_string = "This is a string"
a_string[0] = "a"

Summarize

How are strings like lists? How are they different?

If you want to modify a string, convert it to a list of letters first.


In [ ]:
a_string = "This is a string"
print(a_string)

#Convert the string to a list
a_list = list(a_string)

# Set the first element in the list to "a"
a_list[0] = "a"

# Convert the list back to a string
a_string = "".join(a_list)
print(a_string)

Dictionaries

Dictionaries map keys to values. The keys and values can be almost anything.

Predict what this code will do.


In [ ]:
some_dict = {"a":10,
             "b":20,
             "c":30}
print(some_dict["a"])
print(some_dict["b"])

In [ ]:
another_dict = {1:10,
                "test":print,
                1.5:-10,
                (1,2,3):"cow"}
print(another_dict["test"])
print(another_dict[(1,2,3)])

Predict what this code will do.


In [ ]:
some_dict = {"a":10,
             "b":20,
             "c":30}
print(some_dict[10])

Summarize

How do you access elements in a dictionary?

Predict what this code will do.


In [ ]:
some_dict = {"a":10,"b":20,"c":30}
for k in some_dict.keys():
    print(k,some_dict[k])

In [ ]:
some_dict = {"a":10,"b":20,"c":30}    
for v in some_dict.values():
    print(v)

Summarize

What do keys() and values() do?

Predict what this code will do.


In [ ]:
some_dict = {"a":10,"b":20,"c":30}
some_dict["c"] = 50
print(some_dict)

In [ ]:
some_dict = {"a":10,"b":20,"c":30}
some_dict["d"] = 50
print(some_dict)

Summarize

How do you set values in the dictionary?

Predict what this code will do.


In [ ]:
some_dict = {"a":10,"b":20,"c":30}
x = some_dict.pop("a")
print(x)
print(some_dict)

Summarize

How do you remove a key-value pair from a dictionary?

Implement

Create a dictionary that lets you look up each species by its color.

species color
cow brown
pig pink
salmon silver
squid it decides

In [ ]:

Summary

  • tuples
    • are like lists (ordered collection of stuff)
    • you can access elements like lists
    • but you cannot change them once you've made them
  • strings
    • are like tuples (ordered collections of letters)
    • you can access elements like tuples or lists
    • you cannot change them once you've made them like tuples
  • dictionaries
    • links keys to values
    • like lists, can be changed after you make them