Python: Difference between revisions
Jump to navigation
Jump to search
Line 52: | Line 52: | ||
* Use triple double quotes """ to write a [https://docs.python.org/3/tutorial/introduction.html long string spanning multiple lines] or [http://stackoverflow.com/questions/7696924/multiline-comments-in-python comments in a python script] | * Use triple double quotes """ to write a [https://docs.python.org/3/tutorial/introduction.html long string spanning multiple lines] or [http://stackoverflow.com/questions/7696924/multiline-comments-in-python comments in a python script] | ||
* if dna="gatagc", then | * if dna="gatagc", then | ||
<syntaxhighlight lang='python'> | |||
dna[0]='g' | |||
dna[-1]='c' (start counting from the right) | |||
dna[-2]='g' | |||
dna[0:3]='gat' (the end always excluded) | |||
dna[:3]='gat' | |||
dna[2:]='tgc' | |||
len(dna)=6 | |||
type(dna) | |||
print(dna) | |||
dna.count('c') | |||
dna.upper() | |||
dna.find('ag')=3 (only the first occurrence of 'ag' is reported) | |||
dna.find('17', 2) (start looking from pos 17) | |||
dna.rfind('ag') ( search backwards in string) | |||
dna.islower() (True) | |||
dna.isupper() (False) | |||
dna.replace('a', 'A') | |||
</syntaxhighlight> | |||
== User's input == | |||
<syntaxhighlight lang='python'> | |||
dna=raw_input("Enter a DNA sequence: ") # python 2 | |||
dna=input("Enter a DNA sequence: ") # python 3 | |||
</syntaxhighlight> | |||
To convert a user's input (a string) to others | |||
<syntaxhighlight lang='python'> | |||
int(x, [, base]) | |||
flaot(x) | |||
str(x) #converts x to a string | |||
str(65) # '65' | |||
chr(x) # converts an integer to a character | |||
chr(65) # 'A' | |||
</syntaxhighlight> | |||
== Fancy Output == | |||
<syntaxhighlight lang='python'> | |||
print("THE DNA's GC content is ", gc, "%") # gives too many digits following the dot | |||
print("THE DNA's GC content is %5.3f %%" % " % gc) | |||
# the percent operator separating the formatting string and the value to | |||
# replace the format placeholder | |||
print("%d" % 10.6) # 10 | |||
print("%e" % 10.6) # 10.060000e+01 | |||
print("%s" % dna) # gatagc | |||
</syntaxhighlight> | |||
= Projects based on python = | = Projects based on python = |
Revision as of 09:54, 26 February 2016
Basic
Think Python (Free Ebook)
http://www.greenteapress.com/thinkpython/
How to run a python code
python mypython.py
Install a new module
The Python Package Index (PyPI) is the definitive list of packages (or modules)
sudo apt-get install python-pip pip install SomePackage pip show --files SomePackage pip install --upgrade SomePackage pip uninstall SomePackage
If a package has been bundled by its creator using the standard approach to bundling modules (with Python’s distutils tool), all you need to do is download the package, uncompress it and type:
python setup.py install
How to list all installed modules
help('modules')
How to find the location of installed modules
There are different ways
- python -v
- import MODULENAME
- help('MODULENAME')
Using this way, I find the 'RPi' module is installed under /usr/lib/python2.7/dist-packages.
if __name__ == "__main__":
http://stackoverflow.com/questions/419163/what-does-if-name-main-do
Import a compiled C module
- An example based on SWIG compiler.
string and string operators
Reference: Python for Genomic Data Science from coursera.
- Use double quote instead of single quote to define a string
- Use triple double quotes """ to write a long string spanning multiple lines or comments in a python script
- if dna="gatagc", then
dna[0]='g' dna[-1]='c' (start counting from the right) dna[-2]='g' dna[0:3]='gat' (the end always excluded) dna[:3]='gat' dna[2:]='tgc' len(dna)=6 type(dna) print(dna) dna.count('c') dna.upper() dna.find('ag')=3 (only the first occurrence of 'ag' is reported) dna.find('17', 2) (start looking from pos 17) dna.rfind('ag') ( search backwards in string) dna.islower() (True) dna.isupper() (False) dna.replace('a', 'A')
User's input
dna=raw_input("Enter a DNA sequence: ") # python 2 dna=input("Enter a DNA sequence: ") # python 3
To convert a user's input (a string) to others
int(x, [, base]) flaot(x) str(x) #converts x to a string str(65) # '65' chr(x) # converts an integer to a character chr(65) # 'A'
Fancy Output
print("THE DNA's GC content is ", gc, "%") # gives too many digits following the dot print("THE DNA's GC content is %5.3f %%" % " % gc) # the percent operator separating the formatting string and the value to # replace the format placeholder print("%d" % 10.6) # 10 print("%e" % 10.6) # 10.060000e+01 print("%s" % dna) # gatagc
Projects based on python
- pithos Pandora on linux
- Many Raspberry Pi GPIO projects
- GeneScissors It also requires pip and scikit-learn packages.
- KeepNote It depends on Python 2.X, sqlite and PyGTK.
- Zim It depends on Python, Gtk and the python-gtk bindings.
- Cherrytree It depends on Python2, Python-gtk2, Python-gtksourceview2, p7zip-full, python-enchant and python-dbus.
Qt for GUI development
- http://zetcode.com/gui/pyqt4/
- http://wiki.wildsong.biz/index.php/PyQt Create GUI in Qt Designer and convert/use it in PyQt.