python-built-in functions

Source: Internet
Author: User

First, compile ()

Compiles a string into Python code.

Compiled mode, single,eval,exec

" print (123) "  = Compile (S,"<string>","exec")   # Compile the string s into Python code and let exec () execute.  exec(R)

Second, EXEC ()

Executes the Python code with no return value, i.e. no result. Which means return none.

You can either receive the code or the string can be executed. That is, if the Python code is received, it will be executed directly. If a string is received, then exec () compiles the string into Python code and executes the code.

" " Example 1: Receive a string, first convert the string to Python code, and then execute the code " "  "print (123)"exec(s)
Output Result: print (123) executed
123
"Example 2: Received code, execute the code directly"
The first is compile () converts the S string into code, and then let exec () execute the "
"Print (123)"
Compile (S,"<string>","exec")
EXEC (R)
Output Result:
123
Example 3: No return value, i.e. no result. can only return "none"
"Print (123)"
Compile (S,"<string>","exec")
EXEC (R)

Output Result:
None

Third, Eval ()

Executes the code, has a return value, converts the string to an expression, evaluates it, and returns the result

" " Example 1: Converting a string s into code and executing an expression of s " "  "8*8"= eval (s)print(ret)
Output Result:
64

Note: EXEC () has no return results

Eval () has a return value

Iv. dir ()

Quick view of what features the object provides

Print (dir (list))
Output Result:

V.Divmod (A, b)

Blog Paging needs:

If there are 99 pages, each page shows 10 pages, how many pages will be required in total

Divmod () generates data of the tuple type.

" " Example 1: Get the value of Divmod () " "  # generated is a tuple type print(R,type (R))print(r[0])Print (r[1])
Output Result:
(9, 8) <class ' tuple ' >
9
8

" " Example 2: Get the value of N1,n2 " " = divmod (98,10)print(n1)print(n2)
Output Result:
9
8

Vi.filter ()

Filter the elements in the sequence, and finally get the sequence that matches the condition!

How to use:

Filter (functions, objects that can be iterated)

filter, which loops the second argument, lets each loop element execute a function if the function returns a value of true, indicating that the element is valid.

Requirement: Gets an element that is greater than 22 in a list.

    " def   F1 (args): Result_list  =< Span style= "color: #000000;" > []  for  item in   args:  if  item > 22: Resul T_list.append (item)  return   Result_ Listlist1  = [11,22,33,44,55]r  = F1 (List1  print  (r) 

Output:
[[]]
 
' Example 3: Using the filter function with Lambda '
List1 = [One, one,55]
Filter (lambda a:a>22,list1)
Print (list (ret))


Output Result:
[33, 44, 55]

Using the 3 examples above, it is easiest to find example 3.

Seven, Map ()

Iterates through the sequence, operates on each element of the sequence, and finally gets a new sequence.

Map  # Adds a function return element to the result



Explanation: In Python, the most basic data structure is the sequence (sequence). Each element in the sequence is assigned an ordinal-that is, the position of the element, also known as an index. The first index is 0, the second one is 1, and so on. The last element in the sequence is marked as-1, the penultimate element is-2, and so on.
Python contains 6 built-in sequences, including lists, tuples, strings, Unicode strings, buffer objects, and Xrange objects.


python-built-in functions

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.