Built-in functions from entry to the autistic Python

A built-in function

  1. eval: type of code execution string

  2. exac: type of code execution string Society

    and prohibit the use of eval exac

  3. hash () function is to distinguish between variable data types and data type immutable

    # print(hash("123"))
    # print(hash(12))
    # print(hash(-1))
    # print(hash(-10))
    # print(hash((2,1)))
    
    # dic = {[1,2,3]:2}
    # print(hash([1,2,3]))
  4. help (): View help information

  5. callable (): Check whether the object can be called,

    # def func():
    #     print(1)
    # lst = [1,23,4,]
    # print(callable(lst))   # 查看对象是否可调用
  6. int (): string or integer numbers into

  7. float (): converted to floating point

  8. complex (): plural

  9. bin (): Decimal Binary transfer

  10. oct (): Decimal turn octal

  11. hex (): Decimal turn hex

  12. divmod (): calculated result divisor and dividend, the quotient and remainder comprising a Ganso

  13. round (): retention of floating-point decimal places, the number of bits can be set to retain, retain the default integer

  14. pow (): power demand x ** y (when the result of the three parameters of x ** y modulo third parameter)

  15. bytes (): for switching between different coding recommended encode

  16. ord (): Get the current position of the epitope encoded by elemental

  17. chr (): find the corresponding bit number table by elements

  18. repr (): View original ecological data (for programmers)

  19. all (): Summary of the container is determined whether the element are true, returns true

  20. any (): Analyzing container elements have a true, it is True

Anonymous function: line of the function lambda (Bibei)

def func():
    print(123)
func()
  1. grammar:

    1. Function name = lambda parameter: Return value

    2. The name of the anonymous function is called lambda

    3. lambda is a keyword-defined functions, the equivalent def function

    4. Only one data type can return,

      面试题:
      print ([lambda : i for i in range(5)])
      (返回5个内存地址)
      
      print(lst[0]())---结果是4--因为循环最后一次输出是4,调用的全局最后一个就是4
      不加后面的小括号就是调用内存地址
      加了括号就是调用函数
      lst = [lambda X : X+1 for i in range(5)]
      print(lst[0](5))
      输出结果为:6,返回值是x+1
      tu = (lambda : i for i in range(3))
      print(tu[0])  #输出错误,不能索引
      print(tu)#输出内存地址
      print(next(tu))       #一个函数地址
      print(next(tu)())     #输出0
      print(next(tu)())     #输出1            
      lst = [lambda : i for i in range(3)]
      print(lst[0]())
      tu = (lambda : i for i in range(3))
      print(next(tu)())
      输出结果:
      2
      0

      Body of the function is stored in the code

      Generator is stored in the code

      - Cause: yield and lead to inconsistent execution result of the function generator

      lst = [lambd x:x+5  for i in range(2)]
      print([i(2) for i in lst])
      
      
      解开顺序:
      lst = []
      for i in range (2):
          lst.append(lambda x :x+5)
      new_list
      for i in lst:
          print(i)      #两个函数的内存地址
          new_list.append(i(2))
      print(new_list)       #输出列表【7,7】
      
      输出结果:【7,7】
      
      
      
      lst = (lambda x:x+5  for i in range(2))
      print([i(2) for i in lst])
      
      
      
      输出结果:【7,7】
      
      lst = (lambda x:x*i  for i in range(2))
      print([i(2) for i in lst])        #【0,2】
      
      
  2. lambda == def == keyword

    1. lambfa x: x
      • x: shape parameter is a function of the ordinary (location, default), any number can be written, can not write
      • : Behind the ordinary is the return value of the function must be written, no default, must have a return value, you can only write one data type

Two built-in functions (remember all)

Built-in functions

  1. Two dictionaries combined into one:

    1. update--- dic2.update(dic1)
    2. Broken: - print (dict (** dic1, ** dic2))
    3. print (dict ([(1,2), (3,3)])) --- brackets at least one of a digital versatile will not, iterate through the list may be ancestral
    4. dict( ** dict1, ** dict2)
  2. set (): The iterables converted into ancestral

  3. sep (): a method for dividing between each element

    print(1,2,3,sep = "|")
    输出结果:1|2|3
  4. end: print after the implementation of the end of the statement, the default \ n

    print(1,2,3,end = "")
    print()
    输出结果:全部在一行
  5. print (): screen output

    • flush - Flush
  6. file: file handles, the default display to the screen

    print (1,2,3,4,5,file = open ("test","w",encoding="utf-8"))
  7. sum () - sum, the object must be iterative, object elements must be an integer, string type can not be used

    print (sum ([1,2,3,1]))
    print (sum ([1,2,3,1],100)       #100是起始值,就是从100开始进行取和,指定开始位置的值
  8. abs (): Returns the absolute value - converted into a positive number, whether you are not negative

  9. dir (): View the current object has any way

  10. zip (): the fastener, when the length is inconsistent iterables choose the shortest merge may be a plurality of

    面试题:
    list1 =[1,2,3,4]
    lst2 = ["alex","wusir","meet"]
    print(list(zip(lst1,lst2)))
    
    输出结果:[(1,"alex"),(2,"wusir"),(3,"meet")]
    
    print(dict(zip(lst1,lst2))) #返回一个字典
    输出结果:{1:"alex",2:"wusir",3:"meet"}
  11. format (): format conversion

    1. Alignment

      print(format("alex",">20"))  #右对齐
      print(format("alex","<20"))  #左对齐
      print(format("alex","^20"))  #居中
    2. Hexadecimal conversion

      print(format(10,"b"))        #十进制转二进制
      print(format(10,"08b"))      #不够就补0
      print (format(10,"08o"))     #oct,八进制
      print (format(10,"08x"))     #hex,十六进制
      print(format(0b1010,"d"))    #二进制转十进制
  12. reversed (): an inverted sequence, flipped sequence returns an iterator

    l = reversed('你好')  # l 获取到的是一个生成器
    print(list(l))
    ret = reversed([1, 4, 3, 7, 9])
    print(list(ret))  # [9, 7, 3, 4, 1]

Guess you like

Origin www.cnblogs.com/heyulong1214/p/11528015.html