python -- Notes on the Python programming Language
- Python has an excellent language reference.
- Python has a style guide.
If you're an experienced programmer new to Python then I recommend Python Tips by Muhammad Yasoob Ullah Khalid.
Implementations of Fundamental Types
List:
- e.g.
[0,1,2] - is a sequence
- is implemented as a variable-length array
- NOT a linked list
- is a different data structure from
array
Tuple:
- e.g.
(0,1,2) - is a sequence
- are immutable
Set:
- e.g.
set([0,1,2]) - e.g.
{0,1,2} - is a sequence
Dictionary:
- e.g.
{ 'a':1 , 'b':2 } - implemented with resizable hash tables
- any immutable object can be used as a dictionary key
- python exposes its hash function as the global
hash()(32-bit)
Built-in functions
Use range(a) for loops.
Note that range(a,b) returns an empty array if a and b are equal.
slice(start, stop) but use a[start:stop] instead
Numbers:
int(x[, base]): constructor that can also be used to change basechr: character from integer as ASCII codeord: ASCII code to integerdivmod(a,b): return(a // b, a % b)for integersbin,octandhexabspow: but usex**yinstead
String:
ord: return n
Iterables:
lenfor lengthnext(iterator[, default]), e.g.next(i, None)allandany: returns true if all/any element are truesortedreversedminandmaxmap(function, iterable)andreduce(function, iterable[, initializer])filter(function, iterable)but use list comprehensions insteadtupleto create a tuple from iterablezip([iterable, ...]): return list of tuples by "zippering" multiple listsenumerate: return list of numbered tuples- Unfortunately, there is no
some
imports
from MODULE import NAME1, NAME2, ...to import NAME1 and NAME2 from MODULEfrom MODULE import *to import everything from MODULEfrom .MODULEis relative import (?)
Concurrency
- Python uses reference counting GIL: Global Interpreter Lock.
- There is only one lock!
TIPS
Functions can return multiple values:
def return_both(a,b):
return a,b
x,y = return_both(1,2)
You can put multiple statements on one line by using a semi-colon, e.g. i += 1; j += 1 but this is discouraged.
Perhaps only do this on a whiteboard or doc to save space.
Get an object's reference count: sys.getrefcount(obj)
RELATED TOOLS
pip
pip is the python package manager
sudo easy_install pipto installpippipwill show you available commandspip search <keyword>pip install <package>pip freezeprints installed packages in "requirements" format- These are usually saved to requirements.txt
pip help <command>will show you extended --help for a command
setuptools is the fundamental pip dependency
virtualenv
virtualenv creates a virtual environment for each program with:
- its own
python - its own
pip
Installation:
pip install virtualenvpip install virtualenvwrappermkvirtualenv <name_of_env>to create a virtual envworkon <name_of_env>to source (use) a virtual env
Virtual environments are not saved in you application folder.
They are located in ~/.virtualenvs/name_of_env
When you pip install a package that contains global binary then it is isntall in ~/.virtualenvs/name_of_env/bin
REFERENCES
A Byte of Python. Swaroop Chitlur.
A non-magical introduction to Pip and Virtualenv for Python beginners. Jamie Matthews. 2013-04-18.
Design and History FAQ. Python Documentation.
The Mighty Dictionary. Brandon Craig Rhodes. PyCon 2010. An optimized explanation of Python's Dictionary implementation using a hash table.