


下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、Python1PythonPython 入門教程 超詳細 1 小時學會 Python為什么使用 Python假設我們有這么一項任務:簡單測試局域網中的電腦是否連通.這些電腦的ip92.168.0.101192.168.0.200.shell.(Linuxbas hWindowsWindows ping ip 的命令依次測試各個機器并得到pingReply fromtime out找,即可知道該機器是否連通.實現:Java 代碼如下:String cmd=cmd.exe ping ;String ipprefix=192.168.10.;int begin=101;int end=200;Pro
2、cess p=nullString cmd=cmd.exe ping ;String ipprefix=192.168.10.;int begin=101;int end=200;Process p=null;for(int i=begin;iend;i+)p= Runtime.getRuntime().exec(cmd+i);String line = null;JavaJavaPython 處理一些簡短程序類似與操作系統的交互編程工作最省時省力.Python 應用場合shellPython快速入門Helloworld安裝完Python5.4)IDLE(Python GUI)Pyth onp
3、rint Hello,world!print Hello,world!print Hello,world!print Hello,world!raw_input(Press enter key to close this window);a.pyF5,你就可以看到Python方式.a.py看到程序結果.PythonJava,這是一個優勢.print 歡迎來到奧運中國!raw_input(Press enter key to close this window);K&R到程序世界的名言.在解釋器中選擇File-New Window 或快捷鍵 Ctrl+N , 打開一個新的編輯器.寫下如下語句:國
4、際化支持我們換一種方式來問候世界.新建一個編輯器并寫如下代碼:在你保存代碼的時候,Python在你保存代碼的時候,Python是否改變文件的字符集,結果如下:# -*- coding: cp936-*-print 歡迎來到奧運中國!raw_input(Press enter key to close this window);將該字符集改為我們更熟悉的形式:# -*- coding: GBK-*-print 歡迎來到奧運中國! # 使用中文的例子raw_input(Press enter key to close this window);程序一樣運行良好.方便易用的計算器用微軟附帶的計算器來
5、計數實在太麻煩了.打開 Python 解釋器,直接進行計算:a=100.0b=201.1c=2343print (a+b+c)/ca=100.0b=201.1c=2343print (a+b+c)/c字符串,ASCIIUNICODE可以如下打印出預定義輸出格式的字符串:print Usage: thingy OPTIONS-hDisplay this usage print Usage: thingy OPTIONS-hDisplay this usage message-H hostnameHostname to connect to字符串是怎么訪問的?請看這個例子:word=abcdefg
6、a=word2print a is: +ab=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 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:-2print
7、 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=len(word)print Length of word is: + str(l)請注意ASCIIUNICODEprint Input your Chinese name:s=raw_input(Press enter to be continued);print Your
8、 name is: l=len(s)print Length of your Chinese name in asc codes is:+str(l); a=unicode(s,GBK)l=len(a)print Im sorry we should use unicode char!Characters number of your Chinese name in unicode is:+str(l);List類似JavaList的數據類型:word=a,b,c,d,e,f,ga=word2print a is: +aword=a,b,c,d,e,f,ga=word2print a is:
9、+ab=word1:3b=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 # All elements of word.f=word-1print f is: print f # The last elements of word.g
10、=word-4:-2print 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:-2print i is: print i # Everything except the last two charactersl=len(word)print b is: print b # index 1 and 2 elements of word.c=word:2print c is: print c # index 0 and 1 elem
11、ents 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.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: print h # The last two elements.i=wor
12、d:-2print i is: print i # Everything except the last two charactersl=len(word)print Length of word is: + print Length of word is: + str(l)print Adds new elementword.append(h)print word條件和循環語句# Multi-way decisionx=int(raw_input(Please enter an integer:)if x0:x=0print Negative changed to zeroelif x=0:
13、print Zeroelse:print More# Loops List# Multi-way decisionx=int(raw_input(Please enter an integer:)if x0:x=0print Negative changed to zeroelif x=0:print Zeroelse:print More# Loops Lista = cat, window, defenestratefor x in a:print x, len(x)如何定義函數# Define and invoke function.# Define and invoke functio
14、n.def sum(a,b):return a+bfunc = sumr = func(5,6)print r# Defines function with default argumentdef add(a,b=2):return def sum(a,b):return a+bfunc = sumr = func(5,6)print r# Defines function with default argumentdef add(a,b=2):return a+br=add(1)print rr=add(1,5)print r并且,介紹一個方便好用的函數:# The range() func
15、tiona =range(5,10)print aa = range(-2,-7)print aa = range(-7,-2)print aa = range(-2,-11,-3) # The 3rd parameter stands for stepprint aI/Ospath=D:/download/baa.txtf=open(spath,w) # Opens file for writing.Creates this file doesnt exist.f.write(First line 1.n)f.writelines(First line 2.)f.close()f=open(
16、spath,r) # Opens file spath=D:/download/baa.txtf=open(spath,w) # Opens file for writing.Creates this file doesnt 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 linef.close()異常處理s=raw_input(Input your age:)if s =:raise Exce
17、ption(Input must no be empty.)try:i=int(s)s=raw_input(Input your age:)if s =:raise Exception(Input must no be empty.)try:i=int(s)except ValueError:print Could not convert data to an integer.except:print Unknown exception!else: # It is useful for code that must be executed if the try clause does not
18、raise an exceptionprint You are %d % i, years oldprint You are %d % i, years oldfinally: # Clean up actionprint Goodbye!finally: # Clean up actionprint Goodbye!類和繼承class Base:definit(self):self.data = def add(self, x):self.data.append(x)def addtwice(self, x):self.add(x)self.add(x)# Child extends Bas
19、eclass Child(Base):def plus(self,a,b):class Base:definit(self):self.data = def add(self, x):self.data.append(x)def addtwice(self, x):self.add(x)self.add(x)# Child extends Baseclass Child(Base):def plus(self,a,b):return a+boChild =Child()oChild.add(str1)print oChild.dataprint oChild.plus(2,3)包機制每一個.p
20、ymodule,module 之間可以互相導入.請參看以下例子:# # 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 print Result of 1 plus 2 is: print add_func(1,2)# If using import a , then here should be a.add_funcmodule.Python包的方式稍微有點古怪,假設我們有一個parent child.child 中有一個modulea.py . 如何讓Python個文件層次結構?很簡單,每個目錄都放一個名為_init_.
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030中國漁具市場經營優勢及未來銷售渠道分析報告
- 2025至2030中國消防維保行業市場發展分析及競爭格局與投資發展戰略報告
- 2025至2030中國海外投資行業市場發展現狀及發展趨勢與產業運行態勢及投資規劃深度研究報告
- 2025至2030中國汽車遮陽簾行業發展趨勢分析與未來投資戰略咨詢研究報告
- 2025至2030中國水復合調味品市場產能預測及發展戰略研究分析報告
- 2025至2030中國每月一次性隱形眼鏡行業發展趨勢分析與未來投資戰略咨詢研究報告
- 2025至2030中國橡膠地磚行業發展趨勢分析與未來投資戰略咨詢研究報告
- 2025-2030中國造紙化學品行業發展狀況及投資動態預測報告
- 2025-2030中國視覺小說市場深度調查與投資策略分析報告
- GB/T 45729-2025激光材料中激活離子濃度測試方法
- 北師大版七年級上冊數學27有理數的乘法課件(2課時)
- 安全生產標準化推進計劃 模板
- 2023年黑龍江省文化和旅游系統事業單位人員招聘筆試模擬試題及答案解析
- 2023年江西新余市數字產業投資發展有限公司招聘筆試題庫含答案解析
- LY/T 3323-2022草原生態修復技術規程
- 部編版六年級語文下冊課件第1課《北京的春節》《臘八粥》
- 涂裝工模擬練習題含答案
- 2023-2024學年河南省永城市小學數學二年級下冊期末評估測試題
- 乳腺疾病的超聲診斷 (超聲科)
- 服務精神:馬里奧特之路
- 《建筑施工安全檢查標準》JGJ59-2011圖解
評論
0/150
提交評論