Python - Key Keywords and Builtins
Builtin Functions
max(A)
and min(A)
Returns the largest (max) or smallest (min) element of A
.
- Independent elements may be with these builtins instead of an array, e.g.,
max(3,4,5)
out = min([4,7,10])
assert(out == 10)
out = min([4,7,10])
assert(out == 10)
any(A)
and all(A)
any(A)
returns true if any of the elements are non-zero, otherwise false all(A)
returns true if all of the elements are non-zero, otherwise false
- Independent elements may be with these builtins instead of an array, e.g.,
any(-12,0,0)
out_any = any([0,0,2])
assert(out_any == True)
out_all = all([1,2,3])
assert(out_all == True)
out_any = any([0,0,2])
assert(out_any == True)
out_all = all([1,2,3])
assert(out_all == True)
ord(a)
Returns the Unicode integer associated with character a
out = ord('a')
assert(out == 97)
out = ord('a')
assert(out == 97)
set(A)
and frozenset(A)
Returns a set (of unique values) from the array A
out = set([1,1,2])
assert(out == {1,2})
out = set([1,1,2])
assert(out == {1,2})
filter(f, A)
Returns only the elements from A
that aren't filtered the boolean function f
func = lambda x: x > 4
out = list(filter(func, [1,3,5,7,9]))
assert(out == [5,7,9])
func = lambda x: x > 4
out = list(filter(func, [1,3,5,7,9]))
assert(out == [5,7,9])
map(f, A)
- Applies the function
f
to each element ofA
, returning an object of the same length - Additional arrays maybe used, in which case
f
must have the same number of arguments
A = [1,2,3,4,5]
out = list(map(lambda a: a**2, A))
assert(out == [1,4,9,16,25])
A = [1,2,3,4,5]
out = list(map(lambda a: a**2, A))
assert(out == [1,4,9,16,25])
hash(s)
- Returns the hash code of a given immutable object.
iter(obj, end)
- Returns an iterator to the object
obj
, an optional custom sentinal (stop value) can be passed viaend
- Use
next(iterator)
to call the iterator's internal__next__()
method, returning the next item in the iterator
A = [1,2,3,4,5]
out = iter(A)
assert(next(A) == 1)
assert(next(A) == 2)
A = [1,2,3,4,5]
out = iter(A)
assert(next(A) == 1)
assert(next(A) == 2)
Tip
On variables of type
dict
used[key] = d.get(key, default)
to create default values for keys that may not exist instead of using an if-else statement. Alternatively used.setdefault(key, default)
pythond = {} d['a'] = d.get('a', 1)
d = {} d['a'] = d.get('a', 1)
Another good approach is to simply use the
collections.defaultdict(type)
type where unregistered keys will take on the default oftype
when accessed, e.g.,int=0
,str=''
,list=[]
.
From functools
reduce(f(acc,k), A, a0)
- Accumulates a value
acc
by applying the functionf
to each elementk
of arrayA
a0
is an initial value such thatacc = a0
before the first iterationf
must be a function of two variables, e.g.,lambda acc, k: acc*k
A = [1,2,3,4,5]
a0 = 0
out = reduce(lambda acc, k: acc+k, A, a0)
assert(out == 15)
A = [1,2,3,4,5]
a0 = 0
out = reduce(lambda acc, k: acc+k, A, a0)
assert(out == 15)
From itertools
chain(*A)
Flattens the array A
by first unpacking it with *
A = [[1,2,3], [4,5,6]]
out = chain(*A)
assert(out == [1,2,3,4,5,6])
A = [[1,2,3], [4,5,6]]
out = chain(*A)
assert(out == [1,2,3,4,5,6])
Operators
>>
and <<
Bit Shift Operator
x = 9 # in binary 00001001
xr = x >> 1 # in binary 00000100 = 4
xl = x << 1 # in binary 00010010 = 18
x = 9 # in binary 00001001
xr = x >> 1 # in binary 00000100 = 4
xl = x << 1 # in binary 00010010 = 18