Python series-python built-in functions

Source: Internet
Author: User
Tags assert chr iterable ord

abs (x) Returns the absolute value of a number, which can be an integer or a floating point. If it is a complex number, it is returned in size all (iterable) Iterates over all elements in the parameter, returns true if all elements are true, equivalent to a function of
def Any (iterable):        for inch iterable:           if element:               return True       return False  

Any (iterable)Iterates over all elements in the parameter, and returns True if an element is true. function is equivalent to
def Any (iterable):        for inch iterable:           if element:               return True       return False  

Assert

asserts that the function belongs to an expression of a cell operation. Similar to lambda. But functions are similar to If...not. If you can be sure that your expression must be true, you can use it. Returns an assertionerror error if the expression is false the program immediately interrupts 
A = 1  assert"Error value"  print("OK"  )  results:  OK  

bin (x)Converts a decimal number to a binary string.
bool ([x])BOOL judges all values in the list [x], returns True if all elements in the list are true, otherwise returns false, the value type that can return false is: ', none,0, (), [],{}. Similar to the All () function
Print(BOOL (['a', 1,2.4]))#TruePrint(bool (None))#FalsePrint(bool (()))#FalsePrint(bool ([]))#FalsePrint(bool ({}))#FalsePrint(BOOL ("'))#FalsePrint(bool (0))#False

callable (object)Checks if object is callable, can be called to return true, otherwise returns FALSE:1, function, class can be called, 2, instance cannot be called, unless the __call__ method is declared in the class 
def func1 ():       Print ('a')   ' 123 '  Print (Callable (a))  # False   Print (Callable (FUNC1))  # True  

divmod (x, y)Returns the quotient and remainder of X/y with the result of a tuple type
Print (Divmod (5,3))  # (1, 2)  

Ord (c)Returns the ASCII code of character C, X is a character, returns a numeric type
Print (Ord ('a'))  # the  

Chr (x)

Returns the ASCII code x corresponding to the character, X is the integer type, and the return character
Print (CHR)   # ' A '  

Classmethod (function)Returns a class method for a function in the defined class, the main function: 1, annotations, to illustrate that this method is a class method, 2, the class method can be called by the class, or be called by the instance, 3, the class method is similar to the static method in Java, 4, the class method does not need to have the self parameterEnumerate (iterable, start=0)

Returns an enumerable object. The next () method of the object will return a tuple.start to start with the ordinal default starting from 0. Or it can be converted by list or tuple.

Print (List (Enumerate ('aa','bb','cc' ), start=2)))  

equivalent to

def Enumerate (sequence, start=0):      = start      for in  sequence:           yield N, elem           + = 1  

eval (expression)

Evaluates the value of an expressionLambdaLambda is not a python built-in function, but a function expression
Lambda x:x * 2  print(F_lam (Ten      )) #

Map (function, iterable, ...)

Each value in the passed-in list is evaluated once for the function, and python3.x returns the <map object at 0x006afa30>, which needs to be converted with a list or a tuple to get the value.
Li = (1, 2, 3, 4)  = map (lambda x:x + 3, Li)  print(f_map)          #  P ython2.x results [4, 5, 6, 7]  print(list (f_map))    #  python3.x Results [4, 5, 6, 7]  

Reduce ()

The first parameter of reduce () is a function, and the second is a sequence (list or tuple). However, its function must receive two parameters.

Reduce () is the function of the descendant to the first element of the sequence to get the result, the result will continue to work with the next element (cumulative calculation).

 from Import Reduce Print (Reduce (lambda x,y:sum ([x, Y]), Range (1,101)))  # The first argument is a function, the second argument is a sequence

Max (iterable, *[, key, default])

Iterates over the data of a list or element, and returns the maximum value. You can define a function to determine
def func (i):       = Divmod (i,3)      return t[1]    = (1,5,3,2,9,19,24)  print( Max (LI)             #   print(max (li,key=func))    #  5 Maximum value  after modulo data in Li

Max (iterable, *[, key, default])

Similar to Max (), take the minimum valueSorted (iterable[, key][, reverse])Sorts the elements in the parameter by default from small to large. You can specify the sort calculation method key=function, whether in reverse order Reverse=true
deffunc (i): t= Divmod (i,3)      returnT[1] Li= (1,5,3,2,9,19,24)  Print(Sorted (LI))#[1, 2, 3, 5, 9, +-]Print(Sorted (li,reverse=True)) #[9, 5, 3, 2, 1]Print(Sorted (li,key=func)) #[3, 9, 1, 5, 2]Print(Sorted (li,key=func,reverse=True)) #[5, 2, 1, 3, 9, +]

Filter (function, iterable)

As with map execution, a function calculation is performed on an iterator iterable element, and the computed result is filtered to match the result of the expression. The result returned in python3.x is <filter object at A 0x021ca610> object that needs to be converted with a list or tuple to get the value.
F_filter = Filter (lambda x:x >, map (Lambda y:y *, Li))  print(f_filter) c5/>#  python2.x results [max]  print(list (f_filter))   #  python3.x Results [40 ]  

Zip (*iterables)

Merges the values of two tuples or lists into a new list.
A = (1, 2, 3)  = [6,7,8]  #  print (Zip (A, y))  # [(1, 6), (2, 7), (3, 8)]  

Isinstance

Isinstance (OBJ,OBJ2) returns whether the Obj object was created by Obj2, or the Obj object was created by the base class of the Obj2 object.
A = 1  print#  True  class  A (object):      pass    class B (A):       Pass = B ()  print(isinstance (b, b)) # True           Print (Isinstance (b, A))  # True  

IssubclassIssubclass (C,B) determines if class C is a subclass of class B
class A (object):       Pass    class B (A):       Pass    Print (Issubclass (b,a))   Print (Issubclass (A,object))  

Python series-python built-in functions

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.