一:python 簡介
(1)Python的由來
Python(英語發音:/ˈpaɪθən/), 是一種面向對象、解釋型計算機程序設計語言,由Guido van Rossum於1989年底發明,第一個公開發行版發行於1991
年。Python語法簡潔而清晰,具有豐富和強大的類庫。它常被昵稱為膠水語言,它能夠把用其他語言制作的各種模塊(尤其是C/C++)很輕松地聯結
在一起。常見的一種應用情形是,使用Python快速生成程序的原型(有時甚至是程序的最終界面),然后對其中有特別要求的部分,用更合適的語言改寫,
比如3D游戲中的圖形渲染模塊,性能要求特別高,就可以用C++重寫。
(2)Python 語法簡介 ---- 類型轉化
int(x [,base ]) 將x轉換為一個整數
long(x [,base ]) 將x轉換為一個長整數
float(x ) 將x轉換到一個浮點數
complex(real [,imag ]) 創建一個復數
str(x ) 將對象 x 轉換為字符串
repr(x ) 將對象 x 轉換為表達式字符串
eval(str ) 用來計算在字符串中的有效Python表達式,並返回一個對象
tuple(s ) 將序列 s 轉換為一個元組
list(s ) 將序列 s 轉換為一個列表
chr(x ) 將一個整數轉換為一個字符
unichr(x ) 將一個整數轉換為Unicode字符
ord(x ) 將一個字符轉換為它的整數值
hex(x ) 將一個整數轉換為一個十六進制字符串
oct(x ) 將一個整數轉換為一個八進制字符串
(3)Python 語法簡介 ---- 類型轉化
s + r 序列連接
s * n , n * s s的 n 次拷貝,n為整數
s % d 字符串格式化(僅字符串)
s[i] 索引
s[i :j ] 切片
x in s , x not in s 從屬關系
for x in s : 迭代
len(s) 長度
min(s) 最小元素
max(s) 最大元素
s[i ] = x 為s[i]重新賦值
s[i :j ] = r 將列表片段重新賦值
del s[i ] 刪除列表中一個元素
del s[i :j ] 刪除列表中一個片段
(4)(3)Python 語法簡介 ---- 類型轉化
x >> y 右移
x & y 按位與
x | y 按位或
x ^ y 按位異或 (exclusive or)
~x 按位翻轉
x + y 加
x - y 減
x * y 乘
x / y 常規除
x // y 地板除
x ** y 乘方 (xy )
x % y 取模 (x mod y )
-x 改變操作數的符號位
+x 什么也不做
~x ~x=-(x+1)
abs(x ) 絕對值
divmod(x ,y ) 返回 (int(x / y ), x % y )
pow(x ,y [,modulo ]) 返回 (x ** y ) x % modulo
round(x ,[n]) 四舍五入,n為小數點位數
x < y 小於
x > y 大於
x == y 等於
x != y 不等於(與<>相同)
x >= y 大於等於
x <= y 小於等於
二:python應用
(1) 文件處理
- filename = raw_input('Enter your file name') #輸入要遍歷讀取的文件路徑及文件名
- file = open(filename,'r')
- done = 0
- while not done:
- aLine = file.readline()
- if(aLine != ''):
- print aLine,
- else:
- done = 1
- file.close() #關閉文件
解釋:
.readline() 和 .readlines() 之間的差異是后者一次讀取整個文件,.readlines() 自動將文件內容分析成一個行的列表,該列表可以由 Python 的 for ... in ... 結構
進行處理。另一方面,.readline() 每次只讀取一行,通常比 .readlines() 慢得多。僅當沒有足夠內存可以一次讀取整個文件時,才應該使用 .readline()。
如果Python文件讀到了文件尾,則會返回一個空字符串‘’,而如果是讀到一個空行的話,則會返回一個‘\n’
Python的readline()方法,每行最后都會加上一個換行字符‘\n’。有時候有的文件最后一行沒有以'\n'結尾時,不返回‘\n’。
readlines()方法返回的是一個列表,而readline()返回一個字符串。
(2)錯誤處理
Python報錯TypeError: 'str' object is not callable
當一般內部函數被用作變量名后可能出現此錯誤。比如:
range=1
for i in range(0,1):
………
就會報這樣的錯誤
這樣的錯會報在for行,但是時間引起的原因卻是在range=1這行,如果兩行相距較遠,怎很難被發現。所以要特別注意不要用內部已有的變量和函數名作自定義變量名。或者str被預先定義了
str=10
for i in range(1,10):
print str(i)
(3) 綜合應用,文件讀取,控制台讀取,時間轉化,編碼轉換
- import time
- from time import strftime
- import sys
- reload(sys)
- sys.setdefaultencoding('utf8')
- # -*- coding: cp936 -*-
- print ("Hello, Python!")
- #!/usr/bin/python
- a = 21
- b = 10
- c = 0
- c = a + b
- print "Line 1 - Value of c is ", c
- c = a - b
- print "Line 2 - Value of c is ", c
- c = a * b
- print "Line 3 - Value of c is ", c
- c = a / b
- print "Line 4 - Value of c is ", c
- c = a % b
- print "Line 5 - Value of c is ", c
- a = 2
- b = 3
- c = a**b
- print "Line 6 - Value of c is ", c
- a = 10
- b = 5
- c = a//b
- print "Line 7 - Value of c is ", c
- # for repeat its
- list = [2, 4, 6, 8]
- sum = 0
- for num in list:
- sum = sum + num
- print("The sum is:", sum)
- # print and Input, assignment
- print("Hello, I'm Python!")
- name = input('What is your name?\n')
- print('Hi, %s.' % name)
- # test for
- fruits = ['Banana', 'Apple', 'Lime']
- loud_fruits = [fruit.upper() for fruit in fruits]
- print(loud_fruits)
- # open, write and read file
- fo = open("./tmp/foo.txt","w+")
- fo.write("Python is a gerat language.\nYeah its great!!\nI am zhang yapeng, who are you?\n")
- t_str = u'我是張燕鵬,您是什么貨色?'
- print(t_str)
- fo.write(t_str)
- fo.close()
- #read and write
- fr = open("./tmp/foo1.txt","r+")
- fw = open("foo_rw.txt","wb")
- done = 0;
- localtime = time.asctime(time.localtime(time.time()))
- print "Local current time : ", localtime
- fw.write(localtime + "\n")
- while not done:
- t_str = fr.readline()
- if(t_str != ''):
- print "Read String is : ", t_str
- fw.write(t_str)
- else:
- done = 1
- fr.close()
- fw.close()
- # test time (import)
- localtime = time.localtime(time.time())
- print "Local current time : ", localtime
- # format the time from time import strftime
- t_time = strftime( '%Y-%m-%d %H:%M:%S', localtime)
- print "formatting local current time : ", t_time
- # design the time by yourself
- year = str(localtime.tm_year)
- mon = str(localtime.tm_mon)
- day = str(localtime.tm_mday)
- hour = str(localtime.tm_hour)
- mins = str(localtime.tm_min)
- sec = str(localtime.tm_sec)
- newtime = u"時間是: " + year + "年" + mon + "月" + day + "日 " + hour + ":" + mins + ":" + sec
- print "Local current time : ", newtime
(4)運行圖:
(5) 總結:
(1)Python是一門入手非常快的語言,處理大數據的好語言,一些規范非常類似於c++語言,例如語法和一些函數命名,文件的打開和讀寫,以及
讀寫方式,非常類似於c++
(2)正如,開頭所寫的 “python是膠水語言,使用Python快速生成程序的原型(有時甚至是程序的最終界面),然后對其中有特別要求的部分,用更合適
的語言改寫,比如3D游戲中的圖形渲染模塊,性能要求特別高,就可以用C++重寫。”
(3)分享一下非常基礎的系統的學習網站
(4)W3CSchool.cc (3)中提到的學習網站是非常基礎的人們課程,要是想深入,具體的內容可以百度。
原文鏈接:http://blog.csdn.net/u010700335/article/details/42025391