str
Activities for CS1 in Python, Modules POGIL:
What does each import statement make available?
Which of the following would work after from math import sqrt?
sqrt(16)math.sqrt(16)math.piConsider this file greet.py:
greet.py in Thonny directly?import greet?if __name__ == "__main__": guard useful?Write a module stats.py that:
average(numbers) that returns the mean of a list.maximum(numbers) that returns the largest value.__main__ block that tests both functions on a sample list.Then, in a separate file, import and use both functions.
/Users/me/Documents/Research/experiment1.csv). includes:
untitled5023.py).csv, .py, .docx)string)Aside: organize your files!
"data.csv" actually means os.getcwd() + "/" + "data.csv".py filedata folder and use data/data.csvThe content of a file is just a string. (maybe really long)
'An old silent pond\nA frog jumps into the pond\nSpla'
We can split it into lines:
A file haiku.txt contains:
An old silent pond
A frog jumps into the pond
Splash! Silence again
Write code using open to:
Write a function save_list(filename, items) that writes each item in items to its own line in a file.
Then write load_list(filename) that reads the file back and returns a list of strings (with whitespace stripped).
Test both functions with a short list of words.
The following JSON file player.json holds game data:
Write code to:
json.load."collector" to the list.json.dump.A scores.json file stores a list of player records, each with "name" and "score" keys.
max(..., key=...).Consider: your phone stores your photos locally and backs them up to the cloud. Your messages are synced across devices. Your game saves to a local file.
How should software designers think about these tradeoffs?