Functions
abs
abs(x) # Return the absolute value of a number.
all
all(iterable) # Return True is all elements in iterable are True
any
any(iterable) # Return True if any of the iterable are True
bin
> bin(3) # Convert an integer num to a binary string
'0b11'
> bin(-10)
'-0b1010'
bool
> bool(1) # Return boolean value
True
> bool(0)
False
chr
> chr(97) # Return the string representation of unicode integer
'a' # inverse is ord()
enumerate
enumerate(iterable, start=0) # Return an enumerate object
> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
eval
float
float(val) # Return a floating point number from num or string
>>> float('+1.23')
1.23
>>> float(' -12345\n')
-12345.0
>>> float('1e-003')
0.001
>>> float('+1E6')
1000000.0
>>> float('-inf')
-inf
hash
hash(object) # Return the has value of the object
hex
hex(num) # Convert an integer number to a lowercase hexadecimal string
>hex(255)
'0xff'
> hex(-42)
'-0x2a'
int
int(x, base=10) # Return an integer object constructed from a number or string
len
len(s) # Return the length of an object
map
map(function, iterable, ...)
# Return an iterator that applies function to every item of iterable, yielding the results.
max
max(arg1, arg2, *args[, key]) # Return the largest item in an iterable or
# the largest of two or more arguments.
> max(3, 4)
4
min
min(arg1, arg2, *args[, key]) # Return the smallest item in an iterable or
# the smallest of two or more arguments.
> min(3, 4)
3
oct
oct(x) # Convert an integer number to an octal string prefixed with “0o”.
> oct(8)
'0o10'
> oct(-56)
'-0o70'
ord
ord(c) # Given a string representing one Unicode character,
# return an integer representing the Unicode code point of that character.
# opposite of chr()
> ord('a')
97
pow
pow(base, exp[, mod])
# Return base to the power exp; if mod is present,
# return base to the power exp, modulo mod
# (computed more efficiently than pow(base, exp) % mod).
> pow(10, 2)
100
> pow(38, -1, mod=97)
23
> 23 * 38 % 97 == 1
True
range
range(stop)
range(start, stop[, step])
# start
# The value of the start parameter (or 0 if the parameter was not supplied)
# stop
# The value of the stop parameter
# step
#The value of the step parameter (or 1 if the parameter was not supplied)
> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
> list(range(0, 10, 3))
[0, 3, 6, 9]
> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
> list(range(0))
[]
> list(range(1, 0))
[]
reversed
reversed(seq) # Return a reverse iterator.
round
round(number[, ndigits])
> round(1.5)
2
> round(1.55,1)
1.6
slice
# Return a slice object representing the set of indices specified
# by range(start, stop, step).
slice(stop)
slice(start, stop[, step])
sorted
sorted(iterable, *, key=None, reverse=False)¶
>sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]
> a = [5, 2, 3, 1, 4]
> a.sort()
> a
[1, 2, 3, 4, 5]
> sorted("This is a test string from Andrew".split(), key=str.lower)
['a', 'Andrew', 'from', 'is', 'string', 'test', 'This']
str
# Return a string version of object. If object is not provided,
# returns the empty string.
str(object='')
str(object=b'', encoding='utf-8', errors='strict'
sum
sum(iterable, /, start=0)
> sum([2,3,4])
9
tuple
# Constructs a tuple
> tuple('abc')
('a', 'b', 'c')
zip
# Make an iterator that aggregates elements from each of the iterables.
zip(*iterables)