Week 5: Functions, modules, files, and the terminal¶
~10 min
This week's goal¶
This week you move from one long script to small named pieces. You learn to define and call functions, to see which names a function can use, to split code across files with import, and to read and write files and handle errors. You also learn the terminal basics you need to run all of this from the command line.
The main text is Part 4 of the Python course, lessons 1 to 5, and the terminal and shell page.
Day-by-day (about 1.5-2 hours per day)¶
Day 1. Read the terminal and shell page, at least the sections on moving between folders, listing files, and running programs. Open a terminal, go to your course folder with cd, and list the files with ls. Then run python3 word_count.py from the folder that holds it. Keep this habit for the rest of the plan.
Day 2. Read defining and calling functions. Take the word counter from week 4 and move its counting logic into a function, count_words(text), that returns a dictionary. Call it from the rest of the program. Test the function with assert statements, for example assert count_words("a a b") == {"a": 2, "b": 1}.
Day 3. Read scope: where names live. Write a small function that changes a variable inside itself and a global variable with the same name. Print both before and after the call. Then read the lesson's explanation of local and global names. Write one sentence in your notes that says why a function should return a value rather than change a global.
Day 4. Read recursion basics. Write a function that adds the numbers from 1 to n in two ways: with a loop, and with recursion. Check that both return the same value for n = 5, which is 15. Recursion is useful, but it is not the default choice for every problem. Note in your notes when the loop version is clearer.
Day 5. Read modules and imports and files and exceptions. Move count_words into a file textstats.py. Import it from main.py with from textstats import count_words. In main.py, ask for a file name with input, open the file, and handle FileNotFoundError with a short message instead of a traceback.
Day 6. Finish the tool. Make main.py print the total number of words and the number of distinct words, not only the top five. Run it on a file that exists and on a name that does not. Then run python3 main.py from a different folder to see what breaks. Note the error and the reason in your notes. The reason is usually that Python looks for textstats.py in the folder you run from.
Day 7 is a rest day.
Deliverable¶
A folder with textstats.py (the functions) and main.py (the program). Running python3 main.py asks for a file name, prints the total and distinct word counts, and prints a short message when the file is missing. Your count_words function has at least two assert checks in the same file.
Check yourself¶
- What does a function return if it has no
returnstatement? - What is the difference between a local name and a global name?
- Why is
import textstatsnot enough whentextstats.pyis in another folder? - Why does
with open(name) as f:help when you read a file? - What does
lsshow, and what doescd ..do in the terminal?
Answers
- It returns
None. A function with noreturngives backNonewhen it finishes. - A local name is created inside a function and exists only while the function runs. A global name is defined at the top level of a module and can be read from any function in that module.
- Python searches for the module in the folder you run the program from and in other standard places. If the file is in another folder, Python does not find it unless that folder is on the search path, or you run the program from the folder that holds the file.
- The
withblock closes the file when the block ends, even when an error happens inside it. lslists the files and folders in the current folder.cd ..moves to the parent folder.