Part 5

Tuple

Tuple is a data structure which is, in many ways, similar to a list. The most important differences between the two are:

  • Tuples are enclosed in parentheses (), while lists are enclosed in square brackets []
  • Tuples are immutable, while the contents of a list may change

The following bit of code creates a tuple containing the coordinates of a point:

point = (10, 20)

The items stored in a tuple are accessed by index, just like the items stored in a list:

point = (10, 20)
print("x coordinate:", point[0])
print("y coordinate:", point[1])
Sample output

x coordinate: 10 y coordinate: 20

The values stored in a tuple cannot be changed after the tuple has been defined. The following will not work:

point = (10, 20)
point[0] = 15
Sample output

TypeError: 'tuple' object does not support item assignment

Programming exercise:

Create a tuple

Points:
Loading...

/

Loading...

Please write a function named create_tuple(x: int, y: int, z: int), which takes three integers as its arguments, and creates and returns a tuple based on the following criteria:

  1. The first element in the tuple is the smallest of the arguments
  2. The second element in the tuple is the greatest of the arguments
  3. The third element in the tuple is the sum of the arguments

An example of its use:


if __name__ == "__main__":
    print(create_tuple(5, 3, -1))
Sample output

(-1, 5, 7)

Programming exercise:

The oldest person

Points:
Loading...

/

Loading...

Please write a function named oldest_person(people: list), which takes a list of tuples as its argument. In each tuple, the first element is the name of a person, and the second element is their year of birth. The function should find the oldest person on the list and return their name.

An example of the function in action:

p1 = ("Adam", 1977)
p2 = ("Ellen", 1985)
p3 = ("Mary", 1953)
p4 = ("Ernest", 1997)
people = [p1, p2, p3, p4]

print(oldest_person(people))
Sample output

Mary

Programming exercise:

Older people

Points:
Loading...

/

Loading...

In this exercise we are handling tuples just like the ones described in the previous exercise.

Please write a function named older_people(people: list, year: int), which selects all those people on the list who were born before the year given as an argument. The function should return the names of these people in a new list.

An example of its use:

p1 = ("Adam", 1977)
p2 = ("Ellen", 1985)
p3 = ("Mary", 1953)
p4 = ("Ernest", 1997)
people = [p1, p2, p3, p4]

older = older_people(people, 1979)
print(older)
Sample output

[ 'Adam', 'Mary' ]

What is the purpose of a tuple?

Tuples are ideal for when there is a set collection of values which are in some way connected. For example, when there is a need to handle the x and y coordinates of a point, a tuple is a natural choice, because coordinates will always consist of two values:

point = (10, 20)

Technically it is of course possible to also use a list to store these:

point = [10, 20]

A list is a collection of consecutive items in a certain order. The size of a list may also change. When we are storing the coordinates of a point, we want to store the x and y coordinates specifically, not an arbitrary list containing those values.

Because tuples are immutable, unlike lists, they can be used as keys in a dictionary. The following bit of code creates a dictionary, where the keys are coordinate points:

points = {}
points[(3, 5)] = "monkey"
points[(5, 0)] = "banana"
points[(1, 2)] = "harpsichord"
print(points[(3, 5)])
Sample output
monkey

Attempting a similar dictionary definition using lists would not work:

points = {}
points[[3, 5]] = "monkey"
points[[5, 0]] = "banana"
points[[1, 2]] = "harpsichord"
print(points[[3, 5]])
Sample output

TypeError: unhashable type: 'list'

Tuples without parentheses

The parentheses are not strictly necessary when defining tuples. The following two variable assignments are identical in their results:

numbers = (1, 2, 3)
numbers = 1, 2, 3

This means we can also easily return multiple values using tuples. Let's have alook at he following example:

def minmax(my_list):
  return min(my_list), max(my_list)

my_list = [33, 5, 21, 7, 88, 312, 5]

min_value, max_value = minmax(my_list)
print(f"The smallest item is {min_value} and the greatest item is {max_value}")
Sample output

The smallest item is 5 and the greatest item is 312

This function returns two values in a tuple. The return value is assigned to two variables at once:

min_value, max_value = minmax(my_list)

Using parentheses may make the notation more clear. On the left hand side of the assignment statement we also have a tuple, which contains two variable names. The values contained within the tuple returned by the function are assigned to these two variables.

(min_value, max_value) = minmax(my_list)

You may remember the dictionary method items in the previous section. We used it to access all the keys and values stored in a dictionary:

my_dictionary = {}

my_dictionary["apina"] = "monkey"
my_dictionary["banaani"] = "banana"
my_dictionary["cembalo"] = "harpsichord"

for key, value in my_dictionary.items():
    print("key:", key)
    print("value:", value)

Tuples are at work here, too. The method my_dictionary.items() returns each key-value pair as a tuple, where the first item is the key and the second item is the value.

Another common use case for tuples is swapping the values of two variables:

number1, number2 = number2, number1

The assignment statement above swaps the values stored in the variables number1 and number2. The result is identical to what is achieved with the following bit of code, using a helper variable:

helper_var = number1
number1 = number2
number2 = helper_var
Programming exercise:

Student database

Points:
Loading...

/

Loading...

In this series of exercises you will create a simple student database. Before diving in, please spend a moment reading through the instructions and thinking about what sort of data structures are necessary for organising the data stored by your program.

adding students

First write a function named add_student, which adds a new student to the database. Also write a preliminary version of the function print_student, which prints out the information of a single student.

These function are used as follows:

students = {}
add_student(students, "Peter")
add_student(students, "Eliza")
print_student(students, "Peter")
print_student(students, "Eliza")
print_student(students, "Jack")

Your program should now print out

Sample output
Peter:
 no completed courses
Eliza:
 no completed courses
Jack: no such person in the database

adding completed courses

Please write a function named add_course, which adds a completed course to the information of a specific student in the database. The course data is a tuple consisting of the name of the course and the grade:

students = {}
add_student(students, "Peter")
add_course(students, "Peter", ("Introduction to Programming", 3))
add_course(students, "Peter", ("Advanced Course in Programming", 2))
print_student(students, "Peter")

When some courses have been added, the information printed out changes:

Sample output
Peter:
 2 completed courses:
  Introduction to Programming 3
  Advanced Course in Programming 2
 average grade 2.5

repeating courses

Courses with grade 0 should be ignored when adding course information. Additionally, if the course is already in the database in that specific student's information, the grade recorded in the database should never be lowered if the course is repeated.

students = {}
add_student(students, "Peter")
add_course(students, "Peter", ("Introduction to Programming", 3))
add_course(students, "Peter", ("Advanced Course in Programming", 2))
add_course(students, "Peter", ("Data Structures and Algorithms", 0))
add_course(students, "Peter", ("Introduction to Programming", 2))
print_student(students, "Peter")
Sample output
Peter:
 2 completed courses:
  Introduction to Programming 3
  Advanced Course in Programming 2
 average grade 2.5

summary of database

Please write a function named summary, which prints out a summary based on all the information stored in the database.

students = {}
add_student(students, "Peter")
add_student(students, "Eliza")
add_course(students, "Peter", ("Data Structures and Algorithms", 1))
add_course(students, "Peter", ("Introduction to Programming", 1))
add_course(students, "Peter", ("Advanced Course in Programming", 1))
add_course(students, "Eliza", ("Introduction to Programming", 5))
add_course(students, "Eliza", ("Introduction to Computer Science", 4))
summary(students)

This should print out

Sample output
students 2
most courses completed 3 Peter
best average grade 4.5 Eliza
Programming exercise:

A square of letters

Points:
Loading...

/

Loading...

This final exercise in this part is a relatively demanding problem solving task. It can be solved in many different ways. Even though this current section in the material covers tuples, tuples are not necessarily the best way to go about solving this.

Please write a program which prints out a square of letters as specified in the examples below. You may assume there will be at most 26 layers.

Sample output

Layers: 3

CCCCC
CBBBC
CBABC
CBBBC
CCCCC
Sample output

Layers: 4

DDDDDDD
DCCCCCD
DCBBBCD
DCBABCD
DCBBBCD
DCCCCCD
DDDDDDD

NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == "__main__" block.

Please respond to a quick questionnaire on this week's materials.

Quiz:
Loading...
Points:
Loading...

Log in to view the exercise

You have reached the end of this section!

You can check your current points from the blue blob in the bottom-right corner of the page.