python function knowledge five derivations and a built-in functions (understanding)

17. Derivation of formula:

Derivation: a plurality of lines into the line for recycling

  1. derivation list: []
#普通模式
print([i for i in range(20)])

#循环模式
    #[变量 for i in range(20)]
print([i+1 for i in range(10)])

#筛选模式
    #[变量(加工后) for i in range(20)]
print([i for i in range(20) if i % 2 == 1])
  1. Expression generator unit :()
#普通模式
print((i for i in range(20)))
#结果:是内存地址 <generator object <genexpr> at 0x0000023EBFA0F048>
g = (i for i in range(20))
print(next(g))
print(next(g))

#筛选模式
g = (i for i in range(50) if i %2 ==1)
for i in g:
    print(i)
  1. Dictionary / collection derived formula: {}
#{}
print({for i in range(10)})#集合
print({i:i+1 for i in range(10)})#字典,键值对

#筛选模式
print({for i in range(10) if i % 2 == 0})
print({i:i+1 for i in range(10) if i % 2 == 0})

list:

[Variables (variables after processing) for circulating]

[Variables (variables after processing) for circulating the processing conditions]

Builder derivations:

(Variables (variables after processing) for loop)

(Variables (variables after processing) for circulating the processing conditions)

Dictionary derivations:

{Key: values ​​of processing conditions for loop}

Collection derivations:

{Processing conditions} variable for loop

18. a built-in function (to know):

  1. eval (): the number of string operation by operators
s = '3+5'
print(eval(s))
s = '3*5'
print(eval(s))

def func():
    print(1)
    return
func()
  1. exec():
s1 = """
def func():
    print(123)
func
"""
print(exec(s))#牛逼,工作中不能使用
  1. hash (): Gets the hash value
print(hash("lajdsf"))
  1. help (): print source
print(help(list))
help(list)
  1. callable (): determine whether the call can return bool
def func():
    pass
print(callable(func))
  1. complex (): plural
print(complex(-1))
  1. oct (): Decimal turn octal
  2. hex (): Decimal turn hex
print(oct(15))
print(hex(15))
  1. divmod (): print quotient and remainder
print(divmod(5,2))
  1. round (): rounded, odd and even points may be specified retain decimal places
print(2.5)#偶数0~0.5舍,0.51~0.99入
print(5.5)#奇数0~0.49舍,0.5~0.99入
print(5.231,2)#指定保留两位小数。指定保留小数后都是4舍5入
  1. pow (): Exponentiation
print(pow(2,3))
print(pow(2,4,3))#先求幂,在除第三个数取余
  1. bytes():
s = 'alex'
print(bytes(s,encoding = 'utf-8'))
  1. words():
  2. chr()
print(ord("A"))#字母和数字转aci码
print(ord('你'))#汉字是当前使用的编码
print(chr(20320))
  1. repr (): -> r anti escaped # \ u 3000 represents a space
  2. all (): determine whether the elements are true
  3. any (): determining whether there is true element
lst = [1,2,6,0,2]
print(all(lst))#False
print(any(lst))#true
  1. globals (): View the global variable space exists
  2. locals (): View the current variable space exists
name = 1
def func():
    a = 123
    print(locals())
    return
print(globals)

Guess you like

Origin www.cnblogs.com/Onlywang/p/11228942.html