Python

From ElectroDragon
Jump to: navigation, search

Installation

  • to check if python installed, you can run python and see the command line >>
  • to check python version in linux run, python -V
  • following content based on python2.7
  • Install python-pip, setuptools, wheels, etc
apt-get install python-pip
pip install -U pip setuptools # get support by setuptools, and setup.py
  • Example to use setup.py to install package: python setup.py install

Windows

  • Download from here for 2.7., add pip and add system path
  • Set path for system environment for windows, on cmd command, run once, and you can type python directly anytime anywhere in cmd to run python
set path=%path%;C:\Python27\
  • Or open system environment config to setup
  • Install module, downloard tar.gz file, unzip and run command: setup.py install

Linux

  • Install python 3.2 in linux: apt-get install python3, run by: python3 yourscript.py
  • install python3 pip: apt-get install python3-pip
  • install python-dev python-pip: sudo apt-get install python-dev
  • download targ.gz zip file and setup

Dependency

  • Pip install package
python2   -m pip install SomePackage  # default Python 2
python2.7 -m pip install SomePackage  # specifically Python 2.7
python3   -m pip install SomePackage  # default Python 3
python3.4 -m pip install SomePackage  # specifically Python 3.4

Basic

Command description example Header text
comment # for line, """ and """ for paragraph Example Example
print Example print("234") # print string; print 234 # print variable Example
linux bash run #! /usr/bin/python Example Example
Example Example Example Example

Format

String Format

print("I'm %s. I'm %d year old" % ('Vamei', 99))
print ('this is %s %%') (% date) # %% to print %
  • s.strip() .lstrip() .rstrip(',') remove empty space, and special words

Format type

  • print type(aaa) # print type of aaa variable
  • print len(aaa) # print length

Change format

a = float(b) change to type float

Module String

Replace

import string
a = 'abcde'
b = string.replace(a, 'ab', '', 1)
print b

file operation

    myfile = open('/srv/www/idehe.com/stock/data.txt', 'w')
    myfile.writelines('\n')    
    myfile.close()
 
    myfile = open('/srv/www/idehe.com/stock/data.txt', 'a')             # open for output (creates)
    myfile.writelines('\n')
    myfile.writelines(line1)        # write a line of text
    myfile.writelines('\n')   
    myfile.writelines(line2)        # write a line of text
    myfile.close()

Operation

not is evaluated first; and is evaluated next; or is evaluated last.

Group, List, Dict

  • group = (), list = [], dict = {}

List

  • L[2] 'SPAM!' read the third element
  • L[-2] 'Spam' red the last third element
  • L[1:] ['Spam', 'SPAM!'] cut the list from the second element

Module/Library

Pratical Test

  • for loop
for x in range (xxx, 0, -1):

Project

install/unstall a library, first install pip: apt-get install python-pip

pip install datetime
pip uninstall datetime 

common used library

  • Datetime
  • Django

Kivy

For android

  • install debian jessie, install python-kivy, python-pip
  • install buildozer (pip install), java SDK (default-jdk), cython, etc
  • buildozer init, and run: buildozer set default android_new debug deploy run, android USB debug should turn on
  • buildozer need ANT, SDK, NDK on first time

Other

Reference