Million annual salary python Road - Built-in functions

Built-in objects (68) a first portion

A total of 68 built-in functions

Some may not be high importance of built-in functions, you can only understand

all() any() 
bytes() 
callable() 
chr() ord()
complex() 
divmod() 
eval() exec() 
format() 
frozenset() 
globals() locals() 
hash() 
help() 
id() 
input() 
int() 
iter() next()
bin() oct() hex()
pow() 
repr() 
round()

eval: type of code execution string, and returns the final result.

eval('2 + 2')  # 4
n=81
eval("n + 4")  # 85
eval('print(666)')  # 666

exec: execution string types.

s = '''
for i in [1,2,3]:
    print(i)
'''
exec(s)

Prohibit the use of two or more built-in functions very powerful work

hash: Get an object (the object may be a hash: int, str, Bool, tuple) hash value.

print(hash(12322))
print(hash('123'))
print(hash('arg'))
print(hash('alex'))
print(hash(True))
print(hash(False))
print(hash((1,2,3)))

'''
-2996001552409009098
-4637515981888139739
1
2528502973977326415
'''

help: function module or function for viewing purposes described in detail.

print(help(list))
print(help(str.split))

callable: function is used to check whether an object is callable. If it returns True, calls still may fail; but if it returns False, the calling object ojbect will never succeed.

name = 'alex'
def func():
    pass
print(callable(name))  # False
print(callable(func))  # True

int: function is used to convert a string or an integer number.

print(int())  # 0
print(int('12'))  # 12
print(int(3.6))  # 3
print(int('0100',base=2))  # 将2进制的 0100 转化成十进制。结果为 4

float: function is used to convert the integer to floating point and string.

complex: a function to create the complex value real + imag * j or transformed string or a plural number. If the first parameter is a string, there is no need to specify the second parameter. .

print(float(3))  # 3.0
print(complex(1,2))  # (1+2j)

bin: to convert decimal to binary and back.

oct: converted to decimal octal string and returns.

hex: decimal converted to a hexadecimal string and returns.

print(bin(10),type(bin(10)))  # 0b1010 <class 'str'>
print(oct(10),type(oct(10)))  # 0o12 <class 'str'>
print(hex(10),type(hex(10)))  # 0xa <class 'str'>

divmod: the divisor and the dividend calculation result, returns a tuple of the quotient and remainder of (a // b, a% b).

round: retention of floating-point decimal places, default retention integer, you can set the number of bits reserved

pow: find the X- the y-th power. (Three parameters of x results of z I y taken)

print(divmod(7,2))  # (3, 1)
print(round(7/3,2))  # 2.33
print(round(7/3))  # 2
print(round(3.32567,3))  # 3.326
print(pow(2,3))  # 两个参数为2**3次幂
print(pow(2,3,3))  # 三个参数为2**3次幂,对3取余。

bytes: for conversion between different coding.

# s = '你好'
# bs = s.encode('utf-8')
# print(bs)
# s1 = bs.decode('utf-8')
# print(s1)
# bs = bytes(s,encoding='utf-8')
# print(bs)
# b = '你好'.encode('gbk')
# b1 = b.decode('gbk')
# print(b1.encode('utf-8'))

ord: Enter the characters to find the character encoding of the current position

chr: enter the current position of the digital encoding to identify its corresponding character

# ord 输入字符找该字符编码的位置
# print(ord('a'))
# print(ord('中'))

# chr 输入位置数字找出其对应的字符
# print(chr(97))
# print(chr(20013))

repr: Returns a string form of the object (true colors).

# %r  原封不动的写出来
# name = 'taibai'
# print('我叫%r'%name)

# repr 原形毕露
print(repr('{"name":"alex"}'))
print('{"name":"alex"}')

all: iterables in all True is True

any: iterables, there is a True True

# all  可迭代对象中,全都是True才是True
# any  可迭代对象中,有一个True 就是True
# print(all([1,2,True,0]))
# print(any([1,'',0]))

Guess you like

Origin www.cnblogs.com/zhangchaoyin/p/11221242.html