A built-in function
eval: type of code execution string
exac: type of code execution string Society
and prohibit the use of eval exac
hash () function is to distinguish between variable data types and data type immutable
# print(hash("123")) # print(hash(12)) # print(hash(-1)) # print(hash(-10)) # print(hash((2,1))) # dic = {[1,2,3]:2} # print(hash([1,2,3]))
help (): View help information
callable (): Check whether the object can be called,
# def func(): # print(1) # lst = [1,23,4,] # print(callable(lst)) # 查看对象是否可调用
int (): string or integer numbers into
float (): converted to floating point
complex (): plural
bin (): Decimal Binary transfer
oct (): Decimal turn octal
hex (): Decimal turn hex
divmod (): calculated result divisor and dividend, the quotient and remainder comprising a Ganso
round (): retention of floating-point decimal places, the number of bits can be set to retain, retain the default integer
pow (): power demand x ** y (when the result of the three parameters of x ** y modulo third parameter)
bytes (): for switching between different coding recommended encode
ord (): Get the current position of the epitope encoded by elemental
chr (): find the corresponding bit number table by elements
repr (): View original ecological data (for programmers)
all (): Summary of the container is determined whether the element are true, returns true
any (): Analyzing container elements have a true, it is True
Anonymous function: line of the function lambda (Bibei)
def func():
print(123)
func()
grammar:
Function name = lambda parameter: Return value
The name of the anonymous function is called lambda
lambda is a keyword-defined functions, the equivalent def function
Only one data type can return,
面试题: print ([lambda : i for i in range(5)]) (返回5个内存地址) print(lst[0]())---结果是4--因为循环最后一次输出是4,调用的全局最后一个就是4 不加后面的小括号就是调用内存地址 加了括号就是调用函数
lst = [lambda X : X+1 for i in range(5)] print(lst[0](5)) 输出结果为:6,返回值是x+1
tu = (lambda : i for i in range(3)) print(tu[0]) #输出错误,不能索引 print(tu)#输出内存地址 print(next(tu)) #一个函数地址 print(next(tu)()) #输出0 print(next(tu)()) #输出1
lst = [lambda : i for i in range(3)] print(lst[0]()) tu = (lambda : i for i in range(3)) print(next(tu)()) 输出结果: 2 0
Body of the function is stored in the code
Generator is stored in the code
- Cause: yield and lead to inconsistent execution result of the function generator
lst = [lambd x:x+5 for i in range(2)] print([i(2) for i in lst]) 解开顺序: lst = [] for i in range (2): lst.append(lambda x :x+5) new_list for i in lst: print(i) #两个函数的内存地址 new_list.append(i(2)) print(new_list) #输出列表【7,7】 输出结果:【7,7】 lst = (lambda x:x+5 for i in range(2)) print([i(2) for i in lst]) 输出结果:【7,7】 lst = (lambda x:x*i for i in range(2)) print([i(2) for i in lst]) #【0,2】
lambda == def == keyword
- lambfa x: x
- x: shape parameter is a function of the ordinary (location, default), any number can be written, can not write
- : Behind the ordinary is the return value of the function must be written, no default, must have a return value, you can only write one data type
- lambfa x: x
Two built-in functions (remember all)
Built-in functions
Two dictionaries combined into one:
- update--- dic2.update(dic1)
- Broken: - print (dict (** dic1, ** dic2))
- print (dict ([(1,2), (3,3)])) --- brackets at least one of a digital versatile will not, iterate through the list may be ancestral
- dict( ** dict1, ** dict2)
set (): The iterables converted into ancestral
sep (): a method for dividing between each element
print(1,2,3,sep = "|") 输出结果:1|2|3
end: print after the implementation of the end of the statement, the default \ n
print(1,2,3,end = "") print() 输出结果:全部在一行
print (): screen output
- flush - Flush
file: file handles, the default display to the screen
print (1,2,3,4,5,file = open ("test","w",encoding="utf-8"))
sum () - sum, the object must be iterative, object elements must be an integer, string type can not be used
print (sum ([1,2,3,1])) print (sum ([1,2,3,1],100) #100是起始值,就是从100开始进行取和,指定开始位置的值
abs (): Returns the absolute value - converted into a positive number, whether you are not negative
dir (): View the current object has any way
zip (): the fastener, when the length is inconsistent iterables choose the shortest merge may be a plurality of
面试题: list1 =[1,2,3,4] lst2 = ["alex","wusir","meet"] print(list(zip(lst1,lst2))) 输出结果:[(1,"alex"),(2,"wusir"),(3,"meet")] print(dict(zip(lst1,lst2))) #返回一个字典 输出结果:{1:"alex",2:"wusir",3:"meet"}
format (): format conversion
Alignment
print(format("alex",">20")) #右对齐 print(format("alex","<20")) #左对齐 print(format("alex","^20")) #居中
Hexadecimal conversion
print(format(10,"b")) #十进制转二进制 print(format(10,"08b")) #不够就补0
print (format(10,"08o")) #oct,八进制 print (format(10,"08x")) #hex,十六进制
print(format(0b1010,"d")) #二进制转十进制
reversed (): an inverted sequence, flipped sequence returns an iterator
l = reversed('你好') # l 获取到的是一个生成器 print(list(l)) ret = reversed([1, 4, 3, 7, 9]) print(list(ret)) # [9, 7, 3, 4, 1]