Python built-in functions, anonymous functions

Source: Internet
Author: User
Tags abs

What is a built-in function?

Is what Python provides you with functions that you can use directly, such as Print,input and so on. This is actually the built-in function that the Python interpreter automatically generates when we create the. py, just like the space we learned before.

The built-in space is created automatically by the Python interpreter when the Python file is created.

important built-in functions: ***

Eval:

Eval: Executes the string type of code and returns the final result. (It's just a matter of manipulating the type inside a string, such as the addition and multiplication of the int type in your string). If the list of things in the string or dictionary directly returns the list dictionary as it is.

Print (Eval ('3+4'))  # 7 Print (Eval ("{' name ': ' Old Nanjing ', ' age ': +}"#{' name ': ' Old Nanjing ', ' age ': +} Print (Eval ("[' 1+3 ']"#  [' 1+3 ']

EXEC: Executes the string type of code, the process statement. is the execution of the content within the string is executed out (the only thing you string in is a display that does not execute exec is to give it to execute)

" " Li = [1, 2, 3]for I in Li:    print (i)'exec(ret)  # is to put all the execution within the string in this way outside of the string.

Sum: Computes the sum of the objects that can be iterated (the initial values can be set). But up to two elements can be added

Print # Ten Print # 106 Print (Sum ({1:2,3:4})) # The dictionary just adds the key value .

Max: Returns the maximum value of an object that can be iterated (Key,key is the function name, and the maximum value is returned by the rule of the function)

Print # 4 Print # -9  abs denotes absolute value

The use of Min and Max is the same, except that it returns a minimum of two, which can only be used for an iterative object.

Print (Min ((1,2,3,4,5))) Print (Min ({3:2,4:1}))  # Dictionary use can only be compared to the key of the dictionary

Reversed: Flips a sequence and returns an iterator for this flip sequence. Is that you use reversed to manipulate an iterative formation and turn it into an iterator.

RET  =reversed (['a'b', 1, 2, 3])                                  Print  (ret)                                                           print(ret.__next__())  #  Because reversed has transformed an iterative object into an iterator, you need to use __next__ to take the value  

Repr: Returns the string form of an object (the True colors). is what form is returned within the string.

Print (Repr ('{"name": "Alex"}'))  # The string contains a dictionary that is returned  because the dictionary is also placed within the string and it is    the string of print(repr  '))                                                      

Sorted a sort operation on all objects that can be iterated. Add key It's more of a key than sort to define how to flip

Li = [1,-2,-7, 8, 5, -4, 3]                                      print(sorted (LI))          # from small to large                                 sort print # sort                         from big to small Print (Sorted (li, reverse = True, key =abs))      # sort         by absolute value

The enumerate enumeration is the return of an enumerated object (0, seq[0]), (1, seq[1]), (2, seq[2]),

Li = ['old boy','Alex','Wusir','Sister','Zhaoyun']#returns an enumeration object you one by one print the corresponding serial number for all values in this enumeration object forKvinchEnumerate (LI):#0 Old Boys    Print(K,V)#1 Alex                             #2 Wusir                             #3 Sister                             #4 Zhaoyun


L1 = (' Laowang ', ' Zhangsan ', 1, 2, 3,4)
For I in Enumerate (L1):
Print (i)

The zip Zipper method is a combination of the shortest possible iterative object, which can only be combined from top to bottom and by the shortest iteration object.

The function is used to wrap an object that can be iterated as an argument, package the corresponding element in the object into a tuple, and then return a list of these tuples. If the number of elements in each iterator is inconsistent, the returned list is the same length as the shortest object.

L1 = [1, 2, 3, 4, 5] L2= (5, 6, 7,) L3=['Laowang','SB', 5, 6]                                  #print (Zip (L1, L2, L3)) forIinchZip (L1, L2, L3):#(1, 5, ' Laowang ')    Print(i)#(2, 6, ' SB ')                                 #(3, 7, 5)                                                             

Filter: Filtering •. is to provide a place where you can pass in two conditions and iterate over objects to judge

def func (x):return x%2 = =             0= Filter (Func,[1, 2, 3, 4, 5, 6, 7])  #func是判断条件 behind is the need to determine Content print(ret)                              for in  ret                              :print(i)                            

Map: The specified sequence is mapped according to the provided function. Using a map also creates an adorner that needs to be __next__ or converted to a list to take a value.

def s (x):                      return x**2           =map (S, [1,2,3,4,5])  print(ret.__next__())      Print (ret.__next__())     
Anonymous functions:

Anonymous functions: A sentence function designed to address the simple requirements of functions.

# this piece of code def Calc (n):     return n**nprint(calc# to anonymous function lambda n:n** N Print (Calc (10))


Res=map (Lambda x,y:x+y,[1, 3, 5, 7, 9],[2,4,6,8])   #就是把两个列表相加一起  But the sum of one by one corresponds
Print (res.__next__ ())
Print (res.__next__ ())
Print (res.__next__ ())
Print (res.__next__ ()) #但是 cannot exceed that minimum mapping

Lambda parameters: Return value # arguments can have multiple, separated by commas # Anonymous functions No matter how complex the logic, can only write one line, and the end of the logical execution of the content is the return value # The return value can be any data type as well as a normal function

As we can see, anonymous functions do not really have names.

There is no difference between the invocation of an anonymous function and a normal invocation. is the function name (parameter) can be.

An example of an anonymous function and a built-in function:

res = filter (lambda x:x>10,[5,8,9,11,13])    # for i in res:                              # Print (      i)                                Print(res.__next__())                         Print(res. __next__ ())                                                                       

Python built-in functions, anonymous functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.