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 [1]:
some_tuple = (10,20,30)
print(some_tuple[1])


20

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


(10, 20, 30)

Predict what this code will do.


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


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-2528adf0c601> in <module>()
      1 some_tuple = (10,20,30)
----> 2 some_tuple[1] = 50

TypeError: 'tuple' object does not support item assignment

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


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-2afc9f3911b2> in <module>()
      1 some_tuple = (10,20,30)
----> 2 some_tuple.append(10)

AttributeError: 'tuple' object has no attribute 'append'

Summarize

How are tuples like lists? How are they different?

  • You access values like lists
  • You can't tuples once they've been created

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 [9]:
a_string = "This is a string"
print(a_string[0])
print(a_string[5:])
print(a_string[:-1])


T
is a string
Thisxis a strin

Predict what this code will do.


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


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-d643d3aafb22> in <module>()
      1 a_string = "This is a string"
----> 2 a_string[0] = "a"

TypeError: 'str' object does not support item assignment

Summarize

How are strings like lists? How are they different?

  • You access values like lists
  • You can't change elements individually.

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


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

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

# 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)


This is a string
['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g']
a,h,i,s, ,i,s, ,a, ,s,t,r,i,n,g

Dictionaries

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

Predict what this code will do.


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


10
20

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


<built-in function print>
cow

Predict what this code will do.


In [27]:
some_dict = {"a":10,
             "a":20,
             "c":10}
some_dict["a"] = [1,2,3]

Summarize

How do you access elements in a dictionary?

  • You access "values" in a dictionary using "keys".
  • This only works one-way: keys map to values, values do not map to keys

Predict what this code will do.


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


a 10
b 20
c 30

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


10
20
30

In [30]:
another_dict = dict([(some_dict[k],k) for k in some_dict.keys()])
another_dict[10]


Out[30]:
'a'

Summarize

What do keys() and values() do?

  • These provide access to all of the keys and values in a dictionary.
  • These are iterators (like range) that can be looped through with for.

Predict what this code will do.


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


{'a': 10, 'b': 20, 'c': 50}

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


{'a': 10, 'b': 20, 'c': 30, 'd': 50}

Summarize

How do you set values in the dictionary?

SOME_DICTIONARY[KEY] = VALUE

Predict what this code will do.


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


10
{'b': 20, 'c': 30}

Summarize

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


In [ ]:
VALUE = SOME_DICT.pop(KEY)

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 [ ]:
look_up = {"brown":"cow",
           "pink":"pig",
           "salmon":"silver",
           "it decides":"squid"}

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