17. Derivation of formula:
Derivation: a plurality of lines into the line for recycling
- 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])
- 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)
- 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):
- 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()
- exec():
s1 = """
def func():
print(123)
func
"""
print(exec(s))#牛逼,工作中不能使用
- hash (): Gets the hash value
print(hash("lajdsf"))
- help (): print source
print(help(list))
help(list)
- callable (): determine whether the call can return bool
def func():
pass
print(callable(func))
- complex (): plural
print(complex(-1))
- oct (): Decimal turn octal
- hex (): Decimal turn hex
print(oct(15))
print(hex(15))
- divmod (): print quotient and remainder
print(divmod(5,2))
- 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入
- pow (): Exponentiation
print(pow(2,3))
print(pow(2,4,3))#先求幂,在除第三个数取余
- bytes():
s = 'alex'
print(bytes(s,encoding = 'utf-8'))
- words():
- chr()
print(ord("A"))#字母和数字转aci码
print(ord('你'))#汉字是当前使用的编码
print(chr(20320))
- repr (): -> r anti escaped # \ u 3000 represents a space
- all (): determine whether the elements are true
- any (): determining whether there is true element
lst = [1,2,6,0,2]
print(all(lst))#False
print(any(lst))#true
- globals (): View the global variable space exists
- locals (): View the current variable space exists
name = 1
def func():
a = 123
print(locals())
return
print(globals)