python摘要

python

缺點:執行效能較慢
優點:程式編排方法一致

refer
http://code.google.com/p/using-python/wiki/Welcome
http://zh.wikipedia.org/wiki/Python
http://pydoing.blogspot.tw/2012/12/Python-Guide.html

...............

Install on linux
1
download from www.python.org
2
安装
#./configure --prefix=/usr/local/python
#make
#make install
3
設定路徑
#mv /usr/bin/python /usr/bin/python.bak
#ln -sf /usr/local/python/bin/python /usr/bin/python

refer
http://code.google.com/p/using-python/wiki/Installation

................


指令

進入IDLE(互動模式)
#python
Python 2.7.3 (default, Sep 26 2013, 20:08:41)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>


執行python script
#python < python script >
ex:
#vi hello.py
#!/usr/bin/env python
print ‘hello world'


ps:
忽略warnning message
#python -W ignore < python script>

...........................

注意事項

indentation:在Python中不可隨意縮排,因為縮排是Python劃分程式區塊的方式
ex:
以下執行時會發生錯誤
a = 1
print(a)
a = 3
print(a)

.................................................................
基本操作

變數
1.case-sensitive(大小寫有別)
2.可一次指定多個變數的值
ex:
m, n = 5, 6

3.清除變數方法有2種
var = None
del var
4.建立時自動宣告變數範圍
ex:
x = 10 # x的範圍是全域
def outer():
  y = 20 # y的範圍是在outer() 函式內

5手動設定變數範圍
宣告非全域變數
nonlocal
宣告全域變數
gobal

 


流程控制

while
while < condietion>:
 < statement>

應用
i = 10 # 設定控制變數
while i > 0:
 # 迴圈工作區
 print(i)
 i -= 1 # 調整控制變數值

ex:
len = 10
while i > len:
 i+=1
 print(i)

...

for
for < target> in < range or array>:
 < statement>

應用
i = 10 # 設定控制變數
for i in range(10, 0, -1):
 print(i)

ex:
list1=[1,5,3,2]
for x in list1:
  print(x)

...

if
if < condietion>:
  < statement>
elif:
  < statement>
else:
  < statement>

ex:
判斷字串中是否有"-"
if ‘-' in string:
  print ‘string include -'

.....................................................

import
載入library module,有兩種載入方法,如下

import < module>
ex:
載入sys模組
import sys
print sys.argv, sys.path

ps:
import < module> as < short name>
載入module後另指定別名
ex:
import numpy as np

from < module> import < function>
ex:
載入sys模組內argv,path兩個功能
from sys import argv,path
print argv, path

ps:
載入sys模組內所有功能
from sys import *
print argv, path

....................................................

try and except
程式正常運作時,會執行try的區域
若程式運作發生例外狀況,就會執行except的區域

ex:
a = 22
b = 33

try:
  if a > b:
    print(n)
except:
  print("except")

......................................................................

定義函數
使用def+一個空格+函數名稱與小括弧+冒號

回傳一個值
def big(a, b):
 if a > b:
     return a
 else:
  return b
print(big(33, 22))

回傳多值
def select_choice():
...
 return (i, card)

my_i, my_card = select_choice()

..........................................................

新增模組

1
#mkdir moduletest
#cd moduletest
#vi moduletest.py

def printhello():
 print ‘hello'

2編輯metadata
#vi setup.py
from distutils.core import setup
setup(
 name='moduletest',
 version ='1.0.0',
 py_modules=[‘moduletest'],
 author='raymond',
 author_email='ray@systw.net',
 url='systw.net',
 description ='for test'

3建構與安裝
#python setup.py sdist
#python setup.py install

4使用module
#vi usemodule.py
import moduletest
moduletest.printhello()

..

更新模組
1.
#vi setup.py
...omit...
version ='1.1.0',
...omit...

2
#python setup.py sdist upload

....................................................................

常見錯誤

no display name
ex:
Traceback (most recent call last):
...omit...
_tkinter.TclError: no display name and no $DISPLAY environment variabl
solve:
執行export DISPLAY=localhost:0


IndentationError
ex:
File "pcashow.py", line 13
pl.scatter(data[target == i, 0], data[target == i, 1],
^
IndentationError: expected an indented block

solve:
縮排調好


......................................................................

套件管理

常見的套件安裝
1.到套件底下檢查是否有setup.py
2.執行python setup.py install


套件管理工具
讓Python安裝模組時變得很簡單
常見的有以下
  easy_install
  pip

...

easy_install
支援egg格式安裝

linux 安裝
#yum install python-setuptools

windows安裝
1
download
http://pypi.python.org/pypi/setuptools
2
install
# python ez_setup.py
ps
Windows可使用setuptools-x.x.win32-py2.x.exe直接安裝

...

pip
類似easy_install,有些作業系統已經內建了

安裝方法1
# curl http://python-distribute.org/distribute_setup.py | python
# curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python

安裝方法2( 用easy_install)
# easy_install pip


refer
http://www.openfoundry.org/tw/tech-column/8536-introduction-of-python-extension-management-tools


2014-11-22 16:30:08發表 2015-01-22 18:44:40修改   

數據分析
程式開發

計算機組織與結構
資料結構與演算法
Database and MySql
manage tool
windows
unix-like
linux service
network
network layer3
network layer2
network WAN
network service
作業系統
數位鑑識
資訊安全解決方案
資訊安全威脅
Cisco security
Cisco network
Cisco layer3
Cisco layer2



  登入      [牛的大腦] | [單字我朋友] Powered by systw.net