Python intro and advanced notes Python built in functions summary

  • 2020-04-02 13:55:30
  • OfStack

Built-in function
Commonly used functions

1. Math is relevant
The & # 8226; Abs (x)
Abs () returns the absolute value of a number. If a complex number is given, the return value is the magnitude of the complex number.


>>>print abs(-100)
100
>>>print abs(1+2j)
2.2360679775

The & # 8226; Divmod (x, y)
The divmod(x,y) function does the division and returns the quotient and remainder.


>>> divmod(10,3)
(3, 1)
>>> divmod(9,3) (3, 0)

The & # 8226; Pow (x, y) [z]
The pow() function returns a power with base x and exponent y. If a z value is given, the function computes the value of x to the y being modularized by z.


>>> print pow(2,4)
16
>>> print pow(2,4,2)
0
>>> print pow(2.4,3)
13.824

The & # 8226; Round (x [n])
The round() function returns the rounded value of the floating point number x, which, if given an n value, represents the number rounded to the decimal point.


>>> round(3.333)
3.0
>>> round(3)
3.0
>>> round(5.9)
6.0

The & # 8226; [min (x, y, z... )
The min() function returns the minimum value of a given parameter, which can be a sequence.


>>> min(1,2,3,4)
1
>>> min((1,2,3),(2,3,4))
(1, 2, 3)

The & # 8226; [Max (x, y, z... )
The Max () function returns the maximum value of a given parameter, which can be a sequence.


>>> max(1,2,3,4)
4
>>> max((1,2,3),(2,3,4))
(2, 3, 4)

2. Sequence correlation

The & # 8226; Len (object) > The integer
The len() function returns the length of the string and sequence.


>>> len("aa")
2
>>> len([1,2])
2

The & # 8226; Range (stop [step], [the lower,])
The range() function generates a list of consecutive ordered integers by argument.


>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10,2)
[1, 3, 5, 7, 9]

The & # 8226; Xrange (stop [step], [the lower,])
The xrange() function is similar to range(), but instead of creating a list, xrnage() returns an xrange object and its behavior

It is similar to a list, but only computes the list value when needed, which saves memory when the list is large.


>>> a=xrange(10)
>>> print a[0]
0
>>> print a[1]
1
>>> print a[2]
2

3. Objects and types
The & # 8226; Callable (object)
The callable() function is used to test whether an object is callable, and returns 1(true) if it can. Otherwise return 0(false). Callable objects include functions, methods, code objects, classes, and instances of classes where the method is already defined.


>>> a="123"
>>> print callable(a)
False
>>> print callable(chr)
True

The & # 8226; CMP (x, y)
The CMP () function compares x and y objects and returns an integer if x < Y, returns negative 1; If x > Y, returns 1, and if x is equal to y, returns 0.


>>>a=1
>>>b=2
>>>c=2
>>> print cmp(a,b)
-1
>>> print cmp(b,a)
1
>>> print cmp(b,c)
0

The & # 8226; Isinstance (object, class - or - type - or - a tuple) - > bool
Test object type


>>> a='isinstance test'
>>> b=1234
>>> isinstance(a,str)
True
>>>isinstance(a,int)
False
>>> isinstance(b,str)
False
>>> isinstance(b,int) True

The & # 8226; Type (obj)
The type() function returns the data type of the object.


>>> type(a)
<type 'list'>
>>> type(copy)
<type 'module'>
>>> type(1)
<type 'int'>

Built-in type conversion function

1. Characters and strings
The & # 8226; CRH (I)
The CHR () function returns the string corresponding to the ASCII code.


>>> print chr(65)
A
>>> print chr(66)
B
>>> print chr(65)+chr(66)
AB

The & # 8226; Word (x)
The ord() function returns the ASCII or Unicode value of a string parameter.


>>> ord("a")
97
>>> ord(u"a")
97

The & # 8226; STR (obj)
The STR () function converts the object to a printable string.


>>> str("4")
'4'
>>> str(4)
'4'
>>> str(3+2j)
'(3+2j)'

2. Base conversion
The & # 8226; Int x [, base])
The int() function converts a number and a string to an integer, with base as the optional cardinality.


>>> int(3.3)
3
>>> int(3L)
3
>>> int("13")
13
>>> int("14",15)
19

The & # 8226; Long (x [, base])
The long() function converts Numbers and strings into integers, with base as the optional base.


>>> long("123")
123L
>>> long(11)
11L

The & # 8226; Float (x)
The float() function converts a number or string to a float.


>>> float("12")
12.0
>>> float(12L)
12.0
>>> float(12.2)
12.199999999999999

The & # 8226; Hex (x)
The hex() function converts integers to hexadecimal Numbers.


>>> hex(16)
'0x10'
>>> hex(123)
'0x7b'

The & # 8226; Oct (x)
The oct() function converts given integers into octal Numbers.


>>> oct(8)
'010'
>>> oct(123)
'0173'

The & # 8226; The complex (real [, imaginary])
The complex() function converts a string or number to a complex number.


>>> complex("2+1j")
(2+1j)
>>> complex("2")
(2+0j)
>>> complex(2,1)
(2+1j)
>>> complex(2L,1)
(2+1j)

3. Data structure
The & # 8226; The tuple (x)
The tuple() function converts a sequence object to a tuple.


>>> tuple("hello world")
('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd')
>>> tuple([1,2,3,4])
(1, 2, 3, 4)

The & # 8226; The list (x)
The list() function converts a sequence object into a list. Such as:


>>> list("hello world")
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
>>> list((1,2,3,4))
[1, 2, 3, 4]

Sequence processing function
Len (), Max (), and min() in common functions can also be used for sequences.

The & # 8226; The filter (function, a list)
When filter() is called, it applies a function to each item in the sequence and returns all items that the function returns true, thereby filtering out all items that return false values.


>>> def nobad(s):
    ... return s.find("bad") == -1
    ...
>>> s = ["bad","good","bade","we"]
>>> filter(nobad,s)
['good', 'we']

The & # 8226; The map (the function, the list [list])
The map() function applies a function to all the items in the sequence and returns a list.


>>> import string
>>> s=["python","zope","linux"]
>>> map(string.capitalize,s)
['Python', 'Zope', 'Linux']

Map () can also be applied to multiple lists simultaneously. Such as:


>>> import operator
>>> s=[1,2,3]; t=[3,2,1]
>>> map(operator.mul,s,t) # s[i]*t[j]
[3, 4, 3]

If you pass a value of None instead of a function, map() merges the corresponding elements in each sequence and returns the tuple. Such as:


>>> a=[1,2];b=[3,4];c=[5,6]
>>> map(None,a,b,c)
[(1, 3, 5), (2, 4, 6)]

The & # 8226; Reduce (function, seq [, init])
The reduce() function takes the first two items in the sequence, passes them to the supplied function, gets the result, takes the next item in the sequence, passes the result to the function, and so on, until all the items are processed.

[code]
> > > The import operator
> > > reduce(operator.mul,[2,3,4,5]) # ((2*3)*4)*5
120
> > > reduce(operator.mul,[2,3,4,5],1) # (((1*2)*3)*4)*5
120
> > > reduce(operator.mul,[2,3,4,5],2) # (((2*2)*3)*4)*5
240
[code]

wklken
Email: wklken@yeah.net


Related articles: