天天看点

python 学习day4

函数装饰器

python 学习day4
python 学习day4

内置函数

  • 编译字符串成为python代码
# s = "print(123)"# compile(s, "<string>", "exec")      
  • 执行代码、字符串、表达式
# exec("print(1+2+3)")  #代码与字符串都可以执行,但是无返回值(无返回值)# print(eval("1+2+3"))  #可以执行表达式,将执行结果当作返回值返回(有返回值)      
  • dir(dict) #获取对象中提供的功能信息
  • help(list) #查询class的具体使用方法
  • n1, n2 = divmod(98, 10) #98/10 求商以及余数
  • 对象是类的实例
# string = "zshaox"   #对象string 是str class的实例      
  • 判断对象是否是类的实例
# isin = isinstance(string, str)  #isinstance 对象是否是类的实例# print(isin)      
  • enumerate 遍历数组/列表 信息,打印下标
# for key,value in enumerate([1, 2, 3]):#     print(key, value)      
  • filter循环 与map 循环

    filter 函数返回True将元素添加到结果中 

    map 将函数返回值添加到结果中

tu = (1, 2, 3, 4, 5, '1', '2', '3')# def f2(argv):#     if argv > 2:#         return True#ret = filter(f2, tu)#filter循环第二个参数,将第二个参数中可遍历的元素作为参数一(函数)的元素,执行函数返回True,则将参数传入 ret中ret = filter(lambda a: type(a) == int, tu)ret = list(ret)print(tuple(ret))#map循环第二个参数,将第二个参数中可遍历的元素作为参数传如函数中执行,并将值返回到ret中ret = map(lambda a: a + 100, ret)print(list(ret))      
  • len()在python3 中默认是计算字符的长度;在python2.7中无法计算字符长度,只能计算字节的长度
  • min() #找最小值