Python__內建函數

來源:互聯網
上載者:User

標籤:hello   bytes   一段   檢驗   div   內建函數   val   port   als   

#定義一個函數與變數的定義非常相似,對於有名函數,必須通過變數名訪問
def func(x,y,z = 1):
return x + y + z
#匿名函數:1.沒有名字,2.函數體內建return
#匿名函數的應用情境,臨時使用
lambda x,y,z = 1:x + y + z
print(lambda x,y,z = 1:x + y + z)
f = lambda x,y,z = 1:x + y + z
print(f)
f(1,2,3)

#內建函數
#1.abs()
print(all([1,2,‘a‘,None]))#表示可迭代對象取出的每一個值的布爾值都為真
print(all([]))#可迭代對象為空白的列表,表示為True
print(any([]))#表示為False
print(any([‘ ‘,None,False]))#表示為True
print(any([‘‘,None,False]))#表示為False

#bin,oct,hex
print(bin(10))#十進位轉二進位
print(oct(10))#十進位轉八進位
print(hex(10))#十進位轉十六進位

#bytes
#字串本身就是unicode
#unicode------->encode------>bytes
print(‘hello‘.encode(‘utf-8‘))
print(bytes(‘hello‘,encoding = ‘utf-8‘))

#callable是否可以被調用
print(callable(bytes))#True
print(callable(abs))#True

#chr,ord
print(chr(65))#A
print(chr(35))#表示的是#
print(ord(‘#‘))#表示的是35

s1 = frozenset({1,2,3,4})#表示的是不可變集合

import sys
sys.path ##path表示來自於sys名稱空間的名字
print(dir(sys))#查看sys有哪些屬性

#divmod
print(divmod(10,3))#結果表示的是除數和商

#enumerate
l = [‘a‘,‘b‘,‘c‘]
res = enumerate(l)
for index,item in res:
print(index,item)

#hash(),表示的是檢驗一段字串,會得到hash值

#help()查看協助資訊
print(help())#在函數中寫注釋資訊,可通過help查看

#id:是python解譯器實現的功能,並不是真實的記憶體位址

#isinstance
x = 1
print(isinstance(x,int))#判斷x是否是int的一個執行個體

#pow
print(pow(10,2))#表示的是10**2
print(pow(3,2,2))#表示的是3**2%2

#repr:將對象轉成字串
print(type(repr(1)))#解譯器內部調用

#reversed
l = [1,‘a‘,2,‘c‘]
print(reversed(l))


#round
print(round(3.567,2))#表示的保留兩位小數

#slice
l = [1,2,3,4,5,6]
print(l[0:4:2])
s = slice(0,4,2)
print(l[s])#可以給多個對象使用

#sorted
l = [1,10,4,3,-1]
print(sorted(l,reverse = True))

#zip:拉鏈(找兩個一一對應的部分)
s = ‘hello‘
l = [1,2,3,4,5]
print(zip(s,l))
res = zip(s,l)#為迭代器
print(list(res))

#__import__
import sys
m_name = input(‘module>>: ‘)
if m_name == ‘sys‘:
m = __import__(m_name)
print(m)
print(m.path)



salaries = {
‘egon‘:3000,
‘alex‘:100000,
‘wupeiqi‘:1000,
‘yuanhao‘:2000
}
# print(list(zip(salaries.values(),salaries.keys())))

# print(max(list(zip(salaries.values(),salaries.keys()))))

# print(max(salaries,key = lambda name:salaries[name]))


##filter,map,reduce
names = [‘alex‘,‘wupeiqi‘,‘yuanhao‘,‘egon‘]
# res = map(lambda x:x + ‘_SB‘,names)
# print(list(res))

#從functools中匯入reduce模組
from functools import reduce
# print(reduce(lambda x,y:x + y,range(101)))


def my_map(seq):
for item in seq:
item = item + ‘_SB‘
yield item
res1 = my_map(names)
# print(next(res1))

def my_map(func,seq):
for item in seq:
yield func(item)
# res1 = my_map(lambda x:x + ‘_SB‘,names)
# print(next(res1))

##filter函數
names = [‘alex_SB‘,‘wupeiqi‘,‘yuanhao‘,‘egon‘]
# print(list(filter(lambda name:name.endswith(‘SB‘),names)))

##eval與exec
# cmd = ‘print(x)‘
# x = 1
# eval(cmd)
# eval(cmd,{},{})#第一個大括弧表示的是全域範圍,第二個大括弧表示的是局部範圍
# eval(cmd,{‘x‘:0},{‘y‘:10000})

s = ‘for i in range(10):print(i)‘
code = compile(s,‘‘,‘exec‘)
exec(code)

Python__內建函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.