Beginner python--introduce some built-in methods

Source: Internet
Author: User
Tags iterable

1, ABS () to seek absolute value
A=abs ( -10)print(a)#  output:
2. All ()

Used to detect whether the list element is all empty, 0, False

Print # true if all elements of the list are empty, false, or 0 o'clock, the return value is False Print # cannot test dictionary, so returns True
# output: False True
3. Any ()

Used to detect if the list is all not empty, False, 0

Print # true if all elements of the list are not NULL, FALSE, or 0 o'clock, otherwise false # Output: True
4. Bin ()

Converts an integer or long shape into a binary number

Print # output:0b1010
5, callable ()

Used to check whether an object can be called

Print (Callable (a))  # Output:false
6, Chr () and old ()

This is: Returns the ASCII character corresponding to the number, and returns the ASCII value corresponding to the character.

Print (CHRprint(ord ("w"#  output:a  119
7. Dir ()

Output all built-in methods for this data type

b={}print#output: [' __class__ ', ' __contains__ ', ' __delattr__ ', ' __delitem_ _ ', ' __dir__ ', ' __doc__ ', ' __eq__ ', ' __format__ ', ' __ge__ ', ' __getattribute__ ', ' __getitem__ ', ' __gt__ ', ' __hash__ ', ' __ Init__ ', ' __init_subclass__ ', ' __iter__ ', ' __le__ ', ' __len__ ', ' __lt__ ', ' __ne__ ', ' __new__ ', ' __reduce__ ', ' __reduce_ Ex__ ', ' __repr__ ', ' __setattr__ ', ' __setitem__ ', ' __sizeof__ ', ' __str__ ', ' __subclasshook__ ', ' clear ', ' copy ', ' Fromkeys ', ' get ', ' items ', ' Keys ', ' Pop ', ' Popitem ', ' setdefault ', ' Update ', ' values '
8. EXEC ()

Executes a string dynamically, for example:

x = 10Expr=" "z = 30sum = x + y + zprint (sum)" "defFunc ():#exec executes a piece of code dynamicallyy = 20exec(expr)exec(Expr, {'x': 1,'y': 2})#Modify the data based on the original code to get a different result    exec(Expr, {'x': 1,'y': 2}, {'y': 3,'Z': 4}) func ()#Output:60 33 34
9. Lambda

anonymous functions

c=Lambda# anonymous function print# anonymous function call and pass the parameter print(lambda  # parameter invocation of anonymous functions
# Output: 7
10. Filter ()

The function of the filter function is to filter out the data you want.

The function of the Res=filter (Lambda#filter function is to filter out the data you want for the in   Res:     Print (i)

Output: 6 7 8 9

11, Globals ()

Output all the variables of the entire program, returning a dictionary. The variable name is key and the value is

12, Max (), Min ()

Returns the maximum and minimum values in the list

d=[5,6,12]print# returns the maximum value in the list print# Returns the minimum value of the table

Output: 12 5

13. Oct ()

Converts a decimal number to eight binary numbers

Print (Oct (5)) Print (Oct  ) # Convert decimal to 8 binary # Output: 0o50o12
14. Pow ()

Calculate the exponentiation

Print # 2 of the 8-time Square #  the
15, round ()

Keep floating-point numbers in n decimal places

Print # Keep floating-point numbers 3 decimal places # output:3.568
16. SUM ()

Sum the list

Print # list Sum #  at
17, Sorted ()

Sort all the objects that can be iterated

Sorted (iterable, Cmp=none, Key=none, Reverse=false)--New sorted list

Iterable: is an iterative type;
CMP: A function For comparison, comparing what is determined by key;
Key: A property or function of a list element that is used as a keyword with a default value that iterates over an item in the collection;
Reverse: Sorting rules. Reverse = True descending or reverse = False Ascending, default ascending;
Return value: Is a sorted, iterative type, as with iterable.

(1) Sort by element length

L = [{1:5,3:4},{1:3,6:3},{1:1,2:4,5:6},{1:9}]def  F (x):    return  len (x) Sort (Key=print  (L)#  Output:[{1:9}, {1:5, 3:4}, {1:3, 6:3}, {1:1, 2: 4, 5:6}]

(2) Sort by one of the values in the element, for example:

Students = [(' John ', ' A ', '), (' Jane ', ' B ', '), (' Dave ', ' B ', 10),]

Sort by age:

① using the key function to sort

Sorted (students, key=Lambda x:x[2#

② using CMP functions to sort

Sorted (students, cmp=Lambda x,y:cmp (x[2], y[2]  )#
18. zip ()

Pull two lists together to form a zip data type

a=[1,2,3,4]b=["a","b","C","D"]Print(Zip (A, b))#pull two lists together to form a zip data type forIinchZip (A, b):Print(i)#<zip object at 0x00000249f0946d48>
19, __import__ ()

Another way to invoke the module, which is written in parentheses, is the module's name string

Beginner python--introduce some built-in methods

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.