Python十段經典代碼


Python 語法的精妙之處就體現在下面10個例子中。

for - else

十大裝B語法,for-else 絕對算得上囊波灣!不信,請看:

>>> for i in [1,2,3,4]:
    print(i)
else:
    print(i, '我是else')

1
2
3
4
4 我是else

else 的眼里只有 for,只要 for 順利執行完畢,else 就會屁顛兒屁顛兒地跑一遍:

>>> for i in [1,2,3,4]:
    if i > 2:
        print(i)
else:
    print(i, '我是else')

3
4
4 我是else

只有當 for 循環被 break 語句中斷之后,才會跳過 else 語句:

>>> for i in [1,2,3,4]:
    if i>2:
        print(i)
        break
else:
    print(i, '我是else')

3

一顆星(*)和兩顆星(**)

>>> def multi_sum(*args):
    s = 0
    for item in args:
        s += item
    return s

>>> multi_sum(3,4,5)
12

Python 函數允許同時全部或部分使用固定參數、默認參數、單值(一顆星)可變參數、鍵值對(兩顆星)可變參數,使用時必須按照前述順序書寫。

>>> def do_something(name, age, gender='男', *args, **kwds):
    print('姓名:%s,年齡:%d,性別:%s'%(name, age, gender))
    print(args)
    print(kwds)

>>> do_something('xufive', 50, '男', 175, 75, math=99, english=90)
姓名:xufive,年齡:50,性別:男
(175, 75)
{'math': 99, 'english': 90}

三元表達式

>>> y = 5
>>> if y < 0:
    print('y是一個負數')
else:
    print('y是一個非負數')

y是一個非負數

其實,python 是支持三元表達式的,只是稍微怪異了一點,類似於我們山東人講話。比如,山東人最喜歡用倒裝句:打球去吧,要是不下雨的話;下雨,咱就去自習室。翻譯成三元表達式就是:
打球去吧 if 不下雨 else 去自習室
來看看三元表達式具體的使用:

>>> y = 5
>>> print('y是一個負數' if y < 0 else 'y是一個非負數')
y是一個非負數
python 的三元表達式也可以用來賦值:
>>> y = 5
>>> x = -1 if y < 0 else 1
>>> x
1

with - as

with 語句適合一些事先需要准備,事后需要處理的任務,比如,文件操作,需要先打開文件,操作完成后需要關閉文件。如果不使用with,文件操作通常得這樣:

fp = open(r"D:\phyger\Column\temp\mpmap.py", 'r')
try:
    contents = fp.readlines()
finally:
    fp.close()

如果使用 with - as,那就優雅多了:

>>> with open(r"D:\phyger\Column\temp\mpmap.py", 'r') as fp:
    contents = fp.readlines()

列表推導式

求列表各元素的平方,通常應該這樣寫(當然也有其他寫法,比如使用map函數):

>>> a = [1, 2, 3, 4, 5]
>>> result = list()
>>> for i in a:
    result.append(i*i)

>>> result
[1, 4, 9, 16, 25]

如果使用列表推導式,看起來就舒服多了:

>>> a = [1, 2, 3, 4, 5]
>>> result = [i*i for i in a]
>>> result
[1, 4, 9, 16, 25]

事實上,推導式不僅支持列表,也支持字典、集合、元組等對象。

列表索引的各種騷操作

>>> a = [0, 1, 2, 3, 4, 5]
>>> a[2:4]
[2, 3]
>>> a[3:]
[3, 4, 5]
>>> a[1:]
[1, 2, 3, 4, 5]
>>> a[:]
[0, 1, 2, 3, 4, 5]
>>> a[::2]
[0, 2, 4]
>>> a[1::2]
[1, 3, 5]
>>> a[-1]
5
>>> a[-2]
4
>>> a[1:-1]
[1, 2, 3, 4]
>>> a[::-1]
[5, 4, 3, 2, 1, 0]

如果說,這些你都很熟悉,也經常用,那么接下來這個用法,你一定會感覺很神奇:

>>> a = [0, 1, 2, 3, 4, 5]
>>> b = ['a', 'b']
>>> a[2:2] = b
>>> a
[0, 1, 'a', 'b', 2, 3, 4, 5]
>>> a[3:6] = b
>>> a
[0, 1, 'a', 'a', 'b', 4, 5]

lambda函數

下面是一個求和的匿名函數,輸入參數有兩個,x和y,函數體就是x+y,省略了return關鍵字。

>>> lambda x,y: x+y
<function <lambda> at 0x000001B2DE5BD598>
>>> (lambda x,y: x+y)(3,4) # 因為匿名函數沒有名字,使用的時候要用括號把它包起來

匿名函數一般不會單獨使用,而是配合其他方法,為其他方法提供內置的算法或判斷條件。比如,使用排序函數sorted對多維數組或者字典排序時,就可以指定排序規則。

>>> a = [{'name':'B', 'age':50}, {'name':'A', 'age':30}, {'name':'C', 'age':40}]
>>> sorted(a, key=lambda x:x['name']) # 按姓名排序
[{'name': 'A', 'age': 30}, {'name': 'B', 'age': 50}, {'name': 'C', 'age': 40}]
>>> sorted(a, key=lambda x:x['age']) # 按年齡排序
[{'name': 'A', 'age': 30}, {'name': 'C', 'age': 40}, {'name': 'B', 'age': 50}]

再舉一個數組元素求平方的例子,這次用map函數:

>>> a = [1,2,3]
>>> for item in map(lambda x:x*x, a):
    print(item, end=', ')

1, 4, 9, 

yield 以及生成器和迭代器

pyrhon內置了迭代函數 iter,用於生成迭代器,用法如下:

>>> a = [1,2,3]
>>> a_iter = iter(a)
>>> a_iter
<list_iterator object at 0x000001B2DE434BA8>
>>> for i in a_iter:
    print(i, end=', ')

1, 2, 3, 

yield 則是用於構造生成器的。比如,我們要寫一個函數,返回從0到某正整數的所有整數的平方,傳統的代碼寫法是這樣的:

>>> def get_square(n):
    result = list()
    for i in range(n):
        result.append(pow(i,2))
    return result

>>> print(get_square(5))
[0, 1, 4, 9, 16]

但是如果計算1億以內的所有整數的平方,這個函數的內存開銷會非常大,這是 yield 就可以大顯身手了:

>>> def get_square(n):
    for i in range(n):
        yield(pow(i,2))

>>> a = get_square(5)
>>> a
<generator object get_square at 0x000001B2DE5CACF0>
>>> for i in a:
    print(i, end=', ')

0, 1, 4, 9, 16, 

如果再次遍歷,則不會有輸出了。

裝飾器

下面的例子,很好地展示了裝飾器的優勢。

>>> import time
>>> def timer(func):
    def wrapper(*args,**kwds):
        t0 = time.time()
        func(*args,**kwds)
        t1 = time.time()
        print('耗時%0.3f'%(t1-t0,))
    return wrapper

>>> @timer
def do_something(delay):
    print('函數do_something開始')
    time.sleep(delay)
    print('函數do_something結束')


>>> do_something(3)
函數do_something開始
函數do_something結束
耗時3.077

timer() 是我們定義的裝飾器函數,使用@把它附加在任何一個函數(比如do_something)定義之前,就等於把新定義的函數,當成了裝飾器函數的輸入參數。運行 do_something() 函數,可以理解為執行了timer(do_something) 。

巧用斷言assert

所謂斷言,就是聲明表達式的布爾值必須為真的判定,否則將觸發 AssertionError 異常。

>>> def i_want_to_sleep(delay):
    assert(isinstance(delay, (int,float))), '函數參數必須為整數或浮點數'
    print('開始睡覺')
    time.sleep(delay)
    print('睡醒了')


>>> i_want_to_sleep(1.1)
開始睡覺
睡醒了
>>> i_want_to_sleep(2)
開始睡覺
睡醒了
>>> i_want_to_sleep('2')
Traceback (most recent call last):
  File "<pyshell#247>", line 1, in <module>
    i_want_to_sleep('2')
  File "<pyshell#244>", line 2, in i_want_to_sleep
    assert(isinstance(delay, (int,float))), '函數參數必須為整數或浮點數'
AssertionError: 函數參數必須為整數或浮點數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM