Check also out the excellent documentation at python.org
Environment
Reimport a module after modifications:
import imp
imp.reload(mymodule)
Append directories to your Python Path:
import sys
sys.path.append("home/mydir") # UNIX path
sys.path.append("D:\\data\\mydir") # Windows path
In order to run self-coded functions, you need to place them in a module and then import the module. A module is a folder containing a file called __init__.py
If you want to be able to call
from module import *
the file must contain a definition of the
__all__
variable:
__all__ = ["func1" "func2"]
where the brackets contain the list of submodules (remember to keep it up-to-date). In the example above, the submodule files are ‘func1.py’ and ‘func2.py’.
The file may also contain code that you’d like executed each time the module is imported.
Importing a module:
import module
You may still import submodules if __init__.py is empty:
import module.submodule
Useful commands
>>> import os
>>> os.getcwd() # returns current folder path
>>> os.chdir('newdir')
>>> execfile('myfile.py')
IDLE shortcuts
Alt-P: previous command
Alt-N: next command
Syntax
# this is a comment
List
>>> a = ['word', 100, 13]
>>> a[2]
13
>>> len(a)
3
>>> range(5)
[0, 1, 2, 3, 4]
Functions
def fib(n):
"""Print a Fibonacci series up to n."""
a, b = 0, 1
while b < n:
print b,
a, b = b, a + b