




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、1 輸出你好#打開新窗口,輸入:#! /usr/bin/python# -*- coding: utf8 -*- s1=input("Input your name:")print("你好,%s" % s1)'''知識(shí)點(diǎn): * input("某字符串")函數(shù):顯示"某字符串",并等待用戶輸入.
2、;* print()函數(shù):如何打印. * 如何應(yīng)用中文 * 如何用多行注釋''' 2 輸出字符串和數(shù)字但有趣的是,在javascript里我們會(huì)理想當(dāng)然的將字符串和數(shù)字連接,因?yàn)槭莿?dòng)態(tài)語(yǔ)言嘛.但在Python里有點(diǎn)詭異,如下:#! /usr/bin/pythona=2b="test"c=a+b運(yùn)行這行程序會(huì)出錯(cuò),提示你字符串和數(shù)字不能連接,于是只好用內(nèi)置函數(shù)進(jìn)行轉(zhuǎn)換#! /u
3、sr/bin/python#運(yùn)行這行程序會(huì)出錯(cuò),提示你字符串和數(shù)字不能連接,于是只好用內(nèi)置函數(shù)進(jìn)行轉(zhuǎn)換a=2b="test"c=str(a)+bd="1111"e=a+int(d)#How to print multiply valuesprint ("c is %s,e is %i" % (c,e)'''知識(shí)點(diǎn): * 用int和str函數(shù)將字符串
4、和數(shù)字進(jìn)行轉(zhuǎn)換 * 打印以#開頭,而不是習(xí)慣的/ * 打印多個(gè)參數(shù)的方式 '''1 / 133 列表#! /usr/bin/python# -*- coding: utf8 -*-#列表類似Javascript的數(shù)組,方便易用#定義元組word='a','b','c','d','e',&
5、#39;f','g'#如何通過(guò)索引訪問(wèn)元組里的元素a=word2print ("a is: "+a)b=word1:3print ("b is: ")print (b) # index 1 and 2 elements of word.c=word:2print ("c is: ")print (c) #
6、;index 0 and 1 elements of word.d=word0:print ("d is: ")print (d) # All elements of word.#元組可以合并e=word:2+word2:print ("e is: ")print (e) # All elements of word.
7、f=word-1print ("f is: ")print (f) # The last elements of word.g=word-4:-2print ("g is: ")print (g) # index 3 and 4 elements of word.h=word-2:print ("h is:&
8、#160;")print (h) # The last two elements.i=word:-2print ("i is: ")print (i) # Everything except the last two charactersl=len(word)print ("Length of word is: "+
9、str(l)print ("Adds new element")word.append('h')print (word)#刪除元素del word0print (word)del word1:3print (word)'''知識(shí)點(diǎn): * 列表長(zhǎng)度是動(dòng)態(tài)的,可任意添加刪除元素. * 用索引可以很方便訪問(wèn)元素,甚至返回一個(gè)子列表
10、; * 更多方法請(qǐng)參考Python的文檔'''4 字典#! /usr/bin/pythonx='a':'aaa','b':'bbb','c':12print (x'a')print (x'b')print (x'c')for key in x: print ("Key
11、is %s and value is %s" % (key,xkey) '''知識(shí)點(diǎn): * 將他當(dāng)Java的Map來(lái)用即可.'''5 字符串比起C/C+,Python處理字符串的方式實(shí)在太讓人感動(dòng)了.把字符串當(dāng)列表來(lái)用吧.#! /usr/bin/pythonword="abcdefg"a=word2print ("a&
12、#160;is: "+a)b=word1:3print ("b is: "+b) # index 1 and 2 elements of word.c=word:2print ("c is: "+c) # index 0 and 1 elements of word.d=word0:print ("
13、d is: "+d) # All elements of word.e=word:2+word2:print ("e is: "+e) # All elements of word.f=word-1print ("f is: "+f) # The last elements of word.g=word-4:
14、-2print ("g is: "+g) # index 3 and 4 elements of word.h=word-2:print ("h is: "+h) # The last two elements.i=word:-2print ("i is: "+i) # Everything
15、0;except the last two charactersl=len(word)print ("Length of word is: "+ str(l)中文和英文的字符串長(zhǎng)度是否一樣?#! /usr/bin/python# -*- coding: utf8 -*- s=input("輸入你的中文名,按回車?yán)^續(xù)");print ("你的名字是 :
16、60;" +s)l=len(s)print ("你中文名字的長(zhǎng)度是:"+str(l)知識(shí)點(diǎn):· 類似Java,在python3里所有字符串都是unicode,所以長(zhǎng)度一致.6 條件和循環(huán)語(yǔ)句#! /usr/bin/python#條件和循環(huán)語(yǔ)句x=int(input("Please enter an integer:")if x<0: x=0 print (&q
17、uot;Negative changed to zero")elif x=0: print ("Zero")else: print ("More")# Loops Lista = 'cat', 'window', 'defenestrate'for x in
18、a: print (x, len(x)#知識(shí)點(diǎn):# * 條件和循環(huán)語(yǔ)句# * 如何得到控制臺(tái)輸入7 函數(shù)#! /usr/bin/python# -*- coding: utf8 -*- def sum(a,b): return a+bfunc = sumr =
19、60;func(5,6)print (r)# 提供默認(rèn)值def add(a,b=2): return a+br=add(1)print (r)r=add(1,5)print (r)一個(gè)好用的函數(shù)#! /usr/bin/python# -*- coding: utf8 -*- # The range() functiona =range (1,10)for i i
20、n a: print (i) a = range(-2,-11,-3) # The 3rd parameter stands for stepfor i in a: print (i)知識(shí)點(diǎn):· Python 不用來(lái)控制程序結(jié)構(gòu),他強(qiáng)迫你用縮進(jìn)來(lái)寫程序,使代碼清晰. · 定義函數(shù)方便簡(jiǎn)單 &
21、#183; 方便好用的range函數(shù)8 異常處理#! /usr/bin/pythons=input("Input your age:")if s ="": raise Exception("Input must no be empty.")try: i=int(s)except Exception as err:
22、160; print(err)finally: # Clean up action print("Goodbye!")9 文件處理對(duì)比Java,python的文本處理再次讓人感動(dòng)#! /usr/bin/pythonspath="D:/download/baa.txt"f=open(spath,"w") # Opens file for writing.Cre
23、ates this file doesn't exist.f.write("First line 1.n")f.writelines("First line 2.")f.close()f=open(spath,"r") # Opens file for readingfor line in f: print("每一
24、行的數(shù)據(jù)是:%s"%line)f.close()知識(shí)點(diǎn):· open的參數(shù):r表示讀,w寫數(shù)據(jù),在寫之前先清空文件內(nèi)容,a打開并附加內(nèi)容. · 打開文件之后記得關(guān)閉10 類和繼承class Base: def _init_(self): self.data = def add(self, x): &
25、#160; self.data.append(x) def addtwice(self, x): self.add(x) self.add(x)# Child extends Baseclass Child(Base):
26、160; def plus(self,a,b): return a+boChild =Child()oChild.add("str1")print (oChild.data)print (oChild.plus(2,3)'''知識(shí)點(diǎn): * self:類似Java的this參數(shù) ''&
27、#39;11 包機(jī)制每一個(gè).py文件稱為一個(gè)module,module之間可以互相導(dǎo)入.請(qǐng)參看以下例子:# a.pydef add_func(a,b): return a+b# b.pyfrom a import add_func # Also can be : import aprint ("Import add_func from module a"
28、;)print ("Result of 1 plus 2 is: ")print (add_func(1,2) # If using "import a" , then here should be "a.add_func"module可以定義在包里面.Python定義包的方式稍微有點(diǎn)古怪,假設(shè)我們有一個(gè)pare
29、nt文件夾,該文件夾有一個(gè)child子文件夾.child中有一個(gè)module a.py . 如何讓Python知道這個(gè)文件層次結(jié)構(gòu)?很簡(jiǎn)單,每個(gè)目錄都放一個(gè)名為_init_.py 的文件.該文件內(nèi)容可以為空.這個(gè)層次結(jié)構(gòu)如下所示:parent -_init_.py -child - _init_.py -a.pyb.py那么Python如何找到我們定義的module?在標(biāo)準(zhǔn)包sys中,path屬性記錄了Python的包路徑.你可以將之打印
30、出來(lái):import sysprint(sys.path)通常我們可以將module的包路徑放到環(huán)境變量PYTHONPATH中,該環(huán)境變量會(huì)自動(dòng)添加到sys.path屬性.另一種方便的方法是編程中直接指定我們的module路徑到sys.path 中:import sysimport ossys.path.append(os.getcwd()+'parentchild')print(sys.path)from a import add_funcprint (sys.path)print ("I
31、mport add_func from module a")print ("Result of 1 plus 2 is: ")print (add_func(1,2)知識(shí)點(diǎn):· 如何定義模塊和包 · 如何將模塊路徑添加到系統(tǒng)路徑,以便python找到它們 · 如何得到當(dāng)前路徑12 內(nèi)建幫助手冊(cè)對(duì)比C+,Java的突出進(jìn)步是內(nèi)建Javadoc機(jī)制,程序員可以通過(guò)閱讀Javadoc了解函數(shù)用法.Python也內(nèi)建了一些方
32、便函數(shù)以便程序員參考.· dir函數(shù): 查看某個(gè)類/對(duì)象的方法. 如果有某個(gè)方法想不起來(lái),請(qǐng)敲dir. 在idle里,試試 dir(list) · help函數(shù): 詳細(xì)的類/對(duì)象介紹. 在idle里, 試試 help(list)1 遍歷文件夾和文件 import osimport os.path# os,os.path里包含大多數(shù)文件訪問(wèn)的函數(shù),所以要先引入它們. # 請(qǐng)按照你的實(shí)際情況修改這個(gè)路徑 rootdir = " d:/download " for paren
33、t, dirnames, filenames in os.walk(rootdir): # case 1: for dirname in dirnames: print ( " parent is: " + parent) &
34、#160; print ( " dirname is: " + dirname) # case 2 for filename in filenames: print ( " parent is: &q
35、uot; + parent) print ( " filename with full path : " + os.path.join(parent, filename)''' 知識(shí)點(diǎn): * os.walk返回一個(gè)三元組.其中dirnames是所有文件夾名字(不包含路徑),filename
36、s是所有文件的名字(不包含路徑).parent表示父目錄. * case1 演示了如何遍歷所有目錄. * case2 演示了如何遍歷所有文件. * os.path.join(dirname,filename) : 將形如"/a/b/c"和"d.java"變成/a/b/c/d.java".''' 2 分割路徑和文件名
37、import os.path# 常用函數(shù)有三種:分隔路徑,找出文件名.找出盤符(windows系統(tǒng)),找出文件的擴(kuò)展名. # 根據(jù)你機(jī)器的實(shí)際情況修改下面參數(shù). spath = " D:/download/repository.7z " # case 1: p,f = os.path.split(spath);print ( " dir is: " + p)print ( " file is: " + f)# case 2: drv,left = os.pat
38、h.splitdrive(spath);print ( " driver is: " + drv)print ( " left is: " + left)# case 3: f,ext = os.path.splitext(spath);print ( " f is: " + f)print ( " ext is: " + ext)''' 知識(shí)點(diǎn):
39、 這三個(gè)函數(shù)都返回二元組. * case1 分隔目錄和文件名 * case2 分隔盤符和文件名 * case3 分隔文件和擴(kuò)展名''' 總結(jié):5個(gè)函數(shù) · os.walk(spath) · os.path.split(spath) · os.path.splitdrive(spath) · os.path.splitext(spath) · os.path.join(path1,path2) 3 復(fù)制文件 import shutilimport osimport os.pathsrc = " d:downloadtestmyfile1.txt " dst = " d:downloadtestmyfile2.txt " dst2 = " d:/download/test/測(cè)試文件夾.txt &quo
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 出售電梯主機(jī)合同范例
- 關(guān)于公司股權(quán)轉(zhuǎn)讓合同范例
- 上海工程吊車租賃合同范例
- 公會(huì)和主播合同范例
- 出售公司土地合同范例
- 產(chǎn)品購(gòu)銷運(yùn)輸合同范例
- 新型滑雪板參數(shù)化設(shè)計(jì)與制備工藝研究
- 基于AISAS模型的南昌市羽毛球愛好者消費(fèi)行為研究
- 個(gè)人股協(xié)議合同范例
- 公司借貸款合同范例
- 二年級(jí)下冊(cè)語(yǔ)文神州謠部編版課件
- 消毒供應(yīng)中心清洗消毒滅菌技術(shù)操作規(guī)范
- BIM在鋼結(jié)構(gòu)工程中應(yīng)用
- 職業(yè)健康安全交底
- GB∕T 7588.1-2020 電梯制造與安裝安全規(guī)范 第1部分:乘客電梯和載貨電梯
- 壓力容器定期(月度)檢查記錄
- 無(wú)線電遙控模型飛機(jī)戴氏教學(xué)法(業(yè)余特技飛行訓(xùn)練)
- 2022年全省公訴業(yè)務(wù)知識(shí)考試參考答案
- 天潤(rùn)化肥事故案例警示會(huì)3.5
- 小學(xué)語(yǔ)文總復(fù)習(xí)指導(dǎo)(課堂PPT)
- 鎮(zhèn)政府(街道辦事處)辦公大樓平面圖
評(píng)論
0/150
提交評(píng)論