Advanced functions - Built-in functions

Built-in functions

More built-in functions: https://docs.python.org/3/library/functions.html?highlight=built#ascii

Common .........

1.bytes ()
decoding characters.

res = '你好'.encode('utf8')
print(res)

b'\xe4\xbd\xa0\xe5\xa5\xbd'

res = bytes('你好', encoding='utf8')
print(res)

b'\xe4\xbd\xa0\xe5\xa5\xbd'
2.chr () / word ()

CHR () refer to a digital ASCII code table converted into a corresponding character; the ord () converts the character into a corresponding digital.

print(chr(65))

A

print(ord('A'))

65
3.divmod ()
columns.

print(divmod(10, 3))

(3, 1)
4.enumerate ()
iteration with indexes.

l = ['a', 'b', 'c']
for i in enumerate(l):
    print(i)

(0, 'a') (1, 'b') (2, 'c')
5.eval ()
translates into a string data type.

lis = '[1,2,3]'
lis_eval = eval(lis)
print(lis_eval)

[1, 2, 3]

6.hash ()
whether the hash.

print(hash(1))

1

7.abs ()
absolute value.

print(abs(-13))  # 求绝对值

13

8.all ()
within iterables elements are all true, it returns true.

print(all([1, 2, 3, 0]))
print(all([]))

False True

9.any ()
iterables there is an element it is true, True.

print(any([1, 2, 3, 0]))
print(any([]))

True False

10.bin () / oct () / hex ()
binary, octal, hexadecimal conversion.

print(bin(17))
print(oct(17))
print(hex(17))

0b10001 0o21 0x11
11.dir ()
include all the time functions.

import time
print(dir(time))

['_STRUCT_TM_ITEMS', 'doc', 'loader', 'name', 'package', 'spec', 'altzone', 'asctime', 'clock', 'ctime', 'daylight', 'get_clock_info', 'gmtime', 'localtime', 'mktime', 'monotonic', 'perf_counter', 'process_time', 'sleep', 'strftime', 'strptime', 'struct_time', 'time', 'timezone', 'tzname', 'tzset']
12.frozenset()
不可变集合。

s = frozenset({1, 2, 3})
print(s)

frozenset({1, 2, 3})
13.globals () / loacals ()
to check the global names; see local name.

# print(globals())
def func():
    a = 1
#     print(globals())
    print(locals())

func()

{'a': 1}

14.pow()

print(pow(3, 2, 3))  # (3**2)%3

0

15.round()

print(round(3.5))

4

16.slice()

lis = ['a', 'b', 'c']
s = slice(1, 4, 1)
print(lis[s])  # print(lis[1:4:1])

['b', 'c']

17.sum()

print(sum(range(100)))

4950

12 .__ import __ ()
by introducing module string.

m = __import__('time')
print(m.time())

1556607502.334777

1.3 Object-oriented knowledge

classmethod
staticmethod
property
delattr
hasattr
getattr
setattr
isinstance()
issubclass()
object()
super()

Guess you like

Origin www.cnblogs.com/suren-apan/p/11374757.html