Native functions
There are functions native to python (or builtin ).
abs (x)
Returns an absolute value
>>> abs ( - 1 ) 1
all (iterable)
Returns True if all the elements of an iterable element are True
>>> list = [ True , True , True , 1 ] >>> all ( list ) True
any (iterable)
Returns True if at least one element of an iterable element is True
>>> list = [ True , False , True ] >>> any ( list ) True
bin (x)
Convert an integer to a binary string.
>>> bin ( 101 ) '0b1100101'
callable (object)
Determines if an object is callable .
>>> callable ( "A" ) False >>> callable ( int ) True
str.capitalize ()
The capitalize method allows to put a character string in Xxxxx format
>>> "oLIviER" . capitalize () 'Olivier'
choice ([])
Returns a value from a random list.
>>> import random >>> random . choice ([ 1 , 2 , 3 , 4 , 5 ]) 3 >>> random . choice ([ 1 , 2 , 3 , 4 , 5 ]) 2
str.count (string)
The count method counts the number of occurrences of the requested search.
>>> "olive tree" . count ( "i" ) 2
dir (object)
Specifies the names of the object structure.
>>> dir ( int ) [ '__abs__' , '__add__' , '__and__' , '__class__' , '__cmp__' , '__coerce__' , '__delattr__' , '__div__' , '__divmod__' , '__doc__' , '__float__ ' , ' __floordiv__ ' , ' __format__ ' , ' __getattribute__ ' , ' __getnewargs__ ' , ' __hash__ ' , ' __hex__ ' , '__index__ ' , ' __init__ ' , ' __int__ ' , '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
str.endswith (str)
The endswith method tests if a string ends with the requested string
>>> a = "olive tree" >>> a . endswith ( "r" ) True >>> a . endswith ( "er" ) True >>> a . endswith ( "é" ) False
eval (expression, globals = None, locals = None)
Execute a string of characters.
>>> v = 101 >>> eval ( 'v + 1' ) 102
str.find (string)
The find method finds the first occurrence of the requested search.
>>> "olive tree" . find ( "i" ) 2
help (element)
This function returns you information on the use of the element which interests you.
> > > Help ( int ) Help on class int in module __builtin__: class int (object) | int (x = 0) -> int or long | int (x, base = 10) -> int or long | | Convert a number or string to an integer, or return 0 if no arguments | are given. If x is floating point, the conversion truncates towards zero. | If x is outside the integer range, the function returns a long instead. | | If x is not a number or if base is given, then x must be a string or | Unicode object representing an integer literal in the given base. Tea | literal can be preceded by '+' or '-' and be surrounded by whitespace. | The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to | interpret the base from the string as an integer literal. | >>> int ('0b100', base = 0) | 4
hex
Converts a number to a hexadecimal value.
>>> hex ( 16 ) '0x10'
str.isalnum ()
Returns True if all characters are alphanumeric and there is at least one character. Otherwise False.
>>> "25" . isalnum () True >>> "25b" . isalnum () True >>> "25be" . isalnum () True >>> "25be @" . isalnum () False >>> "-" . isalnum () False >>> "_" . isalnum () False >>> "" . isalnum () False
str.isalpha ()
Returns True if all characters are letters and there is at least one character. Otherwise False
>>> "x" . isalpha () True >>> "-" . isalpha () False >>> "12" . isalpha () False >>> "jean-claude" . isalpha () False >>> "jean claude" . isalpha () False >>> "elect" . isalpha () True
str.isdigit ()
Returns True if all characters are numeric and there is at least one character. Otherwise False.
>>> "1" . isdigit () True >>> "1.5" . isdigit () False >>> "1.5" . isdigit () False >>> "3b" . isdigit () False >>> "" . isdigit () False
str.islower ()
Returns True if all characters are lowercase.
>>> "olive tree" . islower () True >>> "Olivier" . islower () False
str.isspace ()
Returns True if there are only spaces and at least one character.
>>> "" . isspace () True >>> "jean louis" . isspace () False >>> "" . isspace () True
str.istitle ()
Returns True if the string has a title format.
>>> "Title" . istitle () True >>> "Title" . istitle () False >>> "Title of my site" . istitle () False >>> "Title Of My Site" . istitle () True
str.isupper ()
Returns True if all characters are uppercase and there is at least one character.
>>> "OLIVE TREE" . isupper () True >>> "Olivier" . isupper () False >>> "OlivieR" . isupper () False
str.join (list)
The join method transforms a list into a character string.
>>> ":" . join ([ "olive tree" , "engel" ]) 'olive tree: engel'
len (s)
Returns the number of items of an object.
>>> len ([ 1 , 2 , 3 ]) 3 >>> len ( "olive tree" ) 7
locals ()
Return a dictionary with the values of the current variables.
>>> locals () { 'a' : 12 , '__builtins__' : , '__PACKAGE__' : None , 'i' : 20 , 'v' : 101 , 'list' : [ True , False , True ], '__name__ ' : ' __main__ ' , ' __doc__ ' : None }
str.lower ()
The lower method allows you to put a string of characters in lowercase.
>>> "OLIVE TREE" . lower () 'olive tree'
map (function, [])
Execute a function on each item of an iterable element.
>>> def add_one ( x ): ... return x + 1 ... >>> map ( add_one , [ 1 , 2 , 3 ]) [ 2 , 3 , 4 ]
max () / min ()
Returns the highest value for max () and lowest for min ()
>>> max ([ 1 , 3 , 2 , 6 , 99 , 1 ]) 99 >>> max ( 1 , 4 , 6 , 12 , 1 ) 12
randint ()
Returns a random int.
>>> import random >>> random . randint ( 1 , 11 ) 5
random ()
Returns a random value.
>>> import random >>> random . random () 0.9563522652738929
str.replace (string, string)
The replace method replaces a segment of a character string with another:
>>> "olive tree" . replace ( "i" , "a" ) 'olavaer'
reverse ()
The reverse method reverses the order of a list.
>>> x = [ 1 , 4 , 7 ] >>> x . reverse () >>> x [ 7 , 4 , 1 ]
reversed ([])
Returns an inverted iterator.
>>> list ( reversed ([ 1 , 2 , 3 , 4 ])) [ 4 , 3 , 2 , 1 ]
round (number)
Round off a number.
> > > Round ( 1 ) 1.0 > > > round ( 1.2 ) 1.0 > > > round ( 1.5 ) 2.0 > > > round ( 1.7 ) 2.0 > > > round ( - 1.7 ) - 2.0 > > > round ( - 1.2 ) - 1.0
shuffle ([])
Randomly shuffle a list.
>>> import random >>> x = [ 1 , 2 , 3 , 4 , 5 ] >>> random . shuffle ( x ) >>> x [ 2 , 5 , 4 , 1 , 3 ]
str.startswith (prefix [, start [, end]])
Returns True if the string begins with the given prefix. This prefix can be a tuple. The start and end parameters (optional) test the string at the indicated position. The test is case sensitive.
>>> "olive tree" . startswith ( "ol" ) True >>> "olive tree" . startswith (( "ol" , "eng" )) True >>> "olive tree" . startswith (( "xxx" , "eng" )) False >>> "olive tree" . startswith ( "OL" ) False >>> "olive tree" . startswith ( "ol"
list.sort ()
The sort method is used to sort a list.
>>> l = [ 5 , 1 , 4 , 2 , 10 ] >>> l . sort () >>> l [ 1 , 2 , 4 , 5 , 10 ]
sorted (iterable)
Sort an iterable element.
>>> sorted ([ 3 , 2 , 12 , 1 ]) [ 1 , 2 , 3 , 12 ]
str.split (separator)
The split method transforms a string of characters into a list.
>>> "olive tree: engel" . split ( ":" ) [ 'olivier' , 'engel' ]
str.splitlines ([keepends])
Returns a list of lines in the string. This method uses universal line feed, line feed is not included, unless you set the keepends parameter to True.
>>> "olive tree \ n \ n \ engel \ n \ n developer" . splitlines () [ 'olivier' , '' , ' \\ engel' , '' , 'developer' ] >>> "olivier \ n engel \ n developer" . splitlines () [ 'olivier' , 'engel' , 'developer' ] >>> "olivier \ n \ r engel \ n \ r developer" . 'olivier' , '' , 'engel' , '' , 'developer' ] >>> "olivier \ r \ n engel \ r \ n developer" . splitlines () [ 'olivier' , 'engel' , 'developer' ] >>> "olivier \ r \ n engel \ r \ n \ r \ n developer" . splitlines () [ 'olive tree' , 'engel' , '' , 'developer' \ r \ n \ r \ n developer " . splitlines ( True ) [ 'olivier \ r \ n ' , 'engel \ r \ n ' , ' \ r \ n ' , 'developer' ]
sum (iterable [, start])
Adds the values of an iterable element.
>>> sum ([ 1 , 2 , 3 ]) 6
str.title ()
Transform the string into a title format.
>>> "This is a title" . title () 'This Is A Title'
upper ()
The upper method allows you to capitalize a string of characters.
>>> "olive tree" . upper () 'OLIVE TREE'
zip (* iterables)
Used to group list items together in the form of a tuple.
>>> a = [ "olivier" , "bruce" , "john" ] >>> b = [ "engel" , "wayne" , "Wayne" ] >>> zip ( a , b ) [( 'olivier' , 'engel' ), ( 'bruce' , 'wayne' ), ( 'john' , 'Wayne' )]
No comments:
Post a Comment