- abs() 返回绝对值
- divmode() 返回商和余数
- round() 四舍五入
- pow(a, b) 求a的b次幂, 如果有三个参数. 则求完次幂后对第三个数取余
- sum() 求和
- min() 求最小值
- max() 求最大值
print(abs(-2))
print(divmod(20,3))
print(round(4.50))
print(round(4.51))
print(pow(10,2,3))
print(sum([1,2,3,4,5,6,7,8,9,10]))
print(min(5,3,9,12,7,2))
print(max(7,3,15,9,4,13))
s = "hello world!"
print(format(s, "^20"))
print(format(s, "<20"))
print(format(s, ">20"))
print(format(3, 'b' ))
print(format(97, 'c' ))
print(format(11, 'd' ))
print(format(11, 'o' ))
print(format(11, 'x' ))
print(format(11, 'X' ))
print(format(11, 'n' ))
print(format(11))
print(format(123456789, 'e' ))
print(format(123456789, '0.2e' ))
print(format(123456789, '0.2E' ))
print(format(1.23456789, 'f' ))
print(format(1.23456789, '0.2f' ))
print(format(1.23456789, '0.10f'))
print(format(1.23456789e+3, 'F'))
语法:fiter(function. Iterable)
function: 用来筛选的函数. 在filter中会自动的把iterable中的元素传递给function. 然后根据function返回的True或者False来判断是否保留留此项数据 , Iterable: 可迭代对象
def func(i):
return i % 2 == 1
lst = [1,2,3,4,5,6,7,8,9]
l1 = filter(func, lst)
print(l1)
print(list(l1))
会根据提供的函数对指定序列列做映射(lamda)
语法 : map(function, iterable)
可以对可迭代对象中的每一个元素进行映射. 分别去执行 function
def f(i):
return i
lst = [1,2,3,4,5,6,7,]
it = map(f, lst)
- locals() 返回当前作用域中的名字
- globals() 返回全局作用域中的名字
- eval() 执行字符串类型的代码. 并返回最终结果
- exec() 执行字符串类型的代码
- compile() 将字符串类型的代码编码. 代码对象能够通过exec语句来执行或者eval()进行求值
s1 = input("请输入a+b:")
print(eval(s1))
s2 = "for i in range(5): print(i)"
a = exec(s2)
print(a)
exec("""
def func():
print(" 我是周杰伦")
""" )
func()
code1 = "for i in range(3): print(i)"
com = compile(code1, "", mode="exec")
exec(com)
code2 = "5+6+7"
com2 = compile(code2, "", mode="eval")
print(eval(com2))
code3 = "name = input('请输入你的名字:')"
com3 = compile(code3, "", mode="single")
exec(com3)
print(name)
a = 10
print(callable(a))
def f():
print("hello")
print(callable(f))
- dir() : 查看对象的内置属性, 访问的是对象中的**dir**()方法
print(dir(tuple))