CIS2300FinalReview
Tuples
Key Concepts:
• A tuple is an ordered, immutable data structure that holds multiple values of
different types.
• Parentheses ( ) are usually placed around tuples, but the commas are what make it a
tuple → not the parentheses.
t1 = 1, 2, 3
t2 = (1, 2, 3)
• Tuples use square brackets [ ] for indexing and slicing, just like lists
• Tuples are immutable → you cannot update, insert, remove items once created.
Questions:
1. Fix the error below:
fruits = ("apple", "banana", "cherry"
print(fruits)
2. Will this code print all elements of the tuple correctly? If not, fix the error:
cars = ("Ford", "Honda", "Tesla")
for i in range(0, len(cars)+1):
print(cars[i])
3. Given t1 = (1, 2, "apple", "banana”, 5), append "cherry" to the
tuple.
4. Given t2 = (1, 2, ["apple", "banana"], 5), is it possible to append
"cherry"to the inner list without transforming the tuple?
5. What does this code print?
t = (2, 4, 6)
for n in t:
n = (n * n)
print(t)
6. Write a program that loops through the tuple and counts how many numbers are
even. Then, print the total.
7. Using a loop and t = ("a", "b", "c", "d", "e"), print only elements at
even indices.
8. Given t = (1, 2, 3, 4, 5, 6), print the index and the value, but only when
the value is even.
, Dictionaries
Key Concepts:
• A dictionary stores data as key–value pairs.
• Dictionaries use curly braces { }.
student = {"name": "Alex", "age": 20, "major": "CS"}
• Each item has two parts:
o Key → the lookup term
▪ Keys must be unique and must be immutable.
o Value → the data stored under that key
▪ Values can be any type, including lists or other dictionaries.
• Dictionaries are mutable, so you can add, change, or remove items.
• Use square brackets with the key, not the index
o student["name"]
• If you try to access a key that does not exist → KeyError.
• To avoid errors, use .get():
o student.get("age") # returns None if key not found
• You update or add items using square brackets with the key
• If the key already exists, assigning a new value replaces the old one:
o student["Age"] = 21
• If the key does not exist, assigning a new value creates the entry:
o student["GPA"] = 3.7
• Can remove key-value pairs using del or .pop()
o del student["GPA"]
o student.pop("GPA")
• Common Dictionary Methods
Method Description
.keys() Returns all keys
.values() Returns all values
.items() Returns key, value pairs
.get(key) Safe lookup, returns None if key not found
Questions:
1. What does this code print?
d = {"a": 1, "b": 2}
d["c"] = d["a"] + d["b"]
print(d)
2. Would this cause an error? Why?
d = { [1,2,3] : "numbers", "x": 5 }
Tuples
Key Concepts:
• A tuple is an ordered, immutable data structure that holds multiple values of
different types.
• Parentheses ( ) are usually placed around tuples, but the commas are what make it a
tuple → not the parentheses.
t1 = 1, 2, 3
t2 = (1, 2, 3)
• Tuples use square brackets [ ] for indexing and slicing, just like lists
• Tuples are immutable → you cannot update, insert, remove items once created.
Questions:
1. Fix the error below:
fruits = ("apple", "banana", "cherry"
print(fruits)
2. Will this code print all elements of the tuple correctly? If not, fix the error:
cars = ("Ford", "Honda", "Tesla")
for i in range(0, len(cars)+1):
print(cars[i])
3. Given t1 = (1, 2, "apple", "banana”, 5), append "cherry" to the
tuple.
4. Given t2 = (1, 2, ["apple", "banana"], 5), is it possible to append
"cherry"to the inner list without transforming the tuple?
5. What does this code print?
t = (2, 4, 6)
for n in t:
n = (n * n)
print(t)
6. Write a program that loops through the tuple and counts how many numbers are
even. Then, print the total.
7. Using a loop and t = ("a", "b", "c", "d", "e"), print only elements at
even indices.
8. Given t = (1, 2, 3, 4, 5, 6), print the index and the value, but only when
the value is even.
, Dictionaries
Key Concepts:
• A dictionary stores data as key–value pairs.
• Dictionaries use curly braces { }.
student = {"name": "Alex", "age": 20, "major": "CS"}
• Each item has two parts:
o Key → the lookup term
▪ Keys must be unique and must be immutable.
o Value → the data stored under that key
▪ Values can be any type, including lists or other dictionaries.
• Dictionaries are mutable, so you can add, change, or remove items.
• Use square brackets with the key, not the index
o student["name"]
• If you try to access a key that does not exist → KeyError.
• To avoid errors, use .get():
o student.get("age") # returns None if key not found
• You update or add items using square brackets with the key
• If the key already exists, assigning a new value replaces the old one:
o student["Age"] = 21
• If the key does not exist, assigning a new value creates the entry:
o student["GPA"] = 3.7
• Can remove key-value pairs using del or .pop()
o del student["GPA"]
o student.pop("GPA")
• Common Dictionary Methods
Method Description
.keys() Returns all keys
.values() Returns all values
.items() Returns key, value pairs
.get(key) Safe lookup, returns None if key not found
Questions:
1. What does this code print?
d = {"a": 1, "b": 2}
d["c"] = d["a"] + d["b"]
print(d)
2. Would this cause an error? Why?
d = { [1,2,3] : "numbers", "x": 5 }