版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Python大数据开发讲义Python大数据开发讲义第1页4数据分析3数据描述1数据搜集2数据整理
大数据处理过程
2Python大数据开发讲义第2页便捷数据获取Python大数据编程Python大数据开发讲义第3页用Python获取数据当地数据怎样获取?文件打开,读写和关闭• 文件打开• 读文件写文件• 文件关闭4Python大数据开发讲义第4页用Python获取数据网络数据怎样获取?抓取网页,解析网页内容urlliburllib2httplib•httplib25Python
3中被urllib.request代替Python
3中被http.client代替Python大数据开发讲义第5页yahoo财经数据/q/cp?s=%5EDJI+Componen
tPython大数据开发讲义第6页利用urllib库获取yahoo财经数据#Filename:dji.pyimporturllibimportredBytes=urllib.request.urlopen('http:///q/cp?s=%5EDJI+Components').read()
dStr=dBytes.decode('GBK')#在python3中urllib.read()返回bytes对象而非str,语句功效是将dBytes转换成Strm=re.findall('<tr><tdclass="yfnc_tabledata1"><b><ahref=".*?">(.*?)</a></b></td><tdclass="yfnc_tabledata1">(.*?)</td>.*?<b>(.*?)</b>.*?</tr>',dStr)ifm: print(m)
print('\n‘)
print(len(m))else: print('notmatch‘)Python大数据开发讲义第7页数据形式• 包含多个字符串(dji)'AXP','AmericanExpressCompany',
'86.40''BA','TheBoeingCompany',
'122.24''CAT','CaterpillarInc.',
'99.44''CSCO','CiscoSystems,Inc.',
'23.78''CVX','ChevronCorporation',
'115.91'– …Python大数据开发讲义第8页便捷网络数据是否能够简单方便而且快速方式获得雅虎财经上各上市公司股票历史数据?File#Filename:
quotes.pyfrommatplotlib.financeimportquotes_historical_yahoo_ochlfromdatetimeimport
dateimportpandasas
pdtoday=
date.today()start=(today.year-1,today.month,
today.day)quotes=quotes_historical_yahoo_ochl('AXP',start,today)df=
pd.DataFrame(quotes)print
dfPython大数据开发讲义第9页便捷网络数据quotes内容日期收盘价开盘价最高价最低价成交量Python大数据开发讲义第10页便捷网络数据自然语言工具包NLTK古腾堡语料库• 布朗语料库• 路透社语料库• 网络和聊天文本• …>>>fromnltk.corpusimport
gutenberg>>>import
nltk>>>print
gutenberg.fileids()[u'austen-emma.txt',u'austen-persuasion.txt',u'austen-sense.txt',u'bible-kjv.txt',u'blake-poems.txt',u'bryant-stories.txt',u'burgess-busterbrown.txt',u'carroll-alice.txt',u'chesterton-ball.txt',u'chesterton-brown.txt',u'chesterton-thursday.txt',u'edgeworth-parents.txt',u'melville-moby_dick.txt',u'milton-paradise.txt',u'shakespeare-caesar.txt',u'shakespeare-hamlet.txt',u'shakespeare-macbeth.txt',u'whitman-leaves.txt']>>>texts=
gutenberg.words('shakespeare-hamlet.txt')[u'[',u'The',u'Tragedie',u'of',u'Hamlet',u'by',
...]Sourcebrown需要先执行nltk.download()下载某一个或多个包,若下载失败,能够在官网(/nltk_data/)
•单独下载后放到当地python目录nltk_data\corpora下Python大数据开发讲义第11页Python大数据开发讲义第12页数据准备Python大数据编程Python大数据开发讲义第13页数据形式30支成份股(dji)股票数据逻辑结构企业代码企业名最近一次成交价美国运通企业(quotes)股票详细数据逻辑结构日期开盘价收盘价最高价最低价成交量Python大数据开发讲义第14页数据整理quotes数据加属性名File#Filename:quotesproc.pyfrommatplotlib.financeimportquotes_historical_yahoo_ochlfromdatetimeimportdateimportpandasas
pdtoday=
date.today()start=(today.year-1,today.month,
today.day)quotes=quotes_historical_yahoo_ochl('AXP',start,
today)fields=['date','open','close','high','low','volume']quotesdf=pd.DataFrame(quotes,columns=fields)print
quotesdfPython大数据开发讲义第15页数据整理dji数据:加属性名codeAXPnamelasttradeBACAT…XOMquotes数据:加属性名dateopenclosehighlowvolume735190.0735191.0735192.0…735551.0Python大数据开发讲义第16页数据整理用1,2,…作为索引quotesdf=pd.DataFrame(quotes,columns=
fields)quotesdf=pd.DataFrame(quotes,index=range(1,len(quotes)+1),columns=
fields)Python大数据开发讲义第17页数据整理假如能够直接用date作为索引,quotes时间能否转换成常规形式(以下列图中效果)?Source>>>fromdatetimeimport
date>>>firstday=
date.fromordinal(735190)>>>lastday=
date.fromordinal(735551)>>>
firstdaydatetime.date(,11,
18)>>>lastdaydatetime.date(,11,
14)Python大数据开发讲义第18页时间序列#Filename:
quotesproc.pyfrommatplotlib.financeimport
quotes_historical_yahoo_ochlfromdatetimeimportdatefromdatetimeimportdatetimeimportpandasas
pdtoday=
date.today()start=(today.year-1,today.month,
today.day)quotes=quotes_historical_yahoo_ochl('AXP',start,today)fields=
['date','open','close','high','low','volume']list1=
[]foriin
range(0,len(quotes)):x=date.fromordinal(int(quotes[i][0]))y=datetime.strftime(x,'%Y-%m-%d')list1.append(y)quotesdf=pd.DataFrame(quotes,index=list1,columns=
fields)quotesdf=quotesdf.drop(['date'],axis=
1)print
quotesdfFile转换成常规时间转换成固定格式删除原date列Python大数据开发讲义第19页创建时间序列>>>importpandasas
pd>>>dates=pd.date_range('1001',
periods=7)>>>
dates<class
'pandas.tseries.index.DatetimeIndex'>[-10-01,...,
-10-07]Length:7,Freq:D,Timezone:
None>>>importnumpyas
np>>>dates=pd.DataFrame(np.random.randn(7,3),index=dates,columns=
list('ABC'))>>>
datesA B C-10-011.302600-1.214708
1.411628-10-02-0.5123432.277474
0.403811-10-03-0.788498-0.217161
0.173284-10-041.042167-0.453329
-2.107163-10-05-1.6280751.663377
0.943582-10-06-0.0910340.335884
2.455431-10-07-0.679055-0.865973
0.246970[7rowsx3
columns]SourcePython大数据开发讲义第20页数据显示Python大数据编程Python大数据开发讲义第21页数据显示djidfquotesdfPython大数据开发讲义第22页数据显示Source>>>
djidf.indexInt64Index([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
20,21,22,23,24,25,26,27,28,29],
dtype='int64')>>>
djidf.columnsIndex([u'code',u'name',u'lasttrade'],
dtype='object')>>>
dijdf.valuesarray([['AXP','AmericanExpressCompany','90.67'],['BA','TheBoeingCompany',
'128.86'],…['XOM','ExxonMobilCorporation','95.09']],
dtype=object)>>>
djidf.describe<boundmethodDataFrame.describe
of
code name lasttrade0 AXP American
Express
Company 90.671…29BAXOMTheBoeing
CompanyExxonMobil
Corporation128.8695.09显示方式:• 显示索引• 显示列名• 显示数据值• 显示数据描述Python大数据开发讲义第23页数据显示Source>>>
quotesdf.indexIndex([u'-11-18',u'-11-19',u'-11-20',
u'-11-21',u'-11-22',u'-11-25',u'-11-26',
u'-11-27',…-04-08',u'-04-09',u'-04-10',u'-04-11',
...],dtype='object')索引格式Python大数据开发讲义第24页数据显示>>>
djidf.head(5)codename0 AXPAmericanExpress
Company1 BA2 CATCSCOCVXTheBoeing
CompanyCaterpillar
Inc.Cisco
Systems,Inc.Chevron
Corporationlasttrade90.67128.86101.3426.32116.32[5rowsx3
columns]>>>
djidf.tail(5)codename
lasttrade25 UTXUnitedTechnologies
Corporation26 V Visa
Inc.27
VZVerizonCommunications
Inc.WMTXOMWal-MartStores
Inc.ExxonMobil
Corporation107.45248.8451.5082.9695.09[5rowsx3
columns]Sourcedf[:5]df[25:]显示方式:• 显示行−
专用方式−
切片查看道琼斯工业股中前5只和后5只股票基础信息?Python大数据开发讲义第25页数据选择Python大数据编程Python大数据开发讲义第26页数据选择选择方式:• 选择行• 选择列• 选择区域• 筛选(条件选择)Python大数据开发讲义第27页数据选择-12-02open85.092126close84.37high85.596624low84.241402volume3620800-12-0383.97698983.7084.41225683.2944103546100-12-0483.30312383.5984.32203182.8579693579700-12-0583.36290683.6384.07515683.2441983677800-12-0684.66368085.0085.15826884.4262772666600[5rowsx5columns]Source>>>
quotesdf[u'-12-02':u'-12-06']选择方式:• 选择行−
切片−
索引美国运通企业12月2日至12月6日间股票交易信息?Python大数据开发讲义第28页数据选择AXPBACAT012…29
XOMName:code,dtype:
object>>>
djidf.code01AXPBACAT2…29
XOMName:code,dtype:objectSource>>>
djidf['code']选择方式:• 选择列−
列名不支持djidf['code','lasttrade']djidf['code':'lasttrade']道琼斯工业股公司代码?Python大数据开发讲义第29页数据选择选择方式:• 行、列−
标签label(loc)codenamelasttrade1 BATheBoeing
Company128.862 CATCaterpillar
Inc.101.343 CSCOCiscoSystems,
Inc.26.324 CVXChevron
Corporation116.325 DDE.I.duPontdeNemoursandCompany70.80[5rowsx3
columns]>>>
djidf.loc[:,['code','lasttrade']]code lasttrade0 AXP1 BA90.67128.862 CAT 101.34…29
XOM
95.09Source>>>
djidf.loc[1:5,]道琼斯工业股中标号是1至5股票信息以及全部股票代码和最近一次交易价?[30rowsx2
columns]Python大数据开发讲义第30页数据选择选择方式:• 行和列区域标签label(loc)• 单个值at3
CSCOcode
lasttrade1 BA 128.862 CAT 101.3426.324 CVX 116.325 DD 70.80[5rowsx2
columns]>>>
djidf.loc[1,'lasttrade']'128.86‘>>>djidf.at[1,'lasttrade']'128.86'Source>>>
djidf.loc[1:5,['code','lasttrade']]道琼斯工业股中标号是1至5股票代码和最近一次交易价?标号是1股票最近一次交易价?Python大数据开发讲义第31页数据选择选择方式:• 行、列和区域− 用iloc(位置)• 取某个值− iat>>>
djidf.loc[1:5,['code','lasttrade']]2 CATCSCOCVXcode
lasttrade1
BA
128.86101.3426.32116.325 DD 70.80SourceSource>>>
djidf.iloc[1:6,[0,2]]code
lasttrade1 BA 128.862 CAT101.343
CSCO26.324 CVX116.325 DD70.80>>>djidf.loc[1,'lasttrade']'128.86'>>>
djidf.at[1,'lasttrade']'128.86'Source>>>
djidf.iloc[1,2]'128.86'>>>djidf.iat[1,2]'128.86'SourcePython大数据开发讲义第32页数据选择>>>quotesdf[quotesdf.index>=
u'-01-01']open close highlow volume-01-0289.92443888.4990.10250688.420751511-01-0388.18637788.7789.10632587.6719983888500-01-0688.73000088.7389.27405288.4134602844700…-03-2889.53155489.7290.81100289.2637633138900...... .........[221rowsx5
columns]>>>quotesdf[(quotesdf.index>=u'-01-01')&(quotesdf.close>=
95)]openclosehighlowvolume-06-0994.53282095.0295.32821694.1052953825200-06-1894.20466295.0195.03982793.5385182454800-07-0395.03149295.2995.38942694.6735581633800[3rowsx5columns]Source美国运通企业股票信息?深入寻找美国运通企业收盘价大于等于95统计?选择方式:• 条件筛选Python大数据开发讲义第33页简单统计与处理Python大数据编程Python大数据开发讲义第34页简单统计与筛选1 TheBoeingCompany8 TheGoldmanSachsGroup,
Inc.10 InternationalBusinessMachines
Corporation16 3M
Company26 Visa
Inc.Name:name,dtype:
objectSource>>>djidf.mean(columns='lasttrade')lasttrade 91.533667dtype:
float64>>>djidf[djidf.lasttrade>=
120].name求道琼斯工业股中30只股票最近一次成交价平均值?股票最近一次成交价大于等于120企业名?Python大数据开发讲义第35页简单统计与筛选Source>>>len(quotesdf[quotesdf.close>
quotesdf.open])131>>>len(quotesdf)-131120统计美国运通公司近一年股票涨和跌分别天数?统计美国运通公司近一年相邻两天收盘价涨跌情况?Source>>>status=
np.sign(np.diff(quotesdf.close))>>>
statusarray([1.,-1.,1.,-1.,1.,1.,1.,1.,-1.,-1.,-1.,1.,
1.,…-1.,-1.,
-1.])>>>status[np.where(status==1.)].size130>>>status[np.where(status==-1.)].size120Python大数据开发讲义第36页排序>>>djidf.sort(columns=
'lasttrade')codename
lasttrade3 CSCO7 GECiscoSystems,Inc.GeneralElectric
Company26.3226.4620PFEPfizer
Inc.30.3411INTCIntel
Corporation33.95…8GSTheGoldmanSachsGroup,
Inc.189.9826VVisa
Inc.248.84[30rowsx3
columns]>>>djidf.sort(columns=
'lasttrade')[27:].name10 InternationalBusinessMachines
Corporation8 TheGoldmanSachsGroup,
Inc.26 Visa
Inc.Name:name,dtype:
objectSource按最近一次成交价对30只道琼斯工业股股票进行排序。依据排序结果列出前三甲企业名。能够添加sort()函数ascending属性控制次序/逆序排序,默认该属性=True,即次序排列DataFramesort()函数已不推荐使用,相同功效推荐使用sort_index()函数Python大数据开发讲义第37页计数统计openclosehighlowvolume-01-0289.92443888.4990.10250688.420751511-01-0388.18637788.7789.10632587.6719983888500…-01-3085.74139385.9186.26704985.3645084259000-01-3184.57785984.3285.20267284.2109064778000[21rowsx5
columns]>>>
len(t)21Source>>>t=quotesdf[(quotesdf.index>='-01-01')&(quotesdf.index<
'-02-01')]>>>
t统计1月份股票开盘天数?Python大数据开发讲义第38页计数统计统计近一年每个月股票开盘天数?File#Filename:quotesmonth.pyimport
time…listtemp=
[]foriin
range(0,len(quotesdf)):temp=time.strptime(quotesdf.index[i],"%Y-%m-%d")listtemp.append(temp.tm_mon)print
listtemptempdf=
quotesdf.copy()tempdf['month']=
listtempprint
tempdf['month'].value_counts()Output:10 237 2212 219 218 216 215 214 213 211 2111 192 19dtype:
int64Python大数据开发讲义第39页GROUPINGPython大数据编程Python大数据开发讲义第40页分组Grouping次序① Splitting② Applying③ CombiningPython大数据开发讲义第41页分组统计近一年每个月股票开盘天数?Source>>>
tempdf.groupby('month').count()openclosehigh
low
volume monthmonth1 21 21 21 21 21 212 19 19 19 19 19 193 21 21 21 21 21 21…10 23 23 23 23 23 2311 20 20 20 20 20 2012 21 21 21 21 21 21[12rowsx6
columns]>>>
tempdf.groupby('month').count().month假如没有特殊处理话则没有month这一列数据假如没有month这一列数据,则此处month需要改成其它属性名如openOutput:month1 212 193 214 215 216 217 228 219 2110 2311 2012 21Name:
month,dtype:
int64Python大数据开发讲义第42页分组统计近一年每个月总成交量?>>>tempdf.groupby('month').sum().volumemonth1104874000276173800371488400484786400…98534140010120822100Sourcemean()min()max()…11679063001267589400Name:volume,dtype:
float64Python大数据开发讲义第43页分组假如更高效统计近一年每个月总成交量?…123101048740007617380071488400120822100Source>>>g=
tempdf.groupby('month')>>>gvolume=
g['volume']>>>print
gvolume.sum()month11679063001267589400Name:volume,dtype:
float64Python大数据开发讲义第44页MERGEPython大数据编程Python大数据开发讲义第45页合并Merge形式Append加行到DataFrameConcat连接pandas对象Join− SQL类型连接Python大数据开发讲义第46页Appendlow
volume-11-1881.95448781.4582.083082
81.2719463104800-11-1981.49086381.5781.985467
81.3820512775000[2rowsx5
columns]>>>q=
quotesdf[u'-01-01':u'-01-05']>>>
qopen close high low volume-01-0289.92443888.4990.10250688.420751
511-01-0388.18637788.7789.10632587.671998
3888500[2rowsx5
columns]>>>
p.append(q)openclosehighlowvolume-11-1881.95448781.4582.08308281.2719463104800-11-1981.49086381.5781.98546781.3820512775000-01-0289.92443888.4990.10250688.420751511-01-0388.18637788.7789.10632587.6719983888500[4rowsx5columns]Source>>>p=
quotesdf[:2]>>>
popen close high把美国运通企业1月1日至1月5日间股票交易信息合并到近一年中前两天股票信息中?Python大数据开发讲义第47页Concat将美国运通企业近一年股票数据中前5个和后5个合并。open
close high low volume
month-11-1881.95448781.4582.08308281.271946
310480011-11-1981.49086381.5781.98546781.382051277500011-11-2082.06231781.3682.51734081.073138351050011-11-2181.74415183.0583.09946481.684795435520011-11-2283.01924982.9583.02914182.475152269030011-11-1192.28000091.7492.59000091.490000271980011-11-1291.16000091.5591.67000091.100000382520011-11-1391.70000091.0791.70000090.940000363790011-11-1491.07000090.6791.24000090.35000023616001111-11-1790.24000090.1390.26000089.650000
2620500[20rowsx6
columns]Source>>>pieces=[tempdf[:5],tempdf[len(tempdf)-5:]]>>>
pd.concat(pieces)Python大数据开发讲义第48页Concat两个不一样逻辑结构对象能否连接?closehighlowmonthopenvolume081.4582.08308281.271946NaN81.9544873104800181.5781.98546781.382051NaN81.4908632775000281.3682.51734081.073138NaN82.0623173510500381.4582.08308281.2719461181.9544873104800481.5781.98546781.3820511181.4908632775000581.3682.51734081.0731381182.0623173510500[6rowsx6
columns]Source>>>piece1=
quotesdf[:3]>>>piece2=
tempdf[:3]>>>pd.concat([piece1,piece2],ignore_index=
True)objsaxisjoinjoin_axeskeyslevelsnamesverify_integrityignore_indexPython大数据开发讲义第49页JoincodemonthvolumeAXPAXPKOKOcodenamelasttradeAXPKOcodenamelasttrademonthvolumeAXPAXPKOKOPython大数据开发讲义第50页Join将美国运通企业和可口可乐企业近一年中每个月交易总量表(包含企业代码)与30只道琼斯成份股股票信息合并。code|name|month|volumesPython大数据开发讲义第51页Joincodename
lasttradevolume
month0AXPAmericanExpress
Company90.1310487400011AXPAmericanExpress
Company90.137617380022AXPAmericanExpress
Company90.137148840033AXPAmericanExpress
Company90.138478640044AXPAmericanExpress
Company9019KOTheCoca-Cola
Company42.92247854800820KOTheCoca-Cola
Company42.92314443100921KOTheCoca-Cola
Company42.925295419001022KOTheCoca-Cola
Company42.922658630001123KOTheCoca-Cola
Company42.9230114450012[24rowsx5
columns]>>>pd.merge(djidf,AKdf,on='code').drop(['lasttrade'],axis=
1)Source>>>pd.merge(djidf,AKdf,on=
'code')Python大数据开发讲义第52页merge函数参数leftrighthowonleft_onright_onleft_indexright_indexsortsuffixescopyPython大数据开发讲义第53页条件Python大数据编程Python大数据开发讲义第54页if
语句File#Filename:
ifpro.pysd1=
3sd2=
3ifsd1==
sd2:print"thesquare'sareais:%d"%
(sd1*sd2)ifexpression
:expr_true_suite语
法条件表示式:• 比较运算符• 组员运算符• 逻辑运算符expressionexpression条件
为True时执行代码块• 代码块必须缩进(通常为4个空格)expr_true_suitePython大数据开发讲义第55页else
语句ifexpression:expr_true_suiteelse:expr_false_suite语
法expression条件为False
时
执
行
代
码块• 代码块必须缩进• else语句不缩进expr_false_suiteFile#Filename:
elsepro.pysd1=int(raw_input('thefirstside:'))sd2=int(raw_input('thesecond
side:'))ifsd1==
sd2:print"thesquare'sareais:%d"%(sd1*sd2)else:print"therectangle'sareais:%d"
%(sd1*sd2)Inputand
Outputthefirstside:4thesecond
side:4thesquare'sarea
is:16Python大数据开发讲义第56页elif
语句ifexpression
:expr_true_suiteelifexpression2:expr2_true_suite::elifexpressionN:exprN_true_suiteelse:none_of_the_above_suite语
法expression2为True时执行代码块expr2_true_suiteexpressionN为
True时执行代码块exprN_true_suitenone_of_the_above_suite是以上全部条件都不满足时执行代码块elsePython大数据开发讲义第57页elif
语句File#Filename:
elifpro.pyk=raw_input('inputtheindexof
shape:')ifk==
'1':print
'circle'elifk==
'2':print
'oval'elifk==
'3':print
'rectangle'elifk==
'4':print'triangle'else:print'youinputtheinvalid
number'InputandOutputinputtheindexof
shape:3rectangleInputandOutputinputtheindexof
shape:8youinputtheinvalid
numberPython大数据开发讲义第58页条件嵌套File#Filename:
ifnestpro.pyk=raw_input('inputtheindexofshape:')ifk==
'1':print
'circle'elifk==
'2':print
'oval'elifk==
'3':sd1=int(raw_input('thefirstside:'))sd2=int(raw_input(:'thesecondside'))ifsd1==
sd2:print"thesquare'sareais:%d"%(sd1*sd2)else:print"therectangle'sareais:%d"%(sd1*sd2)print
'rectangle'elifk==
'4':print
'triangle'else:print'youinputtheinvalid
number'• 同等缩进为同一条件结构InputandOutputinputtheindexofshape:3thefirst
side:3thesecond
side:4therectangle'sareais:12InputandOutputinputtheindexof
shape:2ovalPython大数据开发讲义第59页猜数字游戏•程序随机产生一个0~300间整数,玩家竞猜,系统给出“猜中”、“太大了”或“太小了”提醒。File#Filename:
guessnum1.pyfromrandomimport
randintx=randint(0,
300)print'Pleaseinputanumberbetween
0~300:'digit=
input()ifdigit==x:print
'Bingo!'elifdigit>
x:print'Toolarge,pleasetryagain.'else:print'Toosmall,pleasetry
again.'Python大数据开发讲义第60页RANGE和XRANGEPython大数据编程Python大数据开发讲义第61页range()语
法• 起始值(包含)start• 终值(不包含)end• 步长(不能为0)step• 不包含end值range(start,end,
step=1)• 缺省step值为1range(start,
end)缺省了start值为0,step为1range
(end)range(start,end,step=1)range(start,
end)range
(end)• 生成一个真实列表Source>>>
range(3,11,2)[3,5,7,
9]>>>
range(3,11)[3,4,5,6,7,8,9,
10]>>>
range(11)[0,1,2,3,4,5,6,7,8,9,
10]Python大数据开发讲义第62页xrange()异同range()xrange()语法一样返回列表生成器生成真实列表用多少生成多少>>>foriinxrange(3,11,2):print
iSource>>>xrange(3,11,2)xrange(3,11,
2)>>>print
xrange(3,11,2)xrange(3,11,
2)Output:3579在Python
3.x中不支持xrange()函数,都使用range()函数,其返回值为range对象,而且需要显式调用,常见list(range(10))形式Python大数据开发讲义第63页循环Python大数据编程Python大数据开发讲义第64页while
循环Source>>>sumA=
0>>>j=
1>>>whilej<
10:sumA+=
jj+=
1>>>
sumA45>>>
j10whileexpression:suite_to_repeat语法• 条件表示式当
expression
值为
True时
执
行
suite_to_repeat代码块expressionPython大数据开发讲义第65页for
循环(一)foriter_variniterable_object:suite_to_repeat语
法• 遍历一个数据集内组员• 在列表解析中使用• 生成器表示式中使用能够明确循环次数StringListTupleDictionaryFileiterable_objectPython大数据开发讲义第66页for
循环(二)字符串就是一个iterable_object• range()返回也是iterable_object>>>foriin
range(3,11,2):print
i,357
9Source>>>s=
'python'>>>forcin
s:print
cpythonPython3.x中print函数用法与Python2.x中语句使用方法也有所改变,比如此处变成print(i,end='
')Python大数据开发讲义第67页猜数字游戏• 程序随机产生一个0~300间整数,玩家竞猜,允许猜屡次,系统给出“猜中”、“太大了”或“太小了”提醒。File#Filename:
guessnum2.pyfromrandomimport
randintx=randint(0,
300)forcountin
range(0,5):print'Pleaseinputanumberbetween
0~300:'digit=
input()ifdigit==x
'Bingo!'elifdigit>
x:print'Toolarge,pleasetryagain.'else:print'Toosmall,pleasetry
again.'Python大数据开发讲义第68页循环中BREAK,CONTINUE和ELSEPython大数据编程Python大数据开发讲义第69页break
语句• break语句终止当前循环,转而执行循环之后语句File#Filename:
breakpro.pysumA=
0i=
1whileTrue:sumA+=
ii+=
1ifsumA>
10:breakprint'i=%d,sum=%d'%(i,sumA)Output:i=6,sumA=15Python大数据开发讲义第70页while
循环和break• 输出2-100之间素数File#Filename:prime.pyfrommathimportsqrtj=2whilej
<=100:i=
2k=sqrt(j)while(i<=k
):ifj%i==0:
breaki
=
i+1if(i
>
k):print
j,j+=
1Output:2357111317
192329313741
434753596167
7173798389
97Python大数据开发讲义第71页for
循环和breakFile• 输出2-100之间素数Output:2357111317
192329313741
434753596167
7173798389
97flag=
1#Filename:prime.pyfrommathimportsqrtforiin
range(2,101):k=
int(sqrt(i))forjinrange(2,k+1):ifi%j==
0:flag=
0breakif( flag ):print
i,Python大数据开发讲义第72页continue
语句在while和for循环中,continue语句作用:– 停顿当前循环,重新进入循环– while循环则判断循环条件是否满足– for循环则判断迭代是否已经结束Python大数据开发讲义第73页continue语句循环中break:File#Filename:
breakpro.pysumA=
0i=
1whilei<=5:sumA+=
iifi==
3:breakprint'i=%d,sum=%d'%
(i,sumA)i+=
1循环中continue:File#Filename:
continuepro.pysumA=
0i=
1whilei<=5:sumA+=
ii+=
1ifi==
3:continueprint'i=%d,sum=%d'%(i,sumA)Python大数据开发讲义第74页猜数字游戏(想停就停,非固定次数)• 程序随机产生一个0~300间整数,玩家竞猜,允许玩家自己控制游戏次数,假如猜中系统给出提醒并退出程序,假如猜错给出“太大了”或“太小了”提醒,假如不想继续玩能够退出。File#Filename:guessnum3.pyfromrandomimportrandintx=randint(0,
300)go=
'y'while(go==
'y'):print'Pleaseinputanumberbetween0~300:'digit=
input()ifdigit==x:print
'Bingo!‘breakelifdigit>
x:print'Toolarge,pleasetryagain.'else:print'Toosmall,pleasetry
again.‘print'Ifyoudonotwanttocontinue,inputn,orinputy.'go=
raw_input()print
goelse:print
'Goodbye!'Python大数据开发讲义第75页循环中else语句• 循环中else:– 假如循环代码从break处终止,跳出循环– 正常结束循环,则执行else中代码>>>
k=5>>>foriin
range(1,10):if
k==3:breakelse:print
i9SourcePython大数据开发讲义第76页自定义函数Python大数据编程Python大数据开发讲义第77页函数函数调用之前必须先定义内置函数自定义函数Python大数据开发讲义第78页语
法自定义函数创建deffunction_name([arguments]):"optionaldocumentationstring"function_suiteSource>>>def
addMe2Me(x):'applyoperation+toargument'return
(x+x)Python大数据开发讲义第79页自定义函数调用• 函数名加上函数运算符,
一对小括号– 括号之间是全部可选参数– 即使没有参数,
小括号也不能省略>>>
addMe2Me(3.7)7.4>>>
addMe2Me(5)10>>>
addMe2Me('Python')'PythonPython'SourceTypeError:addMe2Me()takesexactly1argument(0
given)Source>>>
addMe2Me()Traceback(mostrecentcall
last):File"<pyshell#6>",line1,in<module>addMe2Me()Python大数据开发讲义第80页自定义函数• 输出1-100之间素数if
name
==
"
main
":foriin
range(2,101):if
isprime(i):print
i,File#Filename:prime.pyfrommathimportsqrtdef
isprime(x):ifx==1:return
Falsek=
int(sqrt(x))forjin
range(2,k+1):ifx%j==
0:returnFalsereturn
TrueOutput:2357111317
192329313741
434753596167
7173798389
97与Python2.x中一样,用“if
name
=='main
'”来判断是否是在直接运行该.py文件Python大数据开发讲义第81页默认参数(一)• 函数参数能够有一个默认值,
假如提供有默认值,在函数定义中,默认参数以赋值语句形式提供Source>>>deff(x=
True):'''whetherxisacorrectwordornot'''if
x:print'xisacorrect
word'print
'OK'>>>
f()xisacorrectwordOK>>>
f(False)OKPython大数据开发讲义第82页默认参数(二)• 默认参数值能够改变Source>>>deff(x,y=
True):'''xandybothcorrectwordsornot'''if
y:printx,'andybothcorrect'printx,'is
OK'>>>f
(68)68andyboth
correct68is
OK>>>
f(68,False)68is
OKPython大数据开发讲义第83页默认参数(三)• 默认参数普通需要放置在参数列表最终Sourcedeff(y=True,
x):'''xandybothcorrectwordsornot
'''if
y:printx,'andybothcorrect'printx,'is
O
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 等额还款合同模板
- 美甲店装修合同模板
- 2024国有土地出让合同规范
- 上海公寓合租合同模板
- 扶贫项目转借合同模板
- 灯饰工程合同模板
- 个人租房发票合同模板
- 提前订购生猪合同模板
- 婚礼堂装修合同模板
- 办公文具市场环境与对策分析
- DB37∕T 5097-2021 山东省绿色建筑评价标准
- 泛光照明技术标
- 水稻种植管护工作方案管护措施方法
- 隧道监测总结报告
- 果树栽培技术培训心得体会
- 远离流动摊点,拒绝垃圾食品
- 二次供水单位各类制度及应急处理预案
- 三人表决器课堂教学设计 1-5 三人表决器的元器件的检测、电路安装、调试、维修.docx
- 船体结构焊接要求作业指导书
- 汉语拼音字母描红示范(打印版)
- 大脑分区AAL模板
评论
0/150
提交评论