




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、Python已經(jīng)是3.1版本了,與時俱進更新教程.2.5或2.6請看上一篇文章。本文適合有Java編程經(jīng)驗的程序員快速熟悉Python本文程序在windows xp+python3.1a1 測試通過.本文提到的idle指python shell,即安裝python后你在菜單看到的IDLE(python gui)在idle里ctrl+n可以打開一個新窗口 ,輸入源碼后ctrl+s可以保存,f5運行程序. 凡打開新窗口即指 ctrl+n的操作.1你好12字符串和數(shù)字 23列表34字典45字符串56條件和循環(huán)語句 67函數(shù)78異常處理89文件處理810類和繼承911包機制912內(nèi)建幫助手冊111你好
2、復制代碼代碼如下:#打開新窗口,輸入:#! /usr/bin/python# -*- coding: utf8 -*-s1= input(lnputyour name:)print(你好,%s % s1)III知識點:* input(某字符串)函數(shù):顯示”某字符串,并等待用戶輸入* print()函數(shù):如何打印.*如何應(yīng)用中文*如何用多行注釋2字符串和數(shù)字但有趣的是,在javascript里我們會理想當然的將字符串和數(shù)字連接,因為是動態(tài)語言嘛但在Python里有點詭異,如下:復制代碼代碼如下:#! /usr/bin/pythona=2b=testc=a+b運行這行程序會出錯,提示你字符串和數(shù)字
3、不能連接,于是只好用內(nèi)置函數(shù)進行轉(zhuǎn)換復制代碼代碼如下:#! /usr/bin/python#運行這行程序會出錯,提示你字符串和數(shù)字不能連接,于是只好用內(nèi)置函數(shù)進行轉(zhuǎn)換a=2b=testc=str(a)+bd=1111e=a+in t(d)#How to print multiply valuesprint (c is %s,e is %i % (c,e)III知識點:*用int和str函數(shù)將字符串和數(shù)字進行轉(zhuǎn)換*打印以#開頭,而不是習慣的II*打印多個參數(shù)的方式3列表復制代碼代碼如下:#! /usr/bin/python# -*- coding:utf8 -*-#列表類似Javascript的
4、數(shù)組,方便易用#定義元組word=a,b,c,d,e,f,g#如何通過索引訪問元組里的元素 a=word2 print (a is: +a)b=word1:3print (b is:)print (b) # index 1 and 2 elements of word.c=word:2print (c is:)print (c) # 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) #
5、 All elements of word.f=word-1print (f is:) print (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.print (i is:)print (i) # Everythingexcept the last two charactersl=le n( word)print (Length o
6、f word is: + str(l)print (Adds new element)word.appe nd(h)print (word)#刪除元素del word0print (word)del word1:3print (word)III知識點:*列表長度是動態(tài)的,可任意添加刪除元素*用索引可以很方便訪問元素,甚至返回一個子列表*更多方法請參考 Python的文檔III4字典復制代碼代碼如下:#! /usr/bin/pythonx=a:aaa,b:bbb,c:12print (xa)print (xb)print (xc)for key in x:print (Key is %s and
7、 value is %s % (key,xkey)知識點:*將他當Java的Map來用即可.Ill5字符串比起C/C+,Python 處理字符串的方式實在太讓人感動了.把字符串當列表來用吧復制代碼代碼如下#! /usr/bin/pythonword=abcdefg a=word2print (a is: +a)b=word1:3print (b is:+b)in dex 1 and2 elements of word.c=word:2print (c is:+c)index 0 and1 elements of word.d=word0:print (d is:+d)Alleleme ntso
8、f word.e=word:2+word2:elements of word.last elements of word.print (e is: +e) # Allf=word-1print (f is: +f) # The g=word-4:-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 except the last two characters l=le
9、 n( word)I!of word is: +中文和英文的字符串長度是否一樣復制代碼代碼如下:#! /usr/bin/python# -*- coding: utf8 -*-s=input(”輸入你的中文名,按回車繼續(xù)”);print (你的名字是:+s)l=le n(s)print (你中文名字的長度是:+str(l)知識點:uni code,所以長度一致類似Java,在python3里所有字符串都是6條件和循環(huán)語句復制代碼代碼如下:#! /usr/bin/python#條件和循環(huán)語句x=int(input(Pleaseenter an integer:)if x0:x=0print (N
10、egative changed to zero)elif x=0:print (Zero)else:print (More)# Loops Lista = cat, win dow, defe nestratefor x in a:print (x, len(x)#知識點:# *條件和循環(huán)語句# *如何得到控制臺輸入7函數(shù)復制代碼代碼如下:#! /usr/bin/python# -*- coding: utf8 -*-def sum(a,b):return a+bfunc = sumr = fun c(5,6)print (r)#提供默認值def add(a,b=2):return a+br=
11、add(1)print (r)r=add(1,5)print (r)一個好用的函數(shù)#! /usr/bin/python# -*- coding: utf8 -*-# The range() functiona =range (1,10)for i in a:print (i)a = range(-2,-11,-3)# The 3rd parameter stands for stepfor i in a:print (i)知識點:Python不用來控制程序結(jié)構(gòu),他強迫你用縮進來寫程序,使代碼清晰定義函數(shù)方便簡單方便好用的range函數(shù)8異常處理復制代碼代碼如下:#! /usr/bin/pyth
12、ons=input(lnputyour age:)if s =:raise Excepti on (I nputmust no be empty.)try:i=i nt(s)except Exception as err:prin t(err)fin ally: # Clea n up actio nprin t(Goodbye!)9文件處理對比Java,python的文本處理再次讓人感動 復制代碼代碼如下:#! /usr/bin/python spath=D:/dow nload/baa.txt for line in f: print(每一行的數(shù)據(jù)是:%s%line) f.close()f
13、=ope n( spath,w)# Opens file for writi ng.Creates this file does ntexist.f.write(First line 1.n)f.writelines(Firstline 2.)f.close()f=ope n( spath,r)# Opens file for reading知識點: 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):
14、self.data.appe nd(x) def addtwice(self, x): self.add(x) self.add(x)# Child extends Baseclass Child(Base): def plus(self,a,b): return a+b oChild =Child() oChild.add(str1) print (oChild.data) print (oChild.plus(2,3) III知識點:* self:類似Java的this參數(shù)III11包機制.請參看以下例子每一個.py文件稱為一個module,module之間可以互相導入 復制代碼代碼如下:
15、# a.pydef add_func(a,b):retur n a+b# b.pyfrom a importadd func# Also can be : import aprint (Importadd funcfrom module a)print (Resultof 1 plus2 is:)print (add_func(1,2)#If using import a , then here should be a.add_funcmodule可以定義在包里面.Python定義包的方式稍微有點古怪,假設(shè)我們有一個pare nt文件夾,該文件夾有一個 child子文件夾.child中有一個
16、module a.py .如何讓 Python知道這個文件層次結(jié)構(gòu)?很簡單,每個目錄都放一個名為_init_.py的文件.該文件內(nèi)容可以為空.這個層次結(jié)構(gòu)如下所示:pare nt -_i ni t_.py -child -_init_.py -a.py b.py那么Python如何找到我們定義的module?在標準包sys中,path屬性記錄了 Python的包路徑.你可以將之打印出來:import sys prin t(sys.path)通常我們可以將 module的包路徑放到環(huán)境變量PYTHONPATH中,該環(huán)境變量會自動添加到module 路徑至U sys.path 中:sys.path屬性.另一種方便的方法是編程中直接指定我們的 復制代碼代碼如下:import sys import os sys.path.appe nd(os.getcwd()+pare ntWchild) prin t(sys.path) from a import add_func print (sys.path) print (Import add_func from module a) print (Resu
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度工業(yè)用地房東租賃協(xié)議
- 二零二五年度供應(yīng)鏈金融資產(chǎn)證券化合作協(xié)議
- 2025年度社區(qū)兒童托管班合伙經(jīng)營合同
- 酒店與電商平臺2025年度線上營銷合作框架協(xié)議
- 二零二五年度互聯(lián)網(wǎng)數(shù)據(jù)保密協(xié)議
- 二零二五年度大數(shù)據(jù)公司高管職位聘用協(xié)議
- 2025年度項目投資價格保密合同范本
- 2025年健身器材企業(yè)股權(quán)轉(zhuǎn)讓項目協(xié)議
- 2025年協(xié)議離婚手續(xù)詳盡解析
- 2025年數(shù)字保密電話機合作協(xié)議書
- 二零二五年度醫(yī)療健康產(chǎn)業(yè)貸款擔保合同
- 2025年安徽醫(yī)學高等??茖W校單招職業(yè)適應(yīng)性測試題庫及答案一套
- 2025年贛西科技職業(yè)學院單招職業(yè)技能測試題庫帶答案
- 急性ST段抬高型心肌梗死溶栓治療專家共識2024解讀
- 電影《哪吒之魔童降世》主題班會
- 2025年湖南科技職業(yè)學院高職單招高職單招英語2016-2024歷年頻考點試題含答案解析
- 2025年鎮(zhèn)江市高等專科學校高職單招高職單招英語2016-2024年參考題庫含答案解析
- 《病例隨訪匯報》課件
- 2025江蘇省沿海開發(fā)集團限公司招聘23人高頻重點提升(共500題)附帶答案詳解
- 2024年09月2024華夏金融租賃有限公司校園招聘筆試歷年參考題庫附帶答案詳解
- 鋰電池技術(shù)研發(fā)生產(chǎn)合同
評論
0/150
提交評論