0% found this document useful (0 votes)
26 views6 pages

Lab 0

This document provides instructions for a lab assignment on using tuples and sets to structure data in Python. Students are asked to create two files, lab0_functions.py to contain function definitions and lab0_main.py to contain tests of those functions. The exercises involve representing points as tuples, defining functions to find max/min of tuples and sum y-coordinates of points in a set, and testing those functions. Students are provided examples and guided experiments to learn about tuples, sets, and structuring code with modules.

Uploaded by

张子超
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

Lab 0

This document provides instructions for a lab assignment on using tuples and sets to structure data in Python. Students are asked to create two files, lab0_functions.py to contain function definitions and lab0_main.py to contain tests of those functions. The exercises involve representing points as tuples, defining functions to find max/min of tuples and sum y-coordinates of points in a set, and testing those functions. Students are provided examples and guided experiments to learn about tuples, sets, and structuring code with modules.

Uploaded by

张子超
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Carleton University

Department of Systems and Computer Engineering


ECOR 1042 – Data Management
Lab 0 - Using Tuples and Sets to Structure Data.
Using Modules to Structure the Code

Objective
To learn about two of Python's compound data types (types that store collections of value):
tuples and sets.
To learn how to structure the code using modules

Prerequisite Reading
Read these sections from Practical Programming, 3rd Edition, Chapter 11, before you start this
lab:
● Storing Data Using Sets (pages 203 - 209)
● Storing Data Using Tuples (pages 209 - 214)
● Chapter 6: A Modular Approach to Program Organization

Getting Started
Begin by creating two new files within Wing 101:
• lab0_functions.py
• lab0_main.py
lab0_functions.py will contain all the function definitions, and lab0_main.py will
contain the main script, i.e., the calls to these functions for testing. Note that you will need to
import lab0_functions.py in lab0_main.py to be able to call the functions.
Make sure to include your name and student number at the top of each file using Python
comments.

Exercise 1 (…/4)
Step 1: Suppose we want to represent points on a two-dimensional Cartesian plane. In
ECOR1041, you learned how to use Python lists to store collections of data. One way to
represent a point is to store its (x, y) coordinates in a list. For example, we can represent the point
(13.0, 12.0) by the list [13.0, 12.0].

Try this experiment in the Python shell.


>>> point1 = [13.0, 12.0]
>>> point1

What is displayed when Python evaluates point1? Write the question and the answer in
lab0_main.py using python comments.

Page 1
Step 2: The problem with the approach used in Step 1 is that Python lists are mutable. For
example, we could call the append method to insert a float at the end of the list. Try this
experiment.
What is displayed when Python evaluates point1? Write the question and the answer in
lab0_main.py using python comments.
>>> point1.append(4.0)
>>> point1

If the list is supposed to represent a two-dimensional point, this does not make sense, because the
list now contains three values.
We could then call the pop method on this list to remove numbers. Try this experiment.

What is displayed each time Python evaluates point1? Write the question and the answer in
lab0_main.py using python comments.
>>> point1.pop(0) # Remove the item at index 0 in the list
>>> point1

>>> point1.pop() # Remove the last item in the list


>>> point1

The list now has only one value, so it does not represent a point.
Step 3: To avoid the problems explored in the previous step, we should represent points using an
immutable container. A tuple is similar to a list, but it cannot be modified after is created. In the
next experiment, you will learn how to create a tuple that represents a point in the 2-D Cartesian
coordinate system.
A tuple is created by a sequence of comma-separated values, enclosed in parentheses. Try this
experiment.
What is displayed when the last two statements are evaluated? Write the question and the answer
in lab0_main.py using python comments.
>>> point1 = (13.0, 12.0)
>>> type(point1)
>>> point1

Aside: enclosing the floats in parenthesis is optional, so this statement will create the same
tuple:
>>> point1 = 13.0, 12.0
>>> point1

When tuples are displayed, they are always enclosed in parentheses, so some programmers
prefer to use surrounding parentheses in expressions that create tuples.

Page 2
Step 4: As with lists, tuples are ordered sequences, so an item stored in a tuple t can be retrieved
by the expression t[i], where i is the index (position) of the item.

Try this experiment to retrieve the x and y coordinates of the point represented by point1. What
is displayed when Python evaluates x and y? Write the question and the answer in lab0_main.py
using python comments.
>>> x = point1[0]
>>> y = point1[1]
>>> x
>>> y

We can unpack all the items in a tuple, binding them to individual variables, by using a statement
of the form:
var_1, var_2, var_3, ..., var_n = t

where t is a variable bound to a tuple containing n items. This is equivalent to:

var_1 = t[0]
var_2 = t[1]
...
var_n = t[n-1]

Try this experiment. What is displayed when Python evaluates x and y? Write the question and
the answer in lab0_main.py using python comments.
>>> point2 = (14.0, 16.0)
>>> x, y = point2
>>> x
>>> y

Step 5: We can easily demonstrate that tuples are immutable. You cannot replace items in a
tuple, or insert new items or remove items. Try this experiment. What is displayed when Python
executes each statement? Write the question and the answer in lab0_main.py using python
comments.
Also, copy the following code as a comment and write the answers to the questions:
>>> point2[0] = 12.0 # Can we change the point to (12.0, 16.0)?
>>> point2.append(4.0) # Can we add a third coordinate?
>>> point2.pop(0) # Can we remove the first coordinate?

Step 6: The elements in a compound data type, such as a list or tuple, are not limited to integers
and floating point values. You can, for example, have lists that contain lists and tuples that
contain tuples. Sometimes it makes sense to mix the two types, for example, a tuple that contains
lists or a list that contains tuples.
Try this experiment, which demonstrates how to create a list of tuples that represent the points
(1.0, 5.0), (2.0, 8.0) and (3.5, 12.5):

Page 3
>>> points = [(1.0, 5.0), (2.0, 8.0), (3.5, 12.5)]
>>> points[0]
>>> points[1]
>>> points[2]

Exercise 2 (…/45)
Use the function design recipe from Chapter 3 of Practical Programming to develop a function
named max_min. The function takes a list of tuples, and each tuple contains four non-negative
integers. The function returns a new list of tuples. The new tuple has two values, the max and the
min of the four numbers provided. The tuple is at the same position in the original list.

For example, suppose the first tuple in the list passed to max_min is (27, 219, 134, 12).
The max and min values are 219 and 12, so the first tuple in the new list will be: (219, 12).

Your function definition must have type annotations and a complete docstring. You need to
provide at least 3 examples. The function definition must be in lab0_functions.py
In lab0_main.py, test your function. Your program must include manual testing (i.e., call
expression corresponding to your tests identified in your docstring and print the results with the
expected and the actual values)
Note: You are allowed to use min and max functions in your solution

Exercise 3(…/6)
Step 1: When you read Storing Data Using Sets, you learned that Python's set type allows us to
create mutable collections of unordered distinct items. The items stored in a set must be
immutable, so sets can contain values of type int, float or str, but we cannot store lists or
sets in sets. Tuples are immutable, so we can store tuples in sets.
Try this experiment, which creates a set containing the points (1.0, 2.0), (4.0, 6.0) and
(10.0, -2.0). What is displayed when points is evaluated? Write the question and the answer in
lab0_main.py using python comments.
>>> points = {(1.0, 2.0), (4.0, 6.0), (10.0, -2.0)}
>>> points

We can also initialize the set this way. Try this experiment. What is displayed when points is
evaluated? Write the question and the answer in lab0_main.py using python comments.
>>> point1 = (1.0, 2.0)
>>> point2 = (4.0, 6.0)
>>> point3 = (10.0, -2.0)
>>> points = {point1, point2, point3}
>>> points

We could instead start with an empty set, and call the add method to initialize it, one point at a
time. Try this experiment. What is displayed when points is evaluated? Write the question and

Page 4
the answer in lab0_main.py using python comments.
>>> points = set()
>>> points.add(point1)
>>> points.add(point2)
>>> points.add(point3)
>>> points

Step 2: What happens if we try to insert a point that is already in the set? Try this experiment:
>>> points.add(point2)
>>> points

How many copies of point (4.0, 6.0) are in the set? Write the question and the answer in
lab0_main.py using python comments.
Step 3: Can individual points in the set be retrieved by specifying an index (position)? Try this
experiment. What is displayed when points[0] is evaluated? Write the question and the
answer in lab0_main.py using python comments.
>>> points[0]

Step 4: We can use a for loop to iterate over all the points in the set. What is displayed when
this loop is executed? Write the question and the answer in lab0_main.py using python
comments.
>>> for point in points:
... print(point)
...

Exercise 4 (…/45)
Use the function design recipe from Chapter 3 of Practical Programming to develop a function
named sum_y. The function takes a set of n points: { (x0, y0), (x1, y1), … (xn-1, yn-1) }. Each point
is represented by a tuple containing two floats. The first and second floats are the point's x
and y coordinates, respectively.

The function returns the sum of all the y coordinates: y0 + y1 + y2 + … + yn-1.

Your function definition must have type annotations and a complete docstring. You need to
provide at least 3 examples. The function definition must be in lab0_functions.py
In lab0_main.py, test your function. Your program must include manual testing (i.e., call
expression corresponding to your tests identified in your docstring and print the results with the
expected and the actual values)

Page 5
Wrap Up
Make sure that you are meeting the requirements stated in the lab:
- Correct name for the files
- You name and student number is included at the top of each file using Phyton comments
- Note that for this lab you are required to submit two files:
o lab0_functions.py
o lab0_main.py
You are required to keep a backup copy of (all) your work for the duration of the term.
Last edited: October 21, 2021

Page 6

You might also like