1, 2, 3
(1, 2, 3)
Reading: Think Python Chapter 12
(You can skip 12.4 - Variable-length argument tuples)
(
and )
instead of square brackets.
Like strings, which are also immutable, you can concatenate tuples (which results in a new tuple):
Tuples can be used for assignment, which is called “tuple assignment” or “multiple assignment.”
To get a function to return multiple values, return a tuple of those values:
Sometimes you will need to swap (exchange) the values of two variables.
One way to do this is by creating a temporary variable:
Using tuple assignment makes this easier:
Here’s an example:
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
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:
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
.
Some examples of how this works:
Writing to a file is very similar
open_file.py
'w'
for “writing”
'r'
for “reading”print
function with a keyword argument file=
You should be able to open output_file.txt
and read it with a text editor.
Be careful! When you write to a file with Python, you will overwrite any pre-existing file.
Write a function longest_shortest
that takes as argument a list of strings and return a tuple:
Write a function bubble_sort
that takes as argument a list of numbers and sorts the list, largest to smallest:
When done, return the list.
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.
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 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.
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.
Examples:
unpacked(["cat", "dog"])
should return ('cd', 'ao', 'tg')
unpacked(["mole", "tree", "logs"])
should return ('mtl', 'oro', 'leg', 'ees')
Submit as unpacked.py
.
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
.
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
.