Built-in functions Python list of basic data types far as to look at the dictionary text file input and output cycle design cycle target function object

 

 

Author: Vamei

Source: http://www.cnblogs.com/vamei 

 

Python built (built-in) functions as running python interpreter created. In the Python program, you can always call these functions need not be defined. The most common built-in functions are:

print("Hello World!")

In Python tutorial , we have already mentioned some of the following built-in functions:

Basic data types  type ()

Far as to look at the  dir () help () len ( )

Dictionary  len ()

Input and output text file  open ()

Cycle design  range () enumerate () zip ( )

Cyclic object  ITER ()

Function object  map () filter () reduce ( )

 

Now I take are the actual parameters, you can try to effect directly on the command line.

computation

abs (-5) # absolute value, i.e. 5

round (2.6) # rounded to the nearest integer, i.e. 3.0

pow (2, 3) # 2 ** 3 corresponds, if POW (2, 3, 5), corresponding to 3% 5 2 **

cmp size (2.3, 3.2) # compare two numbers

divmod (9,2) # returns the result of the division and the remainder

max ([1,5,2,9]) # selecting the maximum value

min ([9,2, -4,2]) # for the minimum

sum ([2, -1,9,12]) # summation

 

Type Conversion

int ( "5") # convert integer to integer

float (2) # is converted to float float

long ( "23") # long integer into a long integer

str (2.3) # string into a string

complex (3, 9) # 3 + 9i returns the complex

 

ord ( "A") # "A" corresponding to the character value

chr (65) # 65 corresponding to a character value

(65) # 65 unichr value corresponding unicode character

 

bool (0) # is converted to the corresponding truth value in Python, corresponding to 0 False

In Python, the following objects are equivalent False: [], (), {}, 0, None, 0.0, ''

 

bin (56) # Returns a string that represents a binary number of 56

hex (56) # Returns a string that represents a hexadecimal number of 56

oct (56) # returns a string representing the octal number 56

 

list ((1,2,3)) # conversion table list

tuple ([2,3,4]) # conversion table is a fixed value tuple

slice (5,2, -1) # constructing the target object slice

dict (a = 1, b = "hello", c = [1,2,3]) # Construction dictionary Dictionary

 

Sequence Action

all ([True, 1, "hello!"]) # if all the elements are equivalent to the value True

any ([ "", 0, False, [], None]) # whether any element equivalent to a value of True

 

 

sorted ([1,5,3]) # Returns the positive sequence of the sequence, i.e. [3,5]

reversed ([1,5,3]) # return sequence in reverse order, that is, [3,5,1]

 

Classes, objects, properties

Copy the code
# define class
class Me(object): def test(self): print "Hello!"
def new_test():
    print "New Hello!"
me = Me()
Copy the code

Whether hasattr (me, "test") # check me test object has properties

getattr (me, "test") # returns the test property

setattr (me, "test", new_test) # property to the test new_test

delattr (me, "test") # delete test property

isinstance (me, Me) # me Me object class object is generated (an instance)

issubclass (Me, object) # Me class is a subclass of object classes

 

Compile, execute

repr (me) # Returns the string representation of the object

compile ( "print ( 'Hello')", 'test.py', 'exec') # compiled code string becomes the object

eval ( "1 + 1") # explained string expression. Parameter may be compile () code object returned

exec ( "print ( 'Hello')") # string is interpreted and executed, print ( 'Hello'). Parameter may be compile () code object returned

 

other

input ( "Please input:") # waiting for input

 

globals () # Returns the global namespace, such as a global variable name, the name of a global function

locals () # Returns the local namespace

Guess you like

Origin www.cnblogs.com/-wenli/p/11242137.html