Week 10: Tuples and Files

Reading: Think Python Chapter 12

(You can skip 12.4 - Variable-length argument tuples)

Notes

Immutable Sequences

  • Tuples are similar to lists, but are delimited with parentheses ( and ) instead of square brackets.
    • These parentheses are optional.
1, 2, 3
(1, 2, 3)
(2, "horse", "zebra")
(2, 'horse', 'zebra')
  • Tuples are immutable!
X = (3, 2, 1)
X[1] += 1

Like strings, which are also immutable, you can concatenate tuples (which results in a new tuple):

X = (3, 2, 1)
X + (4, 0)
(3, 2, 1, 4, 0)

Tuples can be used for assignment, which is called “tuple assignment” or “multiple assignment.”

x, y = 3, 4
x
3
y
4

Tuple Return

To get a function to return multiple values, return a tuple of those values:

Tuple Swap

Sometimes you will need to swap (exchange) the values of two variables.

One way to do this is by creating a temporary variable:

L = ['sea', 'lake', 'ocean', 'pond']
temp = L[0]
L[0] = L[1]
L[1] = temp
L
['lake', 'sea', 'ocean', 'pond']

Using tuple assignment makes this easier:

L[0], L[3] = L[3], L[0]
L
['pond', 'sea', 'ocean', 'lake']

Here’s an example:

Files

These notes are not covered by the textbook.

Reading from and writing to text files allows us to easily process large amounts of data with Python.

Download metamorphosis.txt. It is an excerpt from Franz Kafka’s Metamorphosis.

You can open the file using this syntax. It will read in the file as a single string.

open_file.py
with open('metamorphosis.txt', 'r') as in_file:
    lines = in_file.read()

print(type(lines))
print(lines)

File Path

Using the filename by itself as the first argument to open only works if the file is in the same directory as the .py file you are running.

Otherwise, you need to include the full file path.

Try making a new directory called text_files inside of where the .py file is and opening the file:

file_path.py
with open('text_files/metamorphosis.txt', 'r') as in_file:
    lines = in_file.read()

Because the file is read in as a single string, it includes the special “newline” character '\n', which creates a new line (similar to hitting the Enter or Return key on your keyboard).

You can turn this long string into a list of shorter strings, split by the newline characters, using <str>.split.

line_list = lines.split("\n")

Some examples of how this works:

Writing to a file is very similar

open_file.py
s = "some content to write to a file"
with open('output_file.txt', 'w') as out_file:
    print(s, file=out_file)
  • The second argument of open is 'w' for “writing”
    • To read, we used 'r' for “reading”
  • We use the print function with a keyword argument file=
    • Python “prints” the output to a file instead of the console

You should be able to open output_file.txt and read it with a text editor.

Overwriting Files

Be careful! When you write to a file with Python, you will overwrite any pre-existing file.

Practice

Practice Problem 10.1

Practice Problem 10.1

Write a function longest_shortest that takes as argument a list of strings and return a tuple:

  • The first item is the length of the shortest string.
  • The second item is the length of the longest string.

Practice Problem 10.2

Practice Problem 10.2

Write a function bubble_sort that takes as argument a list of numbers and sorts the list, largest to smallest:

  • Visit each item in the list
    • Compare the item to the next item
    • If the next item is larger, swap the two items
  • Repeat this until the list is sorted

When done, return the list.

Practice Problem 10.3

Practice Problem 10.3

Write a function read_walden that reads in walden.txt, counts the number of lines, and returns that number.

You should find 18 lines.

The text is an excerpt from Walden by Henry David Thoreau.

Practice Problem 10.4

Practice Problem 10.4

Write a function count_file_lines that takes one argument: a string representing the path to a text file. Your function should return a tuple:

  • The first item is the number of lines in the file.

  • The second item is the number of characters in the file.

  • count_file_lines('walden.txt') should return 18, 1194

  • count_file_lines('metamorphosis.txt') should return 27, 1672

Homework

  • Homework problems should always be your individual work. Please review the collaboration policy and ask the course staff if you have questions.

  • Double check your file names and return values. These need to be exact matches for you to get credit.

Homework Problem 10.1

Homework Problem 10.1 (30 pts)

Write a function unpacked that takes as input a list of strings. The strings will all be the same length. Your function should return a tuple.

  • The first item of the tuple will be the first characters of each string, concatenated together
  • The second item of the tuple will be the second characters of each string, concatenated together
  • …and so forth.

Examples:

  • unpacked(["cat", "dog"]) should return ('cd', 'ao', 'tg')
  • unpacked(["mole", "tree", "logs"]) should return ('mtl', 'oro', 'leg', 'ees')

Submit as unpacked.py.

Homework Problem 10.2

Homework Problem 10.2 (25 pts)

Write a function alphabetizer that takes as input a list of strings and alphabetizes (puts in alphabetical order) the strings without any regard to capitalization.

  • alphabetizer(['Banana', 'apple']) returns ['apple', 'Banana']

Submit as alphabetizer.py.

Homework Problem 10.3

Homework Problem 10.3 (45 pts)

Write a function file_character that takes one argument: a string representing the path to a text file. The function should return a tuple:

  • The first item is the count of the second-most common character in the file.

  • The second item is the character.

  • file_character('walden.txt') should return 126, 'e'

Submit as file_character.py.