資源描述:
《python入門(mén)實(shí)例》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫(kù)。
1、1你好#打開(kāi)新窗口,輸入:#!?/usr/bin/python#?-*-?coding:?utf8?-*-?s1=input("Input?your?name:")print("你好,%s"?%?s1)'''知識(shí)點(diǎn):????*?input("某字符串")函數(shù):顯示"某字符串",并等待用戶(hù)輸入.????*?print()函數(shù):如何打印.????*?如何應(yīng)用中文???*?如何用多行注釋'''????2字符串和數(shù)字但有趣的是,在javascript里我們會(huì)理想當(dāng)然的將字符串和數(shù)字連接,因?yàn)槭莿?dòng)態(tài)語(yǔ)言嘛.但在Py
2、thon里有點(diǎn)詭異,如下:#!?/usr/bin/pythona=2b="test"c=a+b運(yùn)行這行程序會(huì)出錯(cuò),提示你字符串和數(shù)字不能連接,于是只好用內(nèi)置函數(shù)進(jìn)行轉(zhuǎn)換#!?/usr/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):????*?用
3、int和str函數(shù)將字符串和數(shù)字進(jìn)行轉(zhuǎn)換????*?打印以#開(kāi)頭,而不是習(xí)慣的//????*?打印多個(gè)參數(shù)的方式????'''3列表#!?/usr/bin/python#?-*-?coding:?utf8?-*-#列表類(lèi)似Javascript的數(shù)組,方便易用#定義元組word=['a','b','c','d','e','f','g']#如何通過(guò)索引訪問(wèn)元組里的元素a=word[2]print?("a?is:?"+a)b=word[1:3]print?("b?is:?")print?(b)?#?index?
4、1?and?2?elements?of?word.c=word[:2]print?("c?is:?")print?(c)?#?index?0?and?1?elements?of?word.d=word[0:]print?("d?is:?")print?(d)?#?All?elements?of?word.#元組可以合并e=word[:2]+word[2:]print?("e?is:?")print?(e)?#?All?elements?of?word.f=word[-1]print?("f?is:?")p
5、rint?(f)?#?The?last?elements?of?word.g=word[-4:-2]print?("g?is:?")print?(g)?#?index?3?and?4?elements?of?word.h=word[-2:]print?("h?is:?")print?(h)?#?The?last?two?elements.i=word[:-2]print?("i?is:?")print?(i)?#?Everything?except?the?last?two?charactersl=len
6、(word)print?("Length?of?word?is:?"+?str(l))print?("Adds?new?element")word.append('h')print?(word)#刪除元素del?word[0]print?(word)del?word[1:3]print?(word)'''知識(shí)點(diǎn):????*?列表長(zhǎng)度是動(dòng)態(tài)的,可任意添加刪除元素.????*?用索引可以很方便訪問(wèn)元素,甚至返回一個(gè)子列表????*?更多方法請(qǐng)參考Python的文檔'''4字典#!?/usr/bin/pytho
7、nx={'a':'aaa','b':'bbb','c':12}print?(x['a'])print?(x['b'])print?(x['c'])for?key?in?x:????print?("Key?is?%s?and?value?is?%s"?%?(key,x[key]))????'''知識(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
8、=word[2]print?("a?is:?"+a)b=word[1:3]print?("b?is:?"+b)?#?index?1?and?2?elements?of?word.c=word[:2]print?("c?is:?"+c)?#?index?0?and?1?elements?of?word.d=word[0:]print?("d?is:?"+d)?#?All?elements?of?word.e=word[:2]+w