(Estimated completion time: 15 - 30 minutes)
Goal #1: practice reading and running Python files
Until now we've been using the interactive Python interpreter. This is great for learning and experimenting, but you can't easily save or edit your work. For bigger projects, we'll write our Python code in a file. Let's get some practice with this!
- Download the file http://mit.edu/jesstess/www/BostonPythonWorkshop8/nobel.py by right-clicking on it and saying to save it as a ".py" file to your Desktop. The ".py" extension hints that this is a Python script.
- Open a terminal prompt, and use the navigation commands (
cdon OS X and Linux) to navigate to your Desktop directory. See navigating from a terminal for a refresher on those commands.
- Once you are in your Desktop directory, execute the contents of
at the terminal prompt.
nobel.pyintroduces two new concepts: comments and multiline strings.
nobel.pyin your text editor (see preparing your text editor for a refresher on starting the editor).
- Read through the file in your text editor carefully and check your understanding of both the comments and the code.
Study the script until you can answer these questions:
- How do you comment code in Python?
- How do you print just a newline?
- How do you print a multi-line string so that whitespace is preserved?
Goal #2: more practice with Python files
For each of the following Python programs (folks often call these "scripts"), please do the following:
- Download the script and save it to your Desktop. Be sure to save it as a
- Open the script in your text editor.
- Read through the script. Don't worry about understanding every last detail. Instead focus on:
- Overall, what does this script do?
- What variables and data types are used? Where are the strings, integers, and floats?
- What functions are used, and why?
- Where are the comments, and what do they tell you?
- Once you have a good sense of what the script does, open a terminal, navigate to the directory where you saved the script, and run it. Does it do what you expected?
- Think about one way that you could extend the script to make it more useful. How would you make that change in code?
Here are you scripts to read and run. Have fun with them!