91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python內置方法

發布時間:2020-08-01 05:30:24 來源:網絡 閱讀:476 作者:DevOperater 欄目:編程語言

1.abs取絕對值

>>> abs(9.8)
9.8
>>> abs(-9.8)
9.8

2.dic()變為字典類型

>>> dict({"key":"value"})
{'key': 'value'}

3.help()顯示幫助信息

>>> help(map)
Help on class map in module builtins:

class map(object)
 |  map(func, *iterables) --> map object
 |
 |  Make an iterator that computes the function using arguments from
-- More  --

4.min取數據中的最小值,max取數據中的最大值

print(min([3, 4, 2]))
print(min("wqeqwe"))
print(min((3, 6, 4)))
print(max([3, 4, 2]))
print(max("wqeqwe"))
print(max((3, 6, 4)))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
2
e
3
4
w
6

Process finished with exit code 0

5.all()所有的為true,才返回true。空的列表返回true

print(all([1, 2, 0]))  # 列表中的0是False,所以返回False
print(all([1, 2, 5]))  # 列表中的所有值都是True,所以返回True
print(all([]))  # 空的列表,all()返回true
print(help(all))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
False
True
True
Help on built-in function all in module builtins:

all(iterable, /)
    Return True if bool(x) is True for all values x in the iterable.

    If the iterable is empty, return True.

None

Process finished with exit code 0

6.any()任意一個為true就返回true。空的列表返回false

any()列表中的任意一個為True,就返回True

print(any([1, 2, 0]))  
print(any([1, 2, 5]))  # 列表中的任意一個是True,就返回True
print(any([]))  # 空的列表,any()返回false
print(help(any))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
True
True
False
Help on built-in function any in module builtins:

any(iterable, /)
    Return True if bool(x) is True for any x in the iterable.

    If the iterable is empty, return False.

None

Process finished with exit code 0

7.dir()打印當前程序的所有變量

print(dir())  # 打印當前程序的所有變量
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']

Process finished with exit code 0

8.hex()把十進制數轉換為16進制數

hex()轉換為16進制
print(hex(16))  
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0x10

Process finished with exit code 0

9.slice()切片

>>> l = [2,3,4,5,6,7]
>>> s = slice(1,5,2)
>>> l(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable
>>> l[s]
[3, 5]

10.divmod()求商和余數

>>> divmod(10,3)
(3, 1)
>>> divmod(10,2)
(5, 0)
>>>

11.sorted()排序

>>> sorted([1,9,4])
[1, 4, 9]

d = {1: 0, 10: 4, 9: 2, 15: 3}
print(d.items())
print(sorted(d.items(), key=lambda x: x[1], reverse=True))

E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
dict_items([(1, 0), (10, 4), (9, 2), (15, 3)])
[(10, 4), (15, 3), (9, 2), (1, 0)]

Process finished with exit code 0

13.ascii()轉換為ascii碼

>>> ascii("qwqw我")
"'qwqw\\u6211'"

14.oct()十進制數轉換為8進制數

>>> print(oct(8))
0o10

15.bin()十進制數轉換為二進制數

>>> print(bin(10))
0b1010

16.eval()把字符轉換為里面原有的含義,把字符串轉換為代碼

print(eval("{1:2}"))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
{1: 2}

Process finished with exit code 0

print(eval("1+2*3"))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
7

Process finished with exit code 0

eval('print("hello")')
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
hello

Process finished with exit code 0

eval()只能解析單行代碼,不能解析多行的代碼
code = '''
if 3 > 2:
    print("3>2")
'''
eval(code)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
Traceback (most recent call last):
  File "E:/PythonProject/python-test/BasicGrammer/test.py", line 9, in <module>
    eval(code)
  File "<string>", line 2
    if 3 > 2:
     ^
SyntaxError: invalid syntax

Process finished with exit code 1

17.exec()可以解析執行多行代碼,但是獲取不到函數的返回值,eval()可以

code = '''
if 3 > 2:
    print("3>2")
'''
exec(code)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
3>2

Process finished with exit code 0

code = '''

def foo():
    if 3 > 2:
        print("3>2")
        return 3
foo()
'''
re_exec = exec(code)
print(re_exec)
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
3>2
None

Process finished with exit code 0

print(eval("1+2+3"))
print(exec("1+2+3"))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
6
None

Process finished with exit code 0

18.ord()獲取對應的ascii碼表中的值。chr()獲取ascii表中對應的字符

ord() 獲取對應的ascii碼表中的值
chr() 獲取ascii表中值對應的字符
>>> ord("a")
97
>>> chr(97)
'a'

19.sum()求和

>>> sum((1,2,3))
6
>>> sum([1,2,3])
6
>>> sum({1:2,3:4})
4
>>> sum({1,2,3,4})
10
>>> sum("123")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>

20.bytearray()可通過encode,decode之后,通過index來修改字符串中的值

>>> s = "woai中國"
>>> s[0] = "W"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> s = bytearray(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: string argument without an encoding
>>> s = s.encode('utf-8')
>>> s
b'woai\xe4\xb8\xad\xe5\x9b\xbd'
>>> s = bytearray(s)
>>> s
bytearray(b'woai\xe4\xb8\xad\xe5\x9b\xbd')
>>> s[0]='W'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>> s[0]=97
>>> s
bytearray(b'aoai\xe4\xb8\xad\xe5\x9b\xbd')
>>> s.decode('utf-8')
'aoai中國'

21.id()查看變量的內存地址

>>> id(s[0])  # s[0]的內存地址會變
1487327920
>>> s[0]=66
>>> id(s[0])
1487326928
>>> id(s)
2879739806752  # s的內存地址是不變的
>>> s[0]=67
>>> id(s)
2879739806752

22.map()通過匿名函數lambda來對列表中的數據進行操作

map()
>>> list(map(lambda x:x*x,[1,2,3]))
[1, 4, 9]
filter()
>>> list(filter(lambda x:x>3,[1,2,3,4,5]))
[4, 5]

23.reduce()對數據進行整合操作,返回一個值

>>> import functools
>>> functools.reduce(lambda x,y:x+y,[1,2,3,4])
10
>>> functools.reduce(lambda x,y:x*y,[1,2,3,4])
24
>>> functools.reduce(lambda x,y:x*y,[1,2,3,4],2)
48
>>> functools.reduce(lambda x,y:x*y,[1,2,3,4],3)
72
>>> functools.reduce(lambda x,y:x+y,[1,2,3,4],3)
13

24.print()

def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
    """
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
    """
    pass

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita

msg = "msg"
# 文件的模式必須是可寫入模式(w,r+),不能是只讀模式
f = open(file="寫文件.txt", mode="w", encoding="utf-8")
print(msg, "my input", sep="|", end=":::",file=f)

運行程序
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py

Process finished with exit code 0

查看"寫文件.txt"
msg|my input:::

25.tupple()把可迭代的數據類型變為元組

>>> a = [1,2,3]
>>> tuple(a)
(1, 2, 3)
>>> tuple("1,2,3")
('1', ',', '2', ',', '3')
>>> tuple({1:2})
(1,)
>>> tuple(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

26.callable()判斷是否可調用,即通過abs()方式調用,函數是可調用的,可用于判斷是否是函數

callable()判斷是否可調用,即通過abc()方式調用
函數是可調用的,可用于判斷是否是函數
>>> callable(abs)
True
>>> callable(list)
True
>>> callable([1,2,3])
False

27.frozenset()變為不可變集合

>>> s = frozenset({1,2,3})
>>> s.discard(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'discard'

28.vars()包含變量的名和變量的值,dir()只是變量的名字

>>> vars()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_import
lib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins'
(built-in)>, 'l': [2, 3, 4, 5, 6, 7], 's': bytearray(b'aoaini'), 'd': {10: 2, 12: 1, 9: 0}, 'a': [1
, 2, 3]}
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__',
 'a', 'd', 'l', 's']

>>>

29.locals()打印局部變量,globals()打印全局變量

>>> globals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_import
lib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'l': [2, 3, 4, 5, 6, 7], 's': bytearray(b'aoaini'), 'd': {10: 2, 12: 1, 9: 0}, 'a': [1, 2,
 3]}
>>> locals()
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (bui
lt-in)>, 'l': [2, 3, 4, 5, 6, 7], 's': bytearray(b'aoaini'), 'd': {10: 2, 12: 1, 9: 0}, 'a': [1, 2, 3]}
>>>

30.repr顯示形式變為字符串

>>> repr(abs(23))
'23'
>>> repr(frozenset({12,4}))
'frozenset({12, 4})'
>>> repr({1,2,3})
'{1, 2, 3}'
>>>

31.zip

>>> a = [1,2,3,4,5]
>>> b = ["a","b","c"]
>>> zip(a)
<zip object at 0x00000237FF9EAF08>
>>> zip(a,b)
<zip object at 0x00000237FF9D8448>
>>> list(zip(a,b))
[(1, 'a'), (2, 'b'), (3, 'c')]
>>> dict(zip(a,b))
{1: 'a', 2: 'b', 3: 'c'}
>>> str(zip(a,b))
'<zip object at 0x00000237FF9EAF08>'
>>> tuple(zip(a,b))
((1, 'a'), (2, 'b'), (3, 'c'))
>>>

32.complex()變為復數

>>> complex(3,5)
(3+5j)
>>> complex(3)
(3+0j)

33.round()保留幾位小數位

>>> round(3.12123333333334444445555555555555555,18)
3.1212333333333446
>>> round(3.12123333333334444445555555555555555,2)
3.12

34.hash()把字符串變為固定長度的hash值

不可變數據類型才是可hash的,包含整數,字符串,元組,都是不可變的,是可hash的
>>> hash("12")
8731980002792086209
>>> hash("123")
-1620719444414375290
>>> hash([1,2])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> hash(1)
1
>>> hash(123)
123
>>> hash((1,2))
3713081631934410656
>>> hash((1,2,3))
2528502973977326415
>>> hash({1,2,3})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
>>>

35.set()把可迭代對象變為集合集合

>>> set([1,2,3])
{1, 2, 3}
>>> set((1,2,3))
{1, 2, 3}
>>> set("21")
{'1', '2'}
>>> set(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
>>> set({1:2,3:4})
{1, 3}
>>>
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

富平县| 金坛市| 蒙山县| 申扎县| 黄陵县| 剑川县| 荆门市| 南澳县| 北京市| 新龙县| 洞口县| 灵石县| 龙山县| 化德县| 鹿邑县| 志丹县| 静乐县| 江陵县| 万州区| 屏东市| 霍林郭勒市| 台州市| 湖州市| 铁岭县| 大洼县| 沽源县| 河间市| 墨脱县| 金堂县| 长丰县| 迁西县| 丰原市| 巴南区| 多伦县| 晋宁县| 宁都县| 静乐县| 邹城市| 嘉兴市| 揭东县| 福海县|