Python files



(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 ( and   on Windows, ,  , and   on 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  by typing

python nobel.py

at the terminal prompt.

introduces two new concepts: comments and multiline strings.

Open  in 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?</li> How do you print a multi-line string so that whitespace is preserved?</li> </ol>

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  file.</li> Open the script in your text editor.</li> Read through the script. Don't worry about understanding every last detail. Instead focus on:

<ul> Overall, what does this script do?</li> What variables and data types are used? Where are the strings, integers, and floats?</li> What functions are used, and why?</li> Where are the comments, and what do they tell you?</li> </ul> </li> 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?</li> Think about one way that you could extend the script to make it more useful. How would you make that change in code?</li> </ol>

Here are you scripts to read and run. Have fun with them!


 * tip_calculator.py
 * pizza_calculator.py