Python built-in functions

Source: Internet
Author: User
Tags vars

Python built-in functions

Official documentation: https://docs.python.org/3/library/functions.html

Abs (absolute value)

Print (abs (-5) # output: 5

All (to determine whether each element of the iteratable object is True, if one element is 0, None, empty character '', empty list, empty tuples, and empty dictionary, False is returned)

Iteratable objects: objects that can be traversed cyclically.

Print (all ([]) # Empty list, return Trueprint (all (['',]) # One of the list elements is empty, returns Falseprint (all (0, 1, 2) # returns False if one of the tuples is 0. # output: True # False

Any (judge whether each element of the iteratable object is True, opposite to the all function)

Print (any ([0, '', None, {}, 1]) # returns true print (any ([0,'', None, {}]) # False only when all elements are False # output: True # False

Ascii (returns an object in ASCII format. It is not ascii, and \ x, \ u, \ U, and other characters are output)

Print (ascii ('hello') print (ascii ('Chinese') # output: 'hello' # '\ u4e2d \ u6587'

Bin (returns binary data)

Print (bin (57) # output: 0b111001

Bool (return Boolean value)

Print (bool (5) # output: True

Bytearray (returns a byte array)

Print (bytearray (3) print (bytearray ('hello', encoding = 'utf-8') # specify the encoding print (bytearray ('Chinese ', 'utf-8') print (bytearray ([0,255, 33]) # If the parameter is of iteration type, the element must be an integer in []; # output: bytearray (B '\ x00 \ x00 \ x00') # bytearray (B 'hello') # bytearray (B '\ xe4 \ xb8 \ xad \ xe6 \ x96 \ x87 ') # bytearray (B '\ x0b \ x16! ')

Bytes (returned bytes)

Print (bytes (3) # output: B '\ x00 \ x00 \ x00'

Callable (check whether the object can be called)

Def test (): print ('sudoed') n = 5 print (callable (test) print (callable (n) # output: True # False

Chr (convert a number to a character in the ASCII code)

Print (chr (65) # output:

Ord (convert the characters in the ASCII code into numerical values)

Print (ord ('A') # output: 65

Classmethod

# Placeholder

Compile (Compilation)

# Placeholder

Complex (returns the plural value)

# Placeholder

Delattr

# Placeholder

Dict (create dictionary)

D = dict (a = 1, B = 2) print (d) # output: {'B': 2, 'A': 1}

Dir (view the functions provided by an object or module)

Print (dir (str) # output: ['_ add _', '_ class __', '_ ins INS __',...... 'translate', 'uppper ', 'zfill']

Divmod (input divisor and divisor, return operator and remainder)

Print (divmod (13, 5) # output: (2, 3)

Enumerate)

Name = ['Tom ', 'Lucy', 'ben'] for I, item in enumerate (name, 10): # You can specify the start value, print (I, item) from 0 by default # output: 10 Tom #11 Lucy #12 Ben

Eval (execute the expression in the string)

Print (eval ('5 * 10') # output: 50

Exec (execute the code in the string)

Exec ('a = 2 + 3') print (a) # output: 5

Map (traverses the elements of the iteratable object, passes each element into the specified method or function for execution, and generates the iteratable object)

Def func (a): return a * 2li = [11,22, 33,44] new_li = map (func, li) print (list (new_li) # new_li is a map object, output results can only be converted to a list. # output: [22, 44, 66, 88]

Filter (use a specified method or function to filter elements of the iteratable object and generate a new iteratable object)

Def func (a): if a> 50: return True else: return Falseli = [33,44, 55,66, 77] new_li = filter (func, li) print (list (new_li )) # new_li is a filter object. The result can be output only when it is converted to a list. # output: [55, 66, 77]

Float (convert a number to a float type)

Print (float (22) # output: 22.0

Format (format output)

Print (format (9, 'B') # convert to binary, and output 1001 print (format (9, 'O') # convert to octal, output 11 print (format (9, 'D') # convert to decimal, output 9 print (format (12, 'x') # convert to hexadecimal, lower-case display, output cprint (format (12, 'x') # convert to hexadecimal, upper-case display, output Cprint (format (65, 'C ')) # convert to ASCII and Output

Getattr

# Placeholder

Globals (returns all current global variables)

A = 3 print (globals () # output: {'A': 3, '_ package _': None, '_ builtins __'...... '_ name _': '_ main __'}

Hasattr

# Placeholder

Hash (obtain the object's hash value)

Print (hash ('xxoo ') # output: 1068037005

Help (obtain help information of an object)

print(help(dict))

Hex (hexadecimal output of integers)

Print (hex (123) # output: 0x7b

Id (view the memory address of the variable)

A = 3 print (id (a) # output: 490363184

Input (get user input value)

Name = input ('Please input your name: ') print (name) # Run, please input your name: Tom # output: Tom

Int (create an integer value)

a = int(1314)

Isinstance

# Placeholder

Issubclass

# Placeholder

Iter

# Placeholder

Len (get object length)

A = [1, 2, 4] print (len (a) # output: 4

List (create a list)

li = list([1,2,3,4])

Locals (returns all current local variables)

print(locals())

Max (obtain the maximum value)

Print (max (2, 4, 6, 8) # output: 8

Min (obtain the minimum value)

Print (min (2, 4, 6, 8) # output: 2

Next

# Placeholder

Object

# Placeholder

Oct (back to octal)

Print (oct (12) # output: 0o14

Open (open a file)

# Placeholder

Pow

# Placeholder

Print (print Output)

print(123)

Property

# Placeholder

Range (create a continuous integer sequence)

For I in range (): print (I) # print numbers from 1 to 9

Repr

# Placeholder

Reversed)

N = [1, 2, 3, 4, 5] m = reversed (n) print (list (m) # m is a reversed object, which must be converted to list output # output: [5, 4, 3, 2, 1]

Round (evaluate the value after rounding)

Print (round (4.3) print (round (4.8) # output: 4 #5

Set)

N = set ([1, 2, 3, 3]) print (n) # output: {1, 2, 3}

Setattr

# Placeholder

Slice

# Placeholder

Sorted)

S = [5, 2, 7, 9, 3] print (sorted (s) # output: [2, 3, 5, 7, 9]

Staticmethod

# Placeholder

Str (create a string)

s = str('123abc')

Sum (sum)

Print (sum ([1, 2, 3]) # output: 6

Super

# Placeholder

Tuple (create a tuples)

t = tuple([1,2,3])

Type (view object types)

N = 123 print (type (n) # output: <class 'int'>

Vars (view the functions provided by an object or module, return dictionary, and dir is the return list)

print(vars(str))

Zip (combines all input iteration objects with elements at the same position to form a new iteration object)

X = [1, 2, 3] y = [6, 7, 8, 9] z = zip (x, y) print (list (z) # obtain the shortest sequence # output: [(1, 6), (2, 7), (3, 8)]

 

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.