Python內建函數(4)

來源:互聯網
上載者:User

標籤:most   運算式   ssi   sea   cti   its   mat   語法錯誤   文法   

Python內建函數(4)
1.copyright

互動式提示對象列印許可文本,一個列表
貢獻者和著作權聲明

2.credits
互動式提示對象列印許可文本,一個貢獻者和著作權聲明的列表

3.delattr(object, name)    object -- 對象。name -- 必須是對象的屬性。

class Coordinate:    x = 10    y = -5    z = 0 point1 = Coordinate()  print(‘x = ‘,point1.x)print(‘y = ‘,point1.y)print(‘z = ‘,point1.z) delattr(Coordinate, ‘z‘) print(‘--刪除 z 屬性後--‘)print(‘x = ‘,point1.x)print(‘y = ‘,point1.y) # 觸發錯誤print(‘z = ‘,point1.z)
View Code

運行結果:

(‘x = ‘, 10)(‘y = ‘, -5)(‘z = ‘, 0)--刪除 z 屬性後--(‘x = ‘, 10)(‘y = ‘, -5)Traceback (most recent call last):  File "test.py", line 22, in <module>    print(‘z = ‘,point1.z)AttributeError: Coordinate instance has no attribute ‘z‘

4.dict()

用於建立一個字典

>>>dict()                        # 建立空字典{}>>> dict(a=‘a‘, b=‘b‘, t=‘t‘)     # 傳入關鍵字{‘a‘: ‘a‘, ‘b‘: ‘b‘, ‘t‘: ‘t‘}>>> dict(zip([‘one‘, ‘two‘, ‘three‘], [1, 2, 3]))   # 映射函數方式來構造字典{‘three‘: 3, ‘two‘: 2, ‘one‘: 1} >>> dict([(‘one‘, 1), (‘two‘, 2), (‘three‘, 3)])    # 可迭代對象方式來構造字典{‘three‘: 3, ‘two‘: 2, ‘one‘: 1}>>>

5.divmod(a,b)    a,b都是數字

python divmod() 函數把除數和餘數運算結果結合起來,返回一個包含商和餘數的元組(a // b, a % b)

>>>divmod(7, 2)(3, 1)>>> divmod(8, 2)(4, 0)>>> divmod(1+2j,1+0.5j)((1+0j), 1.5j)

6.enumerate(sequence, [start=0])    sequence -- 一個序列、迭代器或其他支援迭代對象,start -- 下標起始位置

用於將一個可遍曆的資料對象(如列表、元組或字串)組合為一個索引序列,同時列出資料和資料下標,一般用在 for 迴圈當中

>>>seasons = [‘Spring‘, ‘Summer‘, ‘Fall‘, ‘Winter‘]>>>list(enumerate(seasons))[(0, ‘Spring‘), (1, ‘Summer‘), (2, ‘Fall‘), (3, ‘Winter‘)]>>>list(enumerate(seasons, start=1))       # 小標從 1 開始[(1, ‘Spring‘), (2, ‘Summer‘), (3, ‘Fall‘), (4, ‘Winter‘)]

用於for迴圈中

>>>seq = [‘one‘, ‘two‘, ‘three‘]>>>for i, element in enumerate(seq):...    print(i, seq[i])... 0 one1 two2 three>>>

7.eval(expression[, globals[, locals]])

函數用來執行一個字串運算式,並返回運算式的值

  • expression -- 運算式。
  • globals -- 變數範圍,全域命名空間,如果被提供,則必須是一個字典對象。
  • locals -- 變數範圍,局部命名空間,如果被提供,可以是任何映射對象。
>>>x = 7>>> eval( ‘3 * x‘ )21>>> eval(‘pow(2,2)‘)4>>> eval(‘2 + 2‘)4>>> n=81>>> eval("n + 4")85

8.exec(object[, globals[, locals]])

  • object:必選參數,表示需要被指定的Python代碼。它必須是字串或code對象。如果object是一個字串,該字串會先被解析為一組Python語句,然後在執行(除非發生語法錯誤)。如果object是一個code對象,那麼它只是被簡單的執行。
  • globals:選擇性參數,表示全域命名空間(存放全域變數),如果被提供,則必須是一個字典對象。
  • locals:選擇性參數,表示當前局部命名空間(存放局部變數),如果被提供,可以是任何映射對象。如果該參數被忽略,那麼它將會取與globals相同的值
  • exec 傳回值永遠為 None
>>>exec(‘print("Hello Keys")‘)Hello Keys# 單行語句字串#  多行語句字串>>> exec ("""for i in range(5):...     print ("iter time: %d" % i)... """)iter time: 0iter time: 1iter time: 2iter time: 3iter time: 4x = 10expr = """z = 30sum = x + y + zprint(sum)"""def func():    y = 20    exec(expr)    exec(expr, {‘x‘: 1, ‘y‘: 2})    exec(expr, {‘x‘: 1, ‘y‘: 2}, {‘y‘: 3, ‘z‘: 4})    func()

輸出結果:

603334

9.filter(unction, iterable)    function -- 判斷函數.  iterable -- 可迭代對象

filter() 函數用於過濾序列,過濾掉不合格元素,返回一個迭代器對象,如果要轉換為列表,可以使用 list() 來轉換。

該接收兩個參數,第一個為函數,第二個為序列,序列的每個元素作為參數傳遞給函數進行判,然後返回 True 或 False,最後將返回 True 的元素放到新列表中

#!/usr/bin/python3 import mathdef is_sqr(x):    return math.sqrt(x) % 1 == 0 tmplist = filter(is_sqr, range(1, 101))newlist = list(tmplist)print(newlist)

運行結果:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

10.exit(n)

退出程式,預設返回0代表程式運行成功,非0表示異常,也可以自己定義程式退出時的傳回值

 

Python內建函數(4)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.