(0)文件夾html
高速學Python 和 易犯錯誤(文本處理)
python
高速學會Python(實戰)
post
一:python 簡單介紹學習
(1)Python的由來大數據
Python(英語發音:/ˈpaɪθən/), 是一種面向對象、解釋型計算機程序設計語言,由Guido van Rossum於1989年末發明,第一個公開發行版發行於1991ui
年。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 callableprint 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
(5) 總結:
(1)Python是一門入手很快的語言,處理大數據的好語言,一些規範很相似於c++語言。好比語法和一些函數命名。文件的打開和讀寫。以及
讀寫方式,很相似於c++
(2)正如,開頭所寫的 「python是膠水語言,使用Python高速生成程序的原型(有時甚至是程序的終於界面),而後對當中有特別要求的部分。用更合適
的語言改寫。比如3D遊戲中的圖形渲染模塊。性能要求特別高,就可以用C++重寫。」
(4)W3CSchool.cc (3)中提到的學習站點是很基礎的人們課程。要是想深刻,詳細的內容可以百度