Storing Programs for Re-use

  • Using ipython is great for learning because of the instant feedback, but at some point you will want to save your code to use for another day. To do so, we use our text editor to write code in a file that ends with the extension '.py'.

  • All python scripts and test files used in the course can be downloaded https://ki-data.mit.edu/bcc/teaching/IntroToPython.tgz

  • Login and password will be provided during class

Examples

  • Example 1

Open a text editor
type: print("I can program in Python!\n")
Save the file as expert.py

1) in Unix, type python expert.py
python expert.py 
I can program in Python!

2) in ipython type run expert.py
In [1]: run expert.py
I can program in Python!

3) include the following in the first line of your script and type ./expert.py in unix
#!/usr/bin/env python

4) in Canopy
Open expert.py
run expert.py
  • Example 2

Last updated

Was this helpful?