<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
        問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
        當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

        Python簡(jiǎn)明入門(mén)教程

        來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 14:41:54
        文檔

        Python簡(jiǎn)明入門(mén)教程

        Python簡(jiǎn)明入門(mén)教程:本文實(shí)例講述了Python簡(jiǎn)明入門(mén)教程。分享給大家供大家參考。具體如下: 一、基本概念 1、數(shù) 在Python中有4種類(lèi)型的數(shù)——整數(shù)、長(zhǎng)整數(shù)、浮點(diǎn)數(shù)和復(fù)數(shù)。 (1)2是一個(gè)整數(shù)的例子。 (2)長(zhǎng)整數(shù)不過(guò)是大一些的整數(shù)。 (2)3.23和52.3E-4是浮點(diǎn)數(shù)的例子。
        推薦度:
        導(dǎo)讀Python簡(jiǎn)明入門(mén)教程:本文實(shí)例講述了Python簡(jiǎn)明入門(mén)教程。分享給大家供大家參考。具體如下: 一、基本概念 1、數(shù) 在Python中有4種類(lèi)型的數(shù)——整數(shù)、長(zhǎng)整數(shù)、浮點(diǎn)數(shù)和復(fù)數(shù)。 (1)2是一個(gè)整數(shù)的例子。 (2)長(zhǎng)整數(shù)不過(guò)是大一些的整數(shù)。 (2)3.23和52.3E-4是浮點(diǎn)數(shù)的例子。

        本文實(shí)例講述了Python簡(jiǎn)明入門(mén)教程。分享給大家供大家參考。具體如下:

        一、基本概念

        1、數(shù)

        在Python中有4種類(lèi)型的數(shù)——整數(shù)、長(zhǎng)整數(shù)、浮點(diǎn)數(shù)和復(fù)數(shù)。
        (1)2是一個(gè)整數(shù)的例子。
        (2)長(zhǎng)整數(shù)不過(guò)是大一些的整數(shù)。
        (2)3.23和52.3E-4是浮點(diǎn)數(shù)的例子。E標(biāo)記表示10的冪。在這里,52.3E-4表示52.3 * 10-4。
        (4)(-5+4j)和(2.3-4.6j)是復(fù)數(shù)的例子。

        2、字符串

        (1)使用單引號(hào)(')
        (2)使用雙引號(hào)(")
        (3)使用三引號(hào)('''或""")
        利用三引號(hào),你可以指示一個(gè)多行的字符串。你可以在三引號(hào)中自由的使用單引號(hào)和雙引號(hào)。例如:

        '''This is a multi-line string. This is the first line.
        This is the second line.
        "What's your name?," I asked.
        He said "Bond, James Bond."
        '''
        

        (4)轉(zhuǎn)義符
        (5)自然字符串
        自然字符串通過(guò)給字符串加上前綴r或R來(lái)指定。例如r"Newlines are indicated by "。

        3、邏輯行與物理行
        一個(gè)物理行中使用多于一個(gè)邏輯行,需要使用分號(hào)(;)來(lái)特別地標(biāo)明這種用法。一個(gè)物理行只有一個(gè)邏輯行可不用分號(hào)

        二、控制流

        1、if

        塊中不用大括號(hào),條件后用分號(hào),對(duì)應(yīng)elif和else

        if guess == number:
         print 'Congratulations, you guessed it.' # New block starts here
        elif guess < number:
         print 'No, it is a little higher than that' # Another block
        else:
         print 'No, it is a little lower than that'
        

        2、while

        用分號(hào),可搭配else

        while running:
         guess = int(raw_input('Enter an integer : '))
         if guess == number:
         print 'Congratulations, you guessed it.'
         running = False # this causes the while loop to stop
         elif guess < number:
         print 'No, it is a little higher than that'
         else:
         print 'No, it is a little lower than that'
        else:
         print 'The while loop is over.'
         # Do anything else you want to do here
        

        3、for
        用分號(hào),搭配else

        for i in range(1, 5):
         print i
        else:
         print 'The for loop is over'
        

        4、break和continue
        同C語(yǔ)言

        三、函數(shù)

        1、定義與調(diào)用

        def sayHello():
         print 'Hello World!' # block belonging to the function
        sayHello() # call the function
        

        2、函數(shù)形參
        類(lèi)C語(yǔ)言

        def printMax(a, b):
         if a > b:
         print a, 'is maximum'
         else:
         print b, 'is maximum'
        

        3、局部變量
        加global可申明為全局變量

        4、默認(rèn)參數(shù)值

        def say(message, times = 1):
         print message * times
        

        5、關(guān)鍵參數(shù)
        如果某個(gè)函數(shù)有許多參數(shù),而只想指定其中的一部分,那么可以通過(guò)命名來(lái)為這些參數(shù)賦值——這被稱(chēng)作 關(guān)鍵參數(shù) ——使用名字(關(guān)鍵字)而不是位置來(lái)給函數(shù)指定實(shí)參。這樣做有兩個(gè) 優(yōu)勢(shì) ——一,由于不必?fù)?dān)心參數(shù)的順序,使用函數(shù)變得更加簡(jiǎn)單了。二、假設(shè)其他參數(shù)都有默認(rèn)值,可以只給我們想要的那些參數(shù)賦值。

        def func(a, b=5, c=10):
         print 'a is', a, 'and b is', b, 'and c is', c
        func(3, 7)
        func(25, c=24)
        func(c=50, a=100)
        

        6、return

        四、模塊

        1、使用模塊

        import sys
        print 'The command line arguments are:'
        for i in sys.argv:
         print i
        

        如果想要直接輸入argv變量到程序中(避免在每次使用它時(shí)打sys.),可以使用from sys import argv語(yǔ)句

        2、dir()函數(shù)
        可以使用內(nèi)建的dir函數(shù)來(lái)列出模塊定義的標(biāo)識(shí)符。標(biāo)識(shí)符有函數(shù)、類(lèi)和變量。

        五、數(shù)據(jù)結(jié)構(gòu)

        1、列表

        shoplist = ['apple', 'mango', 'carrot', 'banana']
        print 'I have', len(shoplist),'items to purchase.'
        print 'These items are:', # Notice the comma at end of the line
        for item in shoplist:
         print item,
        print '
        I also have to buy rice.'
        shoplist.append('rice')
        print 'My shopping list is now', shoplist
        print 'I will sort my list now'
        shoplist.sort()
        print 'Sorted shopping list is', shoplist
        print 'The first item I will buy is', shoplist[0]
        olditem = shoplist[0]
        del shoplist[0]
        print 'I bought the', olditem
        print 'My shopping list is now', shoplist
        

        2、元組
        元組和列表十分類(lèi)似,只不過(guò)元組和字符串一樣是不可變的即你不能修改元組。

        zoo = ('wolf', 'elephant', 'penguin')
        print 'Number of animals in the zoo is', len(zoo)
        new_zoo = ('monkey', 'dolphin', zoo)
        print 'Number of animals in the new zoo is', len(new_zoo)
        print 'All animals in new zoo are', new_zoo
        print 'Animals brought from old zoo are', new_zoo[2]
        print 'Last animal brought from old zoo is', new_zoo[2][2]
        

        像一棵樹(shù)

        元組與打印

        age = 22
        name = 'Swaroop'
        print '%s is %d years old' % (name, age)
        print 'Why is %s playing with that python?' % name
        

        3、字典

        類(lèi)似哈希

        ab = { 'Swaroop' : 'swaroopch@byteofpython.info',
         'Larry' : 'larry@wall.org',
         'Matsumoto' : 'matz@ruby-lang.org',
         'Spammer' : 'spammer@hotmail.com'
         }
        print "Swaroop's address is %s" % ab['Swaroop']
        # Adding a key/value pair
        ab['Guido'] = 'guido@python.org'
        # Deleting a key/value pair
        del ab['Spammer']
        print '
        There are %d contacts in the address-book
        ' % len(ab)
        for name, address in ab.items():
         print 'Contact %s at %s' % (name, address)
        if 'Guido' in ab: # OR ab.has_key('Guido')
         print "
        Guido's address is %s" % ab['Guido']
        

        4、序列

        列表、元組和字符串都是序列。序列的兩個(gè)主要特點(diǎn)是索引操作符和切片操作符。

        shoplist = ['apple', 'mango', 'carrot', 'banana']
        # Indexing or 'Subscription' operation
        print 'Item 0 is', shoplist[0]
        print 'Item 1 is', shoplist[1]
        print 'Item -1 is', shoplist[-1]
        print 'Item -2 is', shoplist[-2]
        # Slicing on a list
        print 'Item 1 to 3 is', shoplist[1:3]
        print 'Item 2 to end is', shoplist[2:]
        print 'Item 1 to -1 is', shoplist[1:-1]
        print 'Item start to end is', shoplist[:]
        # Slicing on a string
        name = 'swaroop'
        print 'characters 1 to 3 is', name[1:3]
        print 'characters 2 to end is', name[2:]
        print 'characters 1 to -1 is', name[1:-1]
        print 'characters start to end is', name[:]
        

        5、參考

        當(dāng)你創(chuàng)建一個(gè)對(duì)象并給它賦一個(gè)變量的時(shí)候,這個(gè)變量?jī)H僅參考那個(gè)對(duì)象,而不是表示這個(gè)對(duì)象本身!也就是說(shuō),變量名指向你計(jì)算機(jī)中存儲(chǔ)那個(gè)對(duì)象的內(nèi)存。這被稱(chēng)作名稱(chēng)到對(duì)象的綁定。

        print 'Simple Assignment'
        shoplist = ['apple', 'mango', 'carrot', 'banana']
        mylist = shoplist # mylist is just another name pointing to the same object!
        del shoplist[0]
        print 'shoplist is', shoplist
        print 'mylist is', mylist
        # notice that both shoplist and mylist both print the same list without
        # the 'apple' confirming that they point to the same object
        print 'Copy by making a full slice'
        mylist = shoplist[:] # make a copy by doing a full slice
        del mylist[0] # remove first item
        print 'shoplist is', shoplist
        print 'mylist is', mylist
        # notice that now the two lists are different
        

        6、字符串

        name = 'Swaroop' # This is a string object
        if name.startswith('Swa'):
         print 'Yes, the string starts with "Swa"'
        if 'a' in name:
         print 'Yes, it contains the string "a"'
        if name.find('war') != -1:
         print 'Yes, it contains the string "war"'
        delimiter = '_*_'
        mylist = ['Brazil', 'Russia', 'India', 'China']
        print delimiter.join(mylist) //用delimiter來(lái)連接mylist的字符
        
        

        六、面向?qū)ο蟮木幊?/p>

        1、self

        Python中的self等價(jià)于C++中的self指針和Java、C#中的this參考

        2、創(chuàng)建類(lèi)

        class Person:
         pass # An empty block
        p = Person()
        print p
        
        

        3、對(duì)象的方法

        class Person:
         def sayHi(self):
         print 'Hello, how are you?'
        p = Person()
        p.sayHi()
        
        

        4、初始化

        class Person:
         def __init__(self, name):
         self.name = name
         def sayHi(self):
         print 'Hello, my name is', self.name
        p = Person('Swaroop')
        p.sayHi()
        
        

        5、類(lèi)與對(duì)象的方法

        類(lèi)的變量 由一個(gè)類(lèi)的所有對(duì)象(實(shí)例)共享使用。只有一個(gè)類(lèi)變量的拷貝,所以當(dāng)某個(gè)對(duì)象對(duì)類(lèi)的變量做了改動(dòng)的時(shí)候,這個(gè)改動(dòng)會(huì)反映到所有其他的實(shí)例上。
        對(duì)象的變量 由類(lèi)的每個(gè)對(duì)象/實(shí)例擁有。因此每個(gè)對(duì)象有自己對(duì)這個(gè)域的一份拷貝,即它們不是共享的,在同一個(gè)類(lèi)的不同實(shí)例中,雖然對(duì)象的變量有相同的名稱(chēng),但是是互不相關(guān)的。

        class Person:
         '''Represents a person.'''
         population = 0
         def __init__(self, name):
         '''Initializes the person's data.'''
         self.name = name
         print '(Initializing %s)' % self.name
         # When this person is created, he/she
         # adds to the population
         Person.population += 1
        
        

        population屬于Person類(lèi),因此是一個(gè)類(lèi)的變量。name變量屬于對(duì)象(它使用self賦值)因此是對(duì)象的變量。

        6、繼承

        class SchoolMember:
         '''Represents any school member.'''
         def __init__(self, name, age):
         self.name = name
        class Teacher(SchoolMember):
         '''Represents a teacher.'''
         def __init__(self, name, age, salary):
         SchoolMember.__init__(self, name, age)
         self.salary = salary
        
        

        七、輸入輸出

        1、文件

        f = file('poem.txt', 'w') # open for 'w'riting
        f.write(poem) # write text to file
        f.close() # close the file
        f = file('poem.txt')
        # if no mode is specified, 'r'ead mode is assumed by default
        while True:
         line = f.readline()
         if len(line) == 0: # Zero length indicates EOF
         break
         print line,
         # Notice comma to avoid automatic newline added by Python
        f.close() # close the file
        
        

        2、存儲(chǔ)器

        持久性

        import cPickle as p
        #import pickle as p
        shoplistfile = 'shoplist.data'
        # the name of the file where we will store the object
        shoplist = ['apple', 'mango', 'carrot']
        # Write to the file
        f = file(shoplistfile, 'w')
        p.dump(shoplist, f) # dump the object to a file
        f.close()
        del shoplist # remove the shoplist
        # Read back from the storage
        f = file(shoplistfile)
        storedlist = p.load(f)
        print storedlist
        

        3、控制臺(tái)輸入

        輸入字符串 nID = raw_input("Input your id plz")
        輸入整數(shù) nAge = int(raw_input("input your age plz: "))
        輸入浮點(diǎn)型 fWeight = float(raw_input("input your weight "))
        輸入16進(jìn)制數(shù)據(jù) nHex = int(raw_input('input hex value(like 0x20): '),16)
        輸入8進(jìn)制數(shù)據(jù) nOct = int(raw_input('input oct value(like 020): '),8)

        八、異常

        1、try..except

        import sys
        try:
         s = raw_input('Enter something --> ')
        except EOFError:
         print '
        Why did you do an EOF on me?'
         sys.exit() # exit the program
        except:
         print '
        Some error/exception occurred.'
         # here, we are not exiting the program
        print 'Done'
        
        

        2、引發(fā)異常

        使用raise語(yǔ)句引發(fā)異常。你還得指明錯(cuò)誤/異常的名稱(chēng)和伴隨異常 觸發(fā)的 異常對(duì)象。你可以引發(fā)的錯(cuò)誤或異常應(yīng)該分別是一個(gè)Error或Exception類(lèi)的直接或間接導(dǎo)出類(lèi)。

        class ShortInputException(Exception):
         '''A user-defined exception class.'''
         def __init__(self, length, atleast):
         Exception.__init__(self)
         self.length = length
         self.atleast = atleast
        raise ShortInputException(len(s), 3)
        

        3、try..finnally

        import time
        try:
         f = file('poem.txt')
         while True: # our usual file-reading idiom
         line = f.readline()
         if len(line) == 0:
         break
         time.sleep(2)
         print line,
        finally:
         f.close()
         print 'Cleaning up...closed the file'
        
        

        九、Python標(biāo)準(zhǔn)庫(kù)

        1、sys庫(kù)

        sys模塊包含系統(tǒng)對(duì)應(yīng)的功能。sys.argv列表,它包含命令行參數(shù)。

        2、os庫(kù)

        os.name字符串指示你正在使用的平臺(tái)。比如對(duì)于Windows,它是'nt',而對(duì)于Linux/Unix用戶(hù),它是'posix'。
        os.getcwd()函數(shù)得到當(dāng)前工作目錄,即當(dāng)前Python腳本工作的目錄路徑。
        os.getenv()和os.putenv()函數(shù)分別用來(lái)讀取和設(shè)置環(huán)境變量。
        os.listdir()返回指定目錄下的所有文件和目錄名。
        os.remove()函數(shù)用來(lái)刪除一個(gè)文件。
        os.system()函數(shù)用來(lái)運(yùn)行shell命令。
        os.linesep字符串給出當(dāng)前平臺(tái)使用的行終止符。例如,Windows使用' ',Linux使用' '而Mac使用' '。
        os.path.split()函數(shù)返回一個(gè)路徑的目錄名和文件名。
        >>> os.path.split('/home/swaroop/byte/code/poem.txt')
        ('/home/swaroop/byte/code', 'poem.txt')
        os.path.isfile()和os.path.isdir()函數(shù)分別檢驗(yàn)給出的路徑是一個(gè)文件還是目錄。類(lèi)似地,os.path.existe()函數(shù)用來(lái)檢驗(yàn)給出的路徑是否真地存在。

        希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

        聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        Python簡(jiǎn)明入門(mén)教程

        Python簡(jiǎn)明入門(mén)教程:本文實(shí)例講述了Python簡(jiǎn)明入門(mén)教程。分享給大家供大家參考。具體如下: 一、基本概念 1、數(shù) 在Python中有4種類(lèi)型的數(shù)——整數(shù)、長(zhǎng)整數(shù)、浮點(diǎn)數(shù)和復(fù)數(shù)。 (1)2是一個(gè)整數(shù)的例子。 (2)長(zhǎng)整數(shù)不過(guò)是大一些的整數(shù)。 (2)3.23和52.3E-4是浮點(diǎn)數(shù)的例子。
        推薦度:
        • 熱門(mén)焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門(mén)推薦

        專(zhuān)題
        Top
        主站蜘蛛池模板: 羞羞漫画登录页面免费| 最好看最新的中文字幕免费| 91免费国产视频| 精品久久久久国产免费| 亚洲高清无在码在线电影不卡 | 美女网站免费福利视频| 亚洲第一视频在线观看免费| 亚洲av无码成人精品国产| 国产在线观看免费不卡| 羞羞网站免费观看| 亚洲色偷拍区另类无码专区| 日韩亚洲国产高清免费视频| 四色在线精品免费观看| 亚洲va久久久噜噜噜久久天堂| 国产午夜亚洲精品不卡| 免费成人在线观看| 国产成人无码精品久久久免费| 亚洲色欲一区二区三区在线观看| 免费久久人人爽人人爽av| 免费A级毛片在线播放不收费| 激情吃奶吻胸免费视频xxxx| 亚洲综合色婷婷七月丁香| 久艹视频在线免费观看| 在线播放亚洲第一字幕| 伊人久久免费视频| 久久精品国产精品亚洲艾| 青青草a免费线观a| 日韩亚洲翔田千里在线| 在线亚洲午夜理论AV大片| 久久成人无码国产免费播放| 亚洲国产成人精品激情| 免费成人黄色大片| 37pao成人国产永久免费视频| 亚洲一级二级三级不卡| 黄色成人网站免费无码av| 一级一级毛片免费播放| 亚洲片国产一区一级在线观看| 国产亚洲高清在线精品不卡| 亚洲色婷婷综合久久| 午夜国产精品免费观看| 免费无码又爽又黄又刺激网站|