Python實踐基礎
Python簡介
Python是一種解釋型、面向對象、動態數據類型的高級程序設計語言。自從20世紀90年代初Python語言誕生至今,它逐漸被廣泛應用于處理系統管理任務和Web編程。Python已經成為最受歡迎的程序設計語言之一。2011年1月,它被TIOBE編程語言排行榜評為2010年度語言。自從2004年以后,python的使用率是呈線性增長,一直穩居編程語言Top10。
-- 摘自百度百科
Python安裝
Python穩定的生產版本有2.7和3.3。考慮到3.3可能和一些第三方庫不兼容,建議采用2.7作為生產版本。
下載
下載地址:http://www.python.org/getit/releases/2.7.6/
環境變量配置
將Python安裝目錄添加到系統環境變量Path中去,結果如下圖:
Python基礎
命令行工具
Python安裝完成后,即可通過開始菜單啟動Python命令行工具。
Python數據類型
Number(數字) 包括int,long,float,double,complex
String(字符串) 例如:hello,"hello",hello
List(列表) 例如:[1,2,3],[1,2,3,[1,2,3],4]
Dictionary(字典) 例如:{1:"nihao",2:"hello"}
Set(集合) 例如:set([1,2])
Tuple(元組) 例如:(1,2,3,abc)
File(文件) 例如:f =open(a.txt,rw)
命令行依次輸入以下命令(或者在Python GUI下新建文件并運行):
# -*- coding: utf-8 -*-
a = 2
print(type(a))
print(a)
a = int("2")
print(type(a))
print(a)
a = 2.3
print(type(a))
print(a)
a = long(3.4)
print(type(a))
print(a)
a = "hello"
print(type(a))
print(a)
a = []
print(type(a))
print(a)
a = [1,"hello"]
print(a)
a = {1:"hello","name":"world"}
print(type(a))
print(a)
a = [1,2,3,3]
b = set(a)
print(type(b))
print(b)
a = (1,2)
type(a)
print(a)
a = (1)
print(type(a)) #結果是什么類型?
代碼塊
Python用縮進來定義一個代碼塊,類似于C語言中{}的功能。
Python支持Tab或空格縮進,但是應該避免兩者混用。建議充分利用編輯器的Tab轉換功能 ,比如設置編輯器自動將Tab轉換成4個空格。
If 語句
標準if語句格式如下:
1)
if expression:
if_suite
2)
if expression:
if_suite
else:
else_suite
3)
if expression1:
if_suite
elif expression2:
elif_suite
else:
else_suite
示例:
a = 1
if (a>1):
print(“yes”)
else:
print(“no”)
While 循環
while expression:
while_suite
For循環
有別于傳統C語言中的for循環,更類似于C#的foreach。比如:
a = [1,2,3]
for b in a
print(b)
文件操作
Python文件操作功能非常強大而簡單,比如:
handle =open(r”c:\example.txt”, “r”)
data =handle.readlines()
handle.close()
for line in data:
print(line)
異常處理
語句塊
Except 異常類型1, 異常1
語句塊
Except 異常類型2
語句塊
Else
語句快
Finally
語句塊
比如:
#-*- coding: utf-8 -*-
try:
f_name =r"c:\temp\botovatest_sent.xml"
f = open(f_name, "r")
for line in f:
print(line)
f.close();
except IOError, e:
print("文件操作異常:" + e.strerror)
else:
print("未知錯誤")
finally:
print("over")
其中:
不帶異常類型的except泛指所以類型的異常。
else塊不是必須的。
finnaly塊不是必須的。
函數
定義和調用
定義和調用函數的格式如下:
# -*- coding: utf-8 -*-
# 定義函數
def add (x,y):
'apply + operation to argument'
return (x + y)
#調用函數
print(add(1,2))
默認參數
以下調用結果將輸出“5”。
# -*- coding: utf-8 -*-
# 定義函數
def add (x,y=2):
'apply + operation to argument'
return (x + y)
#調用函數
print(add(3))
注意:避免使用可變類型的參數默認值,比如list,dictionary。
deffunction(data=[]):
data.append(1)
return data
print(function())
print(function())
print(function())
輸出結果為:
[1]
[1, 1]
[1, 1, 1]
參考:http://www.cnblogs.com/congbo/archive/2012/11/20/2777031.html
可變數量參數
1)一個星號的參數
def funcD(a, b, *c):
print a
print b
print "length of c is: %d " %len(c)
print c
funcD(1, 2, 3, 4, 5, 6)
輸出 結果為:
1
2
length of c is: 4
(3, 4, 5, 6)
2)兩個星號的參數
def funcD(a, b, **c):
print a
print b
print "length of c is: %d " %len(c)
print c
print type(c)
funcD(1, 2, c=1,d=2)
輸出結果為:
1
2
length of c is: 2
{'c': 1, 'd': 2}
<type 'dict'>
類
與Java或其他語言中的定義相似,用于封裝數據和操作,便于復用。
模塊
Python中一個模塊可理解為一個文件,其中定義了一些函數和變量。導入模塊后即可復用這些函數和變量。
假設m1.py文件內容如下:
def func(a, b):
print a
print b
則如果需要在m2.py中調用m1.py中的func函數,可以編輯m2內容如下:
import m1
m1.func(1,2)
或者:
from m1 import func
func(1,2)
或者:
from m1 import *
func(1,2)
注意:盡量不要使用import *。
包
把一組相關的模塊放在同一目錄下,再加上__init__.py文件就構成了一個包。