Common python built-in functions 1 and python built-in functions
1. abs calculates the absolute value.
>>> abs( -1 )1>>> abs( 1 )1>>>
2, max, min: calculate the maximum and minimum values of a sequence
>>> l = [ 1, 10, -6, 2, 100 ]>>> max( l )100>>> min( l )-6
3. len calculates the length of the list.
>>> s = 'ghostwu'>>> len( s )7>>> l = [ 10, 20, 30 ]>>> len( l )3>>> t = ( 10, 20 )>>> len( t )2>>> dic = { 'a' : 10, 'b' : 20, 'c' : -1, 'd' : 100 }>>> len( dic )4
4. divmod quotient and remainder
>>> divmod( 1, 2 )(0, 1)>>> divmod( 2, 1 )(2, 0)>>>
5, pow, two values, power operation, three values, first power operation, then divided by the third value, evaluate the remainder
>>> pow( 3, 4 )81>>> pow( 2, 2, 6 )4>>>
6. callable test whether the function can be called
>>> callable( min )True>>> callable( f )Traceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name 'f' is not defined>>> callable( abs )True>>> def f():... pass... >>> callable( f )True
7. Type Determination
>>> l = [ 10, 20 ]>>> type( l )<type 'list'>>>> type( [] )<type 'list'>>>> if( type( [] ) == type( l ) ):... print 'yes'... yes>>> t = ( 10, 20 )>>> if( type( [] ) == type ( t ) ):... print 'yes'... else:... print 'no'... no>>>
You can also use isinstance to determine
>>> l = [ 10, 20 ]>>> isinstance( l, list )True>>> type( () )<type 'tuple'>>>> isinstance( ( 10, 20 ), tuple )True>>> isinstance( l, tuple )False>>>
8. cmp compares the string size
>>> cmp( 'a', 'b' )-1>>> cmp( 'b', 'ac' )1>>> cmp( 'a', 'ab' )-1>>> cmp( 'a', 'a' )0>>>
9. range and type conversion
>>> range( 1, 10 )[1, 2, 3, 4, 5, 6, 7, 8, 9]>>> range( -1, -10, -1 )[-1, -2, -3, -4, -5, -6, -7, -8, -9]>>> range( 1, 10, 5 )[1, 6]>>>
>>> s = 'ghostwu'>>> type( s )<type 'str'>>>> int( s )Traceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: invalid literal for int() with base 10: 'ghostwu'>>> s = '20'>>> type( s )<type 'str'>>>> int( s )20>>> type( s )<type 'str'>>>> s + 'abc''20abc'>>> int( s ) + 'abc'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unsupported operand type(s) for +: 'int' and 'str'>>> int( s ) + 1030>>>
A string of the numeric type can be converted to an integer.
Of course, there are other types of conversions.