Python built-in functions

Source: Internet
Author: User
Tags bbcode

Python built-in functions

Python built-in functions

I. mathematical operations

 

Abs (x) Absolute Value
1. The parameter can be an integer or a plural value.
2. If the parameter is a plural value, the modulo of the plural value is returned.
Complex ([real [, imag]) Create a plural number
Divmod (a, B) Separate operator and remainder
Note: integer and floating point types are supported.
Float ([x]) Converts a string or number to a floating point number. If no parameter exists, 0.0 is returned
Int ([x [, base]) Converts a character to the int type. base indicates hexadecimal
Long ([x [, base]) Converts a character to the long type.
Pow (x, y [, z]) Returns the Power y of x.
Range ([start], stop [, step]) Generates a sequence, starting from 0 by default.
Round (x [, n]) Rounding
Sum (iterable [, start]) Sum a set
Oct (x) Convert a number to an octal number.
Hex (x) Convert integer x to a hexadecimal string
Chr (I) Returns the ASCII character corresponding to integer I.
Bin (x) Convert integer x to a binary string
Bool ([x]) Convert x to Boolean type

 

Ii. collection operations
Basestring () Str and unicode superclass
It cannot be called directly. It can be used as an isinstance for judgment.
Format (value [, format_spec]) Format the output string
The formatted parameter sequence starts from 0, for example, "I am {0}, I like {1 }"
Unichr (I) Returns unicode of the given int type.
Enumerate (sequence [, start = 0]) Returns an enumerated object. The next () method of this object returns a tuple.
Iter (o [, sentinel]) Generate an object iterator with the second parameter representing the Separator
Max (iterable [, args...] [key]) Returns the maximum value in the set.
Min (iterable [, args...] [key]) Returns the minimum value in the set.
Dict ([arg]) Create a data dictionary
List ([iterable]) Converts a collection class to another collection class.
Set () Set Object Instantiation
Frozenset ([iterable]) Generate an unchangeable set
Str ([object]) Convert to string type
Sorted (iterable [, cmp [, key [, reverse]) Sort a group
Tuple ([iterable]) Generate a tuple type
Xrange ([start], stop [, step]) The xrange () function is similar to range (), but xrnage () does not create a list, but returns an xrange object. Its behavior is similar to the list, however, the list value is calculated only when needed. when the list is large, this feature can save us memory.

 

Iii. logical judgment
All (iterable) 1. All elements in the set are true
2. In particular, True is returned if it is an empty string.
Any (iterable) 1. One element in the set is true.
2. In particular, if it is an empty string, False is returned.
Cmp (x, y) If x <y, negative number is returned. If x = y, 0 is returned. If x> y, a positive number is returned.

 

Iv. Reflection
Callable (object) Check whether the object is callable
1. classes can be called.
2. The instance cannot be called unless the _ call _ method is declared in the class.
Classmethod () 1. annotation, used to indicate that this method is a Class Method
2. A class method can be called by a class or an instance.
3. The class method is similar to the static method in Java.
4. The self parameter is not required in class methods.

Compile (source, filename,

Mode [, flags [, dont_inherit])

Compile source as a code or AST object. Code objects can be evaluated using exec statements or eval.
1. Parameter source: string or AST (Abstract Syntax Trees) object.
2. filename: name of the code file. If the code is not read from the file, some identifiable values are passed.
3. Parameter model: specifies the type of the compiled code. You can specify it as 'exec ', 'eval', and 'singles '.
4. Parameters flag and dont_inherit
Dir ([object]) 1. If no parameter is specified, a list of variables, methods, and definitions in the current range is returned;
2. When a parameter is included, the attribute and method list of the parameter are returned.
3. If the parameter contains method _ dir _ (), this method will be called. When the parameter is instance.
4. If the parameter does not contain _ dir _ (), this method collects parameter information to the maximum extent.
Delattr (object, name) Delete the property of the object named name
Eval (expression [, globals [, locals]) Calculates the expression value.
Execfile (filename [, globals [, locals]) The usage is similar to exec (). The difference is that the filename parameter of execfile is the file name, while the exec parameter is a string.
Filter (function, iterable) Construct a sequence, equivalent to [item for item in iterable if function (item)]
1. function: a function with a return value of True or False, which can be None.
2. Parameter iterable: sequence or iteratable object
Getattr (object, name [, defalut]) Obtains the attributes of a class.
Globals () Returns a dictionary describing the current global symbol table.
Hasattr (object, name) Determines whether an object contains a feature named name.
Hash (object) If the object is of the hash table type, the hash value of the object is returned.
Id (object) Unique Identifier of the returned object
Isinstance (object, classinfo) Judge whether the object is a class instance
Issubclass (class, classinfo) Determine whether it is a subclass
Len (s) Returns the length of the set.
Locals () Returns the current variable list.
Map (function, iterable ,...) Traverse each element and perform function operations
Memoryview (obj) Returns an object of the memory image type.
Next (iterator [, default]) Similar to iterator. next ()
Object () Base Class
Property ([fget [, fset [, fdel [, doc]) The packaging class for Attribute access. After setting, you can access setter and getter through c. x = value.
Reduce (function, iterable [, initializer]) The merge operation starts with the first two parameters, then the first two results are merged with the third, and so on.
Reload (module) Reload the module
Setattr (object, name, value) Set attribute values
Repr (object) Change an object to a printable format
Slice ()  
Staticmethod Declares a static method. It is an annotation.
Super (type [, object-or-type]) Reference parent class
Type (object) Returns the type of the object.
Vars ([object]) Returns the variable of the object. If no parameter exists, it is similar to the dict () method.
Bytearray ([source [, encoding [, errors]) Returns a byte array.
1. If source is an integer, an initialized array with the source length is returned;
2. If source is a string, the string is converted to a byte sequence based on the specified encoding;
3. If source is an iteration type, the element must be an integer in [0,255;
4. If source is an object consistent with the buffer Interface, this object can also be used to initialize bytearray.
Zip ([iterable,...]) I did not understand it. I just saw the changing aspects of the matrix.

 

V. IO operations

 

File (filename [, mode [, bufsize]) The file Type constructor is used to open a file. If the file does not exist and the mode is write or append, the file will be created. Add 'B' to the mode parameter to operate the file in binary format. Adding '+' to the mode parameter will allow simultaneous read/write operations on the file
1. filename: file name.
2. Parameter mode: 'R' (read), 'w' (write), and 'A' (append ).
3. Parameter bufsize: If it is 0, it indicates no buffer. If it is 1, it indicates row buffering. If it is a number greater than 1, it indicates the buffer size.
Input ([prompt]) Get user input
Raw_input is recommended because this function will not capture users' incorrect input.
Open (name [, mode [, buffering]) Open a file
What is the difference with file? Open is recommended.
Print Print function
Raw_input ([prompt]) Set input. All input values are processed as strings.

 

 

6. open --- open, close, close

Mode description r open a file as read-only. The file pointer is placed at the beginning of the file. This is the default mode. A rb file can only be read in binary format. The file pointer is placed at the beginning of the file. This is the default mode. R + open for reading and writing files. The file pointer is placed at the beginning of the file. Rb + open the file used to read and write binary data. The file pointer is placed at the beginning of the file. W open a file and write only. If the file overwrites the file. If the file does not exist, a new file is created and written. A wb file can only be written in binary format. If the file overwrites the file. If the file does not exist, a new file is created and written. W + opens the file in the write and read modes. If the file exists, overwrite the existing file. If the file does not exist, create a new file for read/write operations. Wb + enables writing and reading files in binary format. If the file exists, overwrite the existing file. If the file does not exist, create a new file for read/write operations. A. open the file to be appended. The file pointer is at the end of the file. That is, the file is in append mode. If the file does not exist, it will create a new file for writing. The AB open file is used to append the file in binary format. The file pointer is at the end of the file. That is to say, the file is in the append mode. If the file does not exist, it will create a new file for writing. A + the method for opening an object is append and read. The file pointer is at the end of the file. This file is opened in append mode. If the file does not exist, it will create a new file for read/write operations. AB + opens a file in the additional and binary format read mode. If the file has a file pointer at the end of the file. This file is opened in append mode. If the file does not exist, it will create a new file for read/write operations.

(1) read-only, r
F = open ("path file", "r") f. write ("asdfasdf") f. close ()

(2) w write-only mode [unreadable; create if the file does not exist; empty if the file exists]

F = open ("ha1.log", 'A') a1 = f. tell () --- view the pointer position print (a1) f. write ("nini") ---- if the file is cleared, "nini" a = f. tell () ---- view the pointer print (a) f. seek (0) # c = f. read () ---- unreadable # print (c) f. close () ----------- close # print (c)

(3) x is not readable. If x does not exist, it is created. If yes, an error is returned.

F = open ("ha3.log", 'x') a1 = f. tell () print (a1) f. write ("nini") a = f. tell () print (a) f. seek (0) # c = f. read () --- = unreadable # print (c) f. close ()

(4). a is not readable. If it does not exist, it is created. If it exists, it is appended.

F = open ("ha3.log", 'A') a1 = f. tell () print (a1) f. write ("cccc") ----- "nini" has been written above. If yes, only content a = f is appended. tell () print (a) f. seek (0) # c = f. read () # print (c) f. close ()

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.