Sunday, October 16, 2005
python for beginner - misc
Here are miscellaneous information on python programmation:
- power notation: 2*2*2 = 2^3 becomes 2**3
- multiple affectation possible: a, b = b, a
(useful to return several values from functions return a,b,c)
- install python html documentation www.python.org/doc
list all modules and their functions (such as math, md5...)
- programming practice: start class names with an upper case (class My_class)
- define functions in right order (i.e. before you call them for the first time)
- if __name__ == "__main__":
print "You ran this module directly and did not import it"
- import 'turtle' module to display animated lines
How to change the default version of python in use in Debian:
- edit /usr/share/python/debian_defaults and set the version number you want
How to change default encoding in python:
- default encoding is 'ascii', you can see this running "import sys; sys.getdefaultencoding();" from a python console
- you cannot change default encoding at runtime, you need to specify it in a special script: sitecustomize.py (generally created under ${python_path}/site-packages but you can save it in any path where 'import' can look ("import sys; print sys.path;" from python console for the full list)
- in this file, writes down:
# sitecustomize.py
# this file can be anywhere in your Python path,
# but it usually goes in usr/lib/python2.5/site−packages/
import sys
sys.setdefaultencoding('utf-8')
- restart python
- power notation: 2*2*2 = 2^3 becomes 2**3
- multiple affectation possible: a, b = b, a
(useful to return several values from functions return a,b,c)
- install python html documentation www.python.org/doc
list all modules and their functions (such as math, md5...)
- programming practice: start class names with an upper case (class My_class)
- define functions in right order (i.e. before you call them for the first time)
- if __name__ == "__main__":
print "You ran this module directly and did not import it"
- import 'turtle' module to display animated lines
How to change the default version of python in use in Debian:
- edit /usr/share/python/debian_defaults and set the version number you want
How to change default encoding in python:
- default encoding is 'ascii', you can see this running "import sys; sys.getdefaultencoding();" from a python console
- you cannot change default encoding at runtime, you need to specify it in a special script: sitecustomize.py (generally created under ${python_path}/site-packages but you can save it in any path where 'import' can look ("import sys; print sys.path;" from python console for the full list)
- in this file, writes down:
# sitecustomize.py
# this file can be anywhere in your Python path,
# but it usually goes in usr/lib/python2.5/site−packages/
import sys
sys.setdefaultencoding('utf-8')
- restart python