美文网首页
python入门 第八天 lambda和内置函数

python入门 第八天 lambda和内置函数

作者: xinmin | 来源:发表于2018-08-19 15:57 被阅读0次
    • lambda 表达式
      # f1相当于f2
      def f1():
          return 123
      
      f2 = lambda : 123
      
      # f3相当于f4
      def f3(a1, a2):
          return a1+a2
      
      f4 = lambda a1,a2: a1 + a2
      
    • 内置函数
      # 绝对值
      i = abs(-123)
      print(i) # 123
      # =============================================
      # all,循环参数,如果每个元素都为真,那么all的返回值为真
      # 为假:0,None,"",[],{},()
      r = all(["123", " ",  [11,], ""])
      print(r)  # False
      # =============================================
      # any 只要有一个为真,则为真
      i = any([None, "", [], {}, (), 1)
      print(i) # True
      # =============================================
      # ascii, 对象的类中找__repr__,获取返回值
      class Foo:
          def __repr__(self):
              return "hello"
      obj = Foo()
      r = ascii(obj)
      print(r) # hello
      # =============================================
      # bin()  十进制转二进制
      # oct()  十进制转八进制
      # int()  十进制转十进制
      # hex()  十进制转十六进制
      # 其他进制转换成十进制
      int('0xef', base=16)
      int('0b11', base=2)
      int('0o11', base=8)
      # =============================================
      # bool,判断真假,把一个对象转换成布尔值,None、"",[],{}为False
      # =============================================
      # chr()  把数字转换成ASCII码对应的字符串
      # 一个字节,8位,2**8 = 256
      # ord()  把字符串转换成ASCII码对应的数字(0-256)
      # 生成随机验证码
      import random
      temp = ""
      for i in range(6):
          num = random.randrange(0, 4)
          if num = 1 or num = 3:
              rad1 = random.randrange(0, 10)
              rad1 = str(rad1)
              temp += rad1
          else:
              rad2 = random.randrange(65, 91) # ASCII码大写A-Z(65-90)
              c = chr(rad1)
              temp += c
      print(temp)
      # ============================================
      # divmod 整除求商和余数
      r = divmod(10, 3)
      print(r) # (3, 1)
      # ============================================
      #  eval 可以执行一个字符串形式的表达式
      a = (" 1 + 3")
      ret = eval(" 1 + 3 ")
      print(ret) # 4
      print(a) # 1 + 3
      res = eval(" a + 60 ", {"a": 99})
      print(res) # 159
      # ============================================
      # exec 执行python代码
      exec("for i in range(10): print(i)")
      # comple 编译python代码
      # ============================================
      # filter(函数, 可迭代的对象) 循环可迭代的对象,获取每一个参数,执行函数(参数)
      def f1(x):
          if x > 22:
              return True
         else:
             return False
      
      ret = filter(f1, [11, 22, 33, 44])
      for i in ret:
          print(i) # [33, 44]
      # lambda表达式的写法
      ret2 = filter(lambda x: x>22, [11, 22, 33, 44])
      for i in ret2:
          print(i) 
      # =============================================
      # map(函数, 可迭代的对象)
      def f1(x):
          if x % 2 == 1:
              return x + 100
         else:
               return x
      # ret = map(f1, [1, 2, 3, 4, 5])
      ret = map(lambda x: x + 100 if x % 2 == 1 else x, [1, 2, 3, 4, 5])   
      print(ret)
      for i in ret:
          print(i)
      # =============================================
      # 排序 sorted
      li = [1, 211, 33, 23] 
      li.sort()
      print(li)
      new_list = sorted(li)
      print(new_li)
      

    相关文章

      网友评论

          本文标题:python入门 第八天 lambda和内置函数

          本文链接:https://www.haomeiwen.com/subject/cpcqiftx.html