![python入門基礎(chǔ)代碼_第1頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/29/dd588161-7b88-44e8-83f3-e21e28b73b30/dd588161-7b88-44e8-83f3-e21e28b73b301.gif)
![python入門基礎(chǔ)代碼_第2頁](http://file3.renrendoc.com/fileroot_temp3/2021-12/29/dd588161-7b88-44e8-83f3-e21e28b73b30/dd588161-7b88-44e8-83f3-e21e28b73b302.gif)
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、python 入門基礎(chǔ)代碼#查找 index 函數(shù)的幫助 help(str.index) #for 循環(huán)和 break 語句 from math import sqrt for i in range(2,101): flag=1 k=int(sqrt(i)for j in range(2,k 1): if i%j=0: flag=0 break if(flag): print i #continue語句 ,滿足條件跳出 continue 后面的語句 sumA=0 i=1 while i=5:sumA =i i =1 if i=3:continueprint i=%d,sum=%d%(i,sum
2、A)#循環(huán)中的 else 語句k=5for i in range(1,10):if k=3:breakelse:print i#自定義函數(shù) /素材 1-100 間素?cái)?shù) from math import sqrt defisprime(x):if x=1:return False k=int(sqrt(x) for j in range(2,k 1): if x%j=0:return False return True for i in range(2,101): if isprime(i):print idef f(x,y=True): # 默認(rèn)參數(shù)要放在參數(shù)列表的最后x and y both
3、 correct words or notif y:print x,and y both correctprint x,is Okf(68)f(68,False) #傳遞函數(shù) def addme2me(x): return(x x) def self(f,y):print f(y)self(addme2me,2.2)#lambda 函數(shù) my_add=lambda x,y:x y my_add(5,6)#數(shù)據(jù)獲取與表示 f=open(rE:360Downloadsfirstpro.txt,w) # 新建一個(gè)文件 f.write(hello,world) #f 為對象,write 寫入文件 f.
4、close()f=open(rE:360Downloadsfirstpro.txt,r) #r 代表讀出 p1=f.read(5)#5 代表讀出的字節(jié)數(shù)p2=f.read()print p1,p2f.closef1=open(rE:360Downloadscompanies.txt) cname=f1.readlines()for i in range(0,len(cname):cnamei=str(i 1) cnameif1.close f2=open(rE:360Downloadsscompanies.txt,w)f2.writelines(cname)f2.close()#網(wǎng)絡(luò)數(shù)據(jù)獲取i
5、mport urllibr=urllib.urlopen(http:/ 值比較 atuple=(ba,126.4) btuple=atuplebtuple is not atuple# 對象身份比較(86.40122.64) and (applebanana)#布爾運(yùn)算 #序列類型運(yùn)算符 week=mondy,tuesday,wednesday,thursday,firday,satuday,sundayprint week1,week-2,n,week1:4,n,week:6,n,week:-1# 序列值的序號是從 0 開始到 N-1 或者從 -1 到 Napple*3ba in (ba,t
6、he boeingcompany) #序列類型轉(zhuǎn)換工廠函數(shù)list(hello,world)# 將字符串轉(zhuǎn)成列表 tuple(hello,world)# 將字符串轉(zhuǎn)成元組 #字符串 s1=helloabworld!s1 #三引號可以保持里面字符串的原貌 s2=rd:pythona.pys2ccode=axp,ba,cat,csco,cvxcprice=86.40,122.64,99.44,23.78,115.91 for i in range(5):print %d%8s:%8s %(i,ccodei,cpricei) #%8s指的占 8 個(gè) 字符print i get %d%! %32 #
7、兩個(gè) % 會(huì)留下一個(gè) astr=what do youthink of this saying no pain,no gain? tempstr=astr.split()1 # 為轉(zhuǎn)義字符 if tempstr.istitle(): print it is title formatelse:print it is not title format八進(jìn)制數(shù) 000 代表的字符 t 代表橫向制表符 print astr# 列表jscores=9,9,8.5,10,7,8,8,9,8,10ascore=9jscores.sort()jscores.pop() # 去掉最高分 jscores.pop(
8、0) # 去掉最低分jscores.append(ascore) # 合并avescore=sum(jscores)/len(jscores) printavescoreweek=monday,tuesday,wednesday,thursday,firdayweekend=saturday,sundayweek.extend(weekend)#extend 合并函數(shù)for i,j in enumerate(week):#enumerate 函數(shù) 產(chǎn)生從 0 開始 的序列print i 1,jnumlist=3,11,5,8,16,1 numlist.sort(reverse=True) #
9、按逆序排列 numlistfruitlist=apple,banana,pear,lemon,avocadofruitlist.sort(key=len) fruitlist # 按字符長度排序#列表解析x*2 for x in range(10)x*2 for x in range(10) if x*22,darray,earray) #where 函數(shù)def fun(x,y):return (x 1)*(y 1) arr=fromfunction(fun,(9,9) #fromfunction 函數(shù) arr#ufunc 函數(shù)能對數(shù)組中每個(gè)元素進(jìn)行操作, 包括 add 等函數(shù)import n
10、umpy as npa=np.arange(1,5) b=np.arange(2,6) np.add(a,b)np.add.accumulate(2,3,8) np.multiply.accumulate(2,3,8)#series#series 類似于一維數(shù)組的對象,由數(shù)據(jù)和索引組成 from pandasimport Series import pandas as pd aser=pd.Series(1,2.0,a)bser=pd.Series(apple,peach,lemon,index=1,2,3) bser.indexbser.values asera import numpy a
11、s np np.exp(aser)#series 數(shù)據(jù)對齊 data=axp:86,csco:122,ba:94sindex=axp,csco,aapl aser=pd.Series(data,index=sindex)aser pd.isnull(aser)#series 的 name 屬性 ==volumeaser#dataframe 表結(jié)構(gòu)#大致可以看成共享一個(gè) index 的 series 的集合data=name:wangdachui,linlin,niuyun,pay:4000,5000,6000frame=pd.Data
12、Frame(data)framenameframe.payframe.ix2 # 索引所對應(yīng)的數(shù)據(jù)#dataframe 對象的修改和刪除framename=admin # 對 name 下的所有元素進(jìn)行更改del =no#便捷數(shù)據(jù)獲取#便捷網(wǎng)絡(luò)數(shù)據(jù) 雅虎財(cái)經(jīng)from matplotlib.finance import quotes_historical_yahoofrom datetime import dateimport pandas as pdtoday=date.today() start=(today.year-1,today
13、.month,today.day)quotes=quotes_historical_yahoo(AXP,start,today)df=pd.DataFrame(quotes) print df#自然語言工具包 NLTK from nltk.corpus import gutenberg importnltk print gutenberg.fileids() #數(shù)據(jù)準(zhǔn)備from matplotlib.finance import quotes_historical_yahoo fromdatetime import date import pandas as pd today=date.tod
14、ay()start=(today.year-1,today.month,today.day)quotes=quotes_historical_yahoo(AXP,start,today)fields=date,open,close,high,low,volumequotesdf=pd.DataFrame(quotes,columns=fields) # 通過 columns數(shù)據(jù)加屬性名quotesdf=pd.DataFrame(quotes,index=range(1,len(quotes)1,columns=fields) print quotesdffrom datetime import
15、 datefrom datetime import datetimey=datetime.strftime(x,%Y-%m-%d) # 將日期轉(zhuǎn)換成固定格 式import pandas as pddates=pd.date_range(20141001,periods=7) # 自己創(chuàng)建時(shí) 間序列dates#數(shù)據(jù)顯示 djidf.index djidf.columns djidf.values djidf.describe# 顯示數(shù)據(jù)描述 djidf.head(5)# 顯示前五行數(shù)據(jù)djidf.tail(5)#數(shù)據(jù)選擇quotesdfu2013-12-02:u2013-12-06 #dataf
16、rame 可以使 用索引選擇djidfcodedjidf.loc1:5, # 通過 loc 通過標(biāo)簽選擇數(shù)據(jù), 第一個(gè)參數(shù)是行 標(biāo)簽,第二個(gè)參數(shù)是列標(biāo)簽djidf.loc:,code,lasttrade # 前面這個(gè):需要djidf.loc1:6,0,2 # 通過的行位置,列位置選擇標(biāo)簽quotesdfquotesdf.index=u2014-01-01quotesdf(quotesdf.index=u2014-01-01)&(quotesdf.close =95)# 條件篩選#簡單統(tǒng)計(jì)預(yù)處理 djidf.mean(columns=lasttrade)# 最近一次成交價(jià)的平均值 dji
17、dfdjidf.lasttrade=120.name# 最近一次成交價(jià)大于等于 120 的公司名 len(quotesdfquotesdf.closequotesdf.open) #股票漲的天 數(shù)len(quotesdf)-131status=np.sign(np.diff(quotesdf.close) #np.diff相鄰兩天數(shù)據(jù)的差statusnp.where(status=1.).sizedjidf.sort(columns=lasttrade)27:.name# 排序結(jié)果列出前 三甲的公司名 ,一共是 30 條記錄,所以是 27 :t=quotesdf(quotesdf.index=
18、u2014-01-01)&(quotesdf.ind exlen(t) # 統(tǒng)計(jì) 2014 年 1 月份的股票開盤天數(shù) #分組tempdf.groupby(month).count().month # 每個(gè)月的股票開 盤天數(shù)tempdf.groupby(month).sum().volume # 每個(gè)月的股票總成 交量g=tempdf.groupby(month) gvolume=gvolume printgvolume.sum()#合并#append 追加 縱向 p=quotesdf:2q=quotesdfu2014-01-01:u2014-01-05 p.append(q)#concat 連接pieces=tempdf:5,tempdflen(tempdf)-4: pd.concat(pieces)#兩個(gè)不同邏輯結(jié)構(gòu)的對象能否連接縱向piece1=quotesdf:3piece2=tempdf:3 pd.concat(piece1,piece2,ignore_index=True)#join 連接 橫向pd.merge(djidf,akdf,on=code)pd.merge.(djidf,akdf,on=code).dro
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度演員廣告代言合同
- 2025年度醫(yī)療機(jī)構(gòu)藥品采購委托代購合同
- 農(nóng)業(yè)綠色發(fā)展行動(dòng)計(jì)劃
- 養(yǎng)老院合同協(xié)議書
- 用戶體驗(yàn)設(shè)計(jì)原則及實(shí)踐
- 簡易買賣合同
- 云計(jì)算在企業(yè)資源規(guī)劃中的應(yīng)用
- 三農(nóng)產(chǎn)品追溯系統(tǒng)建設(shè)方案
- 模具設(shè)計(jì)與制造技術(shù)作業(yè)指導(dǎo)書
- 建房勞務(wù)人工的合同
- 數(shù)學(xué)-河南省三門峽市2024-2025學(xué)年高二上學(xué)期1月期末調(diào)研考試試題和答案
- 二零二五版電力設(shè)施維修保養(yǎng)合同協(xié)議3篇
- 最經(jīng)典凈水廠施工組織設(shè)計(jì)
- VDA6.3過程審核報(bào)告
- 2025年春新人教版數(shù)學(xué)七年級下冊教學(xué)課件
- 《心臟血管的解剖》課件
- 心肺復(fù)蘇課件2024
- 2024-2030年中國并購基金行業(yè)發(fā)展前景預(yù)測及投資策略研究報(bào)告
- 河道清淤安全培訓(xùn)課件
- 2024年湖南商務(wù)職業(yè)技術(shù)學(xué)院單招職業(yè)適應(yīng)性測試題庫帶答案
- 7.3.1印度(第1課時(shí))七年級地理下冊(人教版)
評論
0/150
提交評論