python大数据后期教学_大数据处理之道(十分钟学会Python)
一:python 簡介
(1)Python的由來
Python(英語發(fā)音:/?pa?θ?n/), 是一種面向?qū)ο蟆⒔忉屝陀嬎銠C程序設計語言,由Guido van Rossum于1989年底發(fā)明,第一個公開發(fā)行版發(fā)行于1991
年。Python語法簡潔而清晰,具有豐富和強大的類庫。它常被昵稱為膠水語言,它能夠把用其他語言制作的各種模塊(尤其是C/C++)很輕松地聯(lián)結(jié)
在一起。常見的一種應用情形是,使用Python快速生成程序的原型(有時甚至是程序的最終界面),然后對其中有特別要求的部分,用更合適的語言改寫,
比如3D游戲中的圖形渲染模塊,性能要求特別高,就可以用C++重寫。
(2)Python 語法簡介 ---- 類型轉(zhuǎn)化
int(x [,base ]) ? ? ? ? 將x轉(zhuǎn)換為一個整數(shù)
long(x [,base ]) ? ? ? ?將x轉(zhuǎn)換為一個長整數(shù)
float(x ) ? ? ? ? ? ? ? 將x轉(zhuǎn)換到一個浮點數(shù)
complex(real [,imag ]) ?創(chuàng)建一個復數(shù)
str(x ) ? ? ? ? ? ? ? ? 將對象 x 轉(zhuǎn)換為字符串
repr(x ) ? ? ? ? ? ? ? ?將對象 x 轉(zhuǎn)換為表達式字符串
eval(str ) ? ? ? ? ? ? ?用來計算在字符串中的有效Python表達式,并返回一個對象
tuple(s ) ? ? ? ? ? ? ? 將序列 s 轉(zhuǎn)換為一個元組
list(s ) ? ? ? ? ? ? ? ?將序列 s 轉(zhuǎn)換為一個列表
chr(x ) ? ? ? ? ? ? ? ? 將一個整數(shù)轉(zhuǎn)換為一個字符
unichr(x ) ? ? ? ? ? ? ?將一個整數(shù)轉(zhuǎn)換為Unicode字符
ord(x ) ? ? ? ? ? ? ? ? 將一個字符轉(zhuǎn)換為它的整數(shù)值
hex(x ) ? ? ? ? ? ? ? ? 將一個整數(shù)轉(zhuǎn)換為一個十六進制字符串
oct(x )? ? ? ? ? ???將一個整數(shù)轉(zhuǎn)換為一個八進制字符串
(3)Python 語法簡介 ---- 類型轉(zhuǎn)化
s + r ? ? ? ? ? ? ? ? ? 序列連接
s * n , n * s ? ? ? ? ? s的 n 次拷貝,n為整數(shù)
s % d ? ? ? ? ? ? ? ? ? 字符串格式化(僅字符串)
s[i] ? ? ? ? ? ? ? ? ? ?索引
s[i :j ] ? ? ? ? ? ? ? ?切片
x in s , x not in s ? ? 從屬關(guān)系
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 語法簡介 ---- 類型轉(zhuǎn)化
x >> y ? ? ? ? ? ? ? ? ?右移
x & y ? ? ? ? ? ? ? ? ? 按位與
x | y ? ? ? ? ? ? ? ? ? 按位或
x ^ y ? ? ? ? ? ? ? ? ? 按位異或 (exclusive or)
~x ? ? ? ? ? ? ? ? ? ? ?按位翻轉(zhuǎn)
x + y ? ? ? ? ? ? ? ? ? 加
x - y ? ? ? ? ? ? ? ? ? 減
x * y ? ? ? ? ? ? ? ? ? 乘
x / y ? ? ? ? ? ? ? ? ? 常規(guī)除
x // y ? ? ? ? ? ? ? ? ?地板除
x ** y ? ? ? ? ? ? ? ? ?乘方 (xy )
x % y ? ? ? ? ? ? ? ? ? 取模 (x mod y )
-x ? ? ? ? ? ? ? ? ? ? ?改變操作數(shù)的符號位
+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為小數(shù)點位數(shù)
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() #關(guān)閉文件解釋:
.readline() 和 .readlines() 之間的差異是后者一次讀取整個文件,.readlines() 自動將文件內(nèi)容分析成一個行的列表,該列表可以由 Python 的 for ... in ... 結(jié)構(gòu)
進行處理。另一方面,.readline() 每次只讀取一行,通常比 .readlines() 慢得多。僅當沒有足夠內(nèi)存可以一次讀取整個文件時,才應該使用 .readline()。
如果Python文件讀到了文件尾,則會返回一個空字符串‘’,而如果是讀到一個空行的話,則會返回一個‘\n’
Python的readline()方法,每行最后都會加上一個換行字符‘\n’。有時候有的文件最后一行沒有以‘\n‘結(jié)尾時,不返回‘\n’。
readlines()方法返回的是一個列表,而readline()返回一個字符串。
(2)錯誤處理
Python報錯TypeError: ‘str‘ object is not callable
當一般內(nèi)部函數(shù)被用作變量名后可能出現(xiàn)此錯誤。比如:
range=1
for i in range(0,1):
………
就會報這樣的錯誤
這樣的錯會報在for行,但是時間引起的原因卻是在range=1這行,如果兩行相距較遠,怎很難被發(fā)現(xiàn)。所以要特別注意不要用內(nèi)部已有的變量和函數(shù)名作自定義變量名。或者str被預先定義了
str=10
for i in range(1,10):
print str(i)
(3) 綜合應用,文件讀取,控制臺讀取,時間轉(zhuǎn)化,編碼轉(zhuǎn)換
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) 總結(jié):
(1)Python是一門入手非常快的語言,處理大數(shù)據(jù)的好語言,一些規(guī)范非常類似于c++語言,例如語法和一些函數(shù)命名,文件的打開和讀寫,以及
讀寫方式,非常類似于c++
(2)正如,開頭所寫的 “python是膠水語言,使用Python快速生成程序的原型(有時甚至是程序的最終界面),然后對其中有特別要求的部分,用更合適
的語言改寫,比如3D游戲中的圖形渲染模塊,性能要求特別高,就可以用C++重寫。”
(4)W3CSchool.cc (3)中提到的學習網(wǎng)站是非常基礎(chǔ)的人們課程,要是想深入,具體的內(nèi)容可以百度
總結(jié)
以上是生活随笔為你收集整理的python大数据后期教学_大数据处理之道(十分钟学会Python)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Django的视图层
- 下一篇: 毫米波雷达数据处理_基于毫米波雷达的桥梁