欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python3.5常見內置方法參數(shù)用法實例詳解

 更新時間:2019年04月29日 12:14:47   作者:loveliuzz  
這篇文章主要介紹了Python3.5常見內置方法參數(shù)用法,結合實例形式詳細分析了Python常見的內置方法及參數(shù)使用技巧,需要的朋友可以參考下

本文實例講述了Python3.5常見內置方法參數(shù)用法。分享給大家供大家參考,具體如下:

Python的內置方法參數(shù)詳解網(wǎng)站為:https://docs.python.org/3/library/functions.html?highlight=built#ascii

1、abs(x):返回一個數(shù)字的絕對值。參數(shù)可以是整數(shù)或浮點數(shù)。如果參數(shù)是一個復數(shù),則返回它的大小。

#內置函數(shù)abs()
print(abs(-2))
print(abs(4.5))
print(abs(0.1+7j))

運行結果:

2
4.5
7.000714249274855

2、all(Iterable):如果可迭代的對象的元素全部為真(即:非零)或可迭代對象為空,返回True,否則返回False

#內置函數(shù)all()
print(all([-1,0,7.5]))
print(all([9,-1.6,12]))
print(all([]))

運行結果:

False
True
True

3、any(Iterable):如果可迭代的對象的元素中有一個為真(即:非零),返回True,可迭代對象的元素全部為零(全部為假)或者可迭代對象為空時則返回False。

#內置函數(shù)any()
print(any([-1,0,7.5]))
print(any([0,0,0]))
print(any([]))

運行結果:

True
False
False

4、ascii(object):將內存對象變成可打印的字符串的形式。

#內置函數(shù)ascii(object)
a = ascii([1,2,'你好'])
print(type(a),[a])

運行結果:

<class 'str'> ["[1, 2, '\\u4f60\\u597d']"]

5、bin(x):將十進制整數(shù)轉換成二進制

#內置函數(shù)bin()
print(bin(0))
print(bin(2))
print(bin(8))
print(bin(255))

運行結果:

0b0
0b10
0b1000
0b11111111

6、bool([x]):返回一個bool值,0:返回False,非0:返回True;空列表:返回False

#內置函數(shù)bool()
print(bool(0))
print(bool(1))
print(bool([]))
print(bool([3]))

運行結果:

False
True
False
True

7、bytearray():返回一個新的字節(jié)數(shù)組,可修改的二進制字節(jié)格式。

#內置函數(shù)bytearray()
a = bytes("abcde",encoding='utf-8')
print(a)

b = bytearray("abcde",encoding='utf-8')
print(b)
b[1] = 100
print(b)

運行結果:

b'abcde'
bytearray(b'abcde')
bytearray(b'adcde')

8、callable(object):判斷是否可調用(函數(shù)和類可以調用),列表等不可調用

#內置函數(shù)callable
def nice():
 pass
print(callable(nice))
print(callable([]))

運行結果:

True
False

9、chr(i):返回數(shù)字對應的ASCII碼對應表;相反地,ord():返回ASCII碼對應的數(shù)字

#內置函數(shù)chr()與ord()
print(chr(98))
print(ord('c'))

運行結果:

b
99

10、compile():將字符串編譯成可執(zhí)行的代碼

#內置函數(shù)compile
code = "for i in range(10):print(i)"
print(compile(code,'','exec'))
exec(code)

運行結果:

<code object <module> at 0x008BF700, file "", line 1>
0
1
2
3
4
5
6
7
8
9

11、dir():可以查方法

#內置函數(shù)dir
s = []
print(dir(s))

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
 '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__','__mul__', '__ne__', '__new__',
 '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__',
'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

12、divmod(a,b):返回商和余數(shù)

#內置函數(shù)divmod()
print(divmod(5,3))
print(divmod(8,9))

運行結果:

(1, 2)
(0, 8)

13、enumerate():是枚舉、列舉的意思。
對于一個可迭代的(iterable)/可遍歷的對象(如列表、字符串),enumerate將其組成一個索引序列,

利用它可以同時獲得索引和值;enumerate多用于在for循環(huán)中得到計數(shù)。

#內置函數(shù)enumerate
list = ['歡','迎','你']
for index,item in enumerate(list):
 print(index,item)

運行結果:

0 歡
1 迎
2 你

13、eval():將字符串str當成有效的表達式來求值并返回計算結果。

#內置函數(shù)eval()
#字符串轉換成列表
a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
print(type(a))
b = eval(a)
print(b)
print(type(b))
#字符串轉換成字典
a = "{1: 'a', 2: 'b'}"
print(type(a))
b = eval(a)
print(b)
print(type(b))
#字符串轉換成元組
a = "([1,2], [3,4], [5,6], [7,8], (9,0))"
print(type(a))
b = eval(a)
print(b)
print(type(b))

運行結果:

<class 'str'>
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
<class 'list'>
<class 'str'>
{1: 'a', 2: 'b'}
<class 'dict'>
<class 'str'>
([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))
<class 'tuple'>

14、filter(function,iterable):過濾序列。

匿名函數(shù)用完釋放,不重復使用。

#匿名函數(shù)
calc = lambda n:print(n)
calc(3)
res = filter(lambda n:n>5,range(10))
for i in res:
 print(i)

運行結果:

3
6
7
8
9

15、map():可以把一個 list 轉換為另一個 list,只需要傳入轉換函數(shù).

res = map(lambda n:n*n,range(5))  #等價于列表生成式[lambda i:i*i for i in range(5)]
for i in res:
 print(i)

運行結果:

0
1
4
9
16

16、reduce():python 3.0.0.0以后, reduce已經(jīng)不在built-in function里了, 要用它就得from functools import reduce.

它可以通過傳給reduce中的函數(shù)(必須是二元函數(shù))依次對數(shù)據(jù)集中的數(shù)據(jù)進行操作。

凡是要對一個集合進行操作的,并且要有一個統(tǒng)計結果的,能夠用循環(huán)或者遞歸方式解決的問題,一般情況下都可以用reduce方式實現(xiàn)。

from functools import reduce
res = reduce(lambda x,y:x+y,range(10))  #求和
res1 = reduce(lambda x,y:x*y,range(1,10)) #階乘
print(res)
print(res1)

運行結果:

45
362880

17、globals():返回的是全局變量的字典,修改其中的內容,值會真正的發(fā)生改變。
locals():會以dict類型返回當前位置的全部局部變量。

def test():
 loc_var = 234
 print(locals())
test()

運行結果:

{'loc_var': 234}

18、hash():函數(shù)返回對象的哈希值。返回的哈希值是使用一個整數(shù)表示,通常使用在字典里,以便實現(xiàn)快速查詢鍵值。

print(hash('liu'))
print(hash("liu"))
print(hash('al'))
print(hash(3))

運行結果:

-1221260751
-1221260751
993930640
3

19、hex(x):將一個數(shù)字轉換成十六進制

oct(x):將一個數(shù)字轉換成八進制

print(hex(15))
print(hex(32))

運行結果:

0xf
0x20

print(oct(8))
print(oct(16))
print(oct(31))

運行結果:

0o10
0o20
0o37

20、round():返回浮點數(shù)x的四舍五入值

print(round(1.3457,3))

運行結果:

1.346

21、sorted():排序

a = {6:2,8:0,1:4,-5:6,99:11,4:22}
print(sorted(a.items())) #按照鍵排序
print(sorted(a.items(),key=lambda x:x[1]))  #按照鍵值排序

運行結果:

[(-5, 6), (1, 4), (4, 22), (6, 2), (8, 0), (99, 11)]
[(8, 0), (6, 2), (1, 4), (-5, 6), (99, 11), (4, 22)]

22、zip():接受任意多個(包括0個和1個)序列作為參數(shù),返回一個tuple列表。

a = [1,2,3,4]
b = ['a','b','c','d']
for i in zip(a,b):
 print(i)

運行結果:

(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')

23、__import__('decorator')等價于import decorator

關于Python相關內容感興趣的讀者可查看本站專題:《Python函數(shù)使用技巧總結》、《Python面向對象程序設計入門與進階教程》、《Python數(shù)據(jù)結構與算法教程》、《Python字符串操作技巧匯總》、《Python編碼操作技巧總結》及《Python入門與進階經(jīng)典教程

希望本文所述對大家Python程序設計有所幫助。

相關文章

  • 獲取python文件擴展名和文件名方法

    獲取python文件擴展名和文件名方法

    本篇文章通過python寫一個獲取python文件擴展名和文件名的功能,并分享了代碼,有興趣的參考下。
    2018-02-02
  • Python通過psd-tools解析PSD文件

    Python通過psd-tools解析PSD文件

    這篇文章主要介紹了Python通過psd-tools解析PSD文件,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-06-06
  • python列表生成器常用迭代器示例詳解

    python列表生成器常用迭代器示例詳解

    這篇文章主要為大家介紹了python列表生成器常用迭代器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06
  • Python繪圖之turtle庫的基礎語法使用

    Python繪圖之turtle庫的基礎語法使用

    這篇文章主要給大家介紹了關于Python繪圖之turtle庫的基礎語法使用的相關資料, Turtle庫是Python語言中一個很流行的繪制圖像的函數(shù)庫,再繪圖的時候經(jīng)常需要用到的一個庫需要的朋友可以參考下
    2021-06-06
  • Python變量名詳細規(guī)則詳細變量值介紹

    Python變量名詳細規(guī)則詳細變量值介紹

    這篇文章主要介紹了Python變量名詳細規(guī)則詳細變量值,Python需要使用標識符給變量命名,其實標識符就是用于給程序中變量、類、方法命名的符號(簡單來說,標識符就是合法的名稱,下面葛小編一起進入文章里哦阿姐更多詳細內容吧
    2022-01-01
  • Python pip安裝第三方庫的攻略分享

    Python pip安裝第三方庫的攻略分享

    pip 就是 Python 標準庫(The Python Standard Library)中的一個包,只是這個包比較特殊,用它可以來管理 Python 標準庫(The Python Standard Library)中其他的包。本文為大家介紹了pip安裝第三方庫的方法,需要的可以參考一下
    2022-11-11
  • 下載官網(wǎng)python并安裝的步驟詳解

    下載官網(wǎng)python并安裝的步驟詳解

    在本篇文章里小編給大家整理了關于下載官網(wǎng)python并安裝的步驟詳解,需要的朋友們參考學習下。
    2019-10-10
  • 關于Pyinstaller打包eel和pygame需要注意的坑

    關于Pyinstaller打包eel和pygame需要注意的坑

    這篇文章主要介紹了關于Pyinstaller打包eel和pygame需要注意的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 5分鐘 Pipenv 上手指南

    5分鐘 Pipenv 上手指南

    這篇文章主要介紹了5分鐘 Pipenv 上手指南,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • python列表與列表算法詳解

    python列表與列表算法詳解

    這篇文章主要介紹了Python的列表和列表算法,具有一定參考價值,需要的朋友可以了解下,希望能給你帶來幫助
    2021-08-08

最新評論