Lab 10 Prelab … Python Tuples and Lists
Objectives
to create a list of Python tuples to store data from a file
Copy the Lab10_Prelab folder from the course Labs folder to your L: drive as usual.
Exercise 1.) Creating a List of Tuples to hold Atomic Table Information
The molecular weight of molecules is based on the weight of a given element and the number of atoms of
that element that go into a molecule. Atomic weights are often defined in tables. To simulate a table
where we could look up the value of the atomic weight of an element, we can create a list of tuples, where
each tuple in the list contains an element symbol, the element name, and the atomic weight of the element
(H, Hydrogen, 1.00794). For this exercise we want to create a list that holds the values for the first 10
elements in the atomic weight table found in the file: atomicWeightTable.txt
The entries in the list should consist of the tuples comprised of the symbol, name, and atomic weight of
the element, e.g. ('H', 'Hydrogen', 1.00794)
A list of one element could be defined as:
partialAtomicTable = [('H', 'Hydrogen', 1.00794)]
Save the program that you write for this prelab as molecularWeight.py
Start by writing a function, createAtomicTable(), that has one parameter, the name of the data file. This
function will open the data file, create a list of tuples with the information from the file, and then return
the list of tuples. Use the algorithm below to help you create the function.
To create the list of tuples, you will need to do the following steps:
1. Create an empty list: partialAtomicTable = []
2. Open the file for reading
3. Use the readlines method to create a list of strings representing each line of the table
4. For each line in the list of strings:
use the split method to break each line into a list of strings representing the three pieces of
information. (Note: Using the split function with no parameters will split the string on white space
such as tabs, spaces, newlines, etc.)
using the results from the split function, create a tuple holding the three pieces of information:
currentTuple = (symbol, name, weight)
add the tuples to the list using the append method: partialAtomicTable.append(currentTuple)
5. close the file
6. return the list of tuples
Complete your function, create a main() to test it, and show your work to the TA at the start of Lab10
>>>myAtomicTable = createAtomicTable(“atomicWeightTable.txt”)
>>>printNow( myAtomicTable)
© Copyright 2026 Paperzz