How
Contents
How#
Create a tuple#
To create a tuple which is an ordered collection of objects that cannot be
changed we use the ()
brackets.
Tip
collection = (value_1, value_2, value_3, …, value_n)
For example:
basket = ("Bread", "Biscuits", "Coffee")
basket
('Bread', 'Biscuits', 'Coffee')
How to access particular elements in a tuple#
If we need to we can access elements of this collection using []
brackets. The
first element has index 0
:
tuple[index]
For example:
basket[1]
'Biscuits'
Creating boolean variables#
A boolean variable has one of two values: True
or False
.
To create a boolean variable here are some of the things we can use:
Equality:
value == other_value
Inequality
value != other_value
Strictly less than
value < other_value
Less than or equal
value <= other_value
Inclusion
value in iterable
This a subset of the operators available.
For example:
value = 5
other_value = 10
value == other_value
False
value != other_value
True
value <= other_value
True
value < value
False
value <= value
True
value in (1, 2, 4, 19)
False
It is also possible to combine booleans to create new booleans:
And:
first_boolean and second_boolean
Or:
first_boolean or second_boolean
No:
not boolean
True and True
True
False and True
False
True or False
True
False or False
False
not True
False
not False
True
Creating an iterable with a given number of items#
The range
tool gives a given number of integers.
Tip
range(number_of_integers)
For example:
tuple(range(10))
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Attention
range(N)
gives the integers from 0 until \(N - 1\) (inclusive).
It is also possible to pass two values as inputs so that we have a different lower bound:
tuple(range(4, 10))
(4, 5, 6, 7, 8, 9)
It is also possible to pass a third value as an step size:
tuple(range(4, 10, 3))
(4, 7)
Creating permutations of a given set of elements#
The python itertools
library has a permutations
tool that will generate all
permutations of a given set.
Tip
itertools.permutations(iterable)
import itertools
basket = ("Bread", "Biscuits", "Coffee")
tuple(itertools.permutations(basket))
(('Bread', 'Biscuits', 'Coffee'),
('Bread', 'Coffee', 'Biscuits'),
('Biscuits', 'Bread', 'Coffee'),
('Biscuits', 'Coffee', 'Bread'),
('Coffee', 'Bread', 'Biscuits'),
('Coffee', 'Biscuits', 'Bread'))
It is possible to limit the size to only be permutations of size r
:
tuple(itertools.permutations(basket, r=2))
(('Bread', 'Biscuits'),
('Bread', 'Coffee'),
('Biscuits', 'Bread'),
('Biscuits', 'Coffee'),
('Coffee', 'Bread'),
('Coffee', 'Biscuits'))
Creating combinations of a given set of elements#
The python itertools
library has a combinations
tool that will generate all combinations of size r
of a given set:
Tip
itertools.combinations(iterable, r)
For example:
basket = ("Bread", "Biscuits", "Coffee")
tuple(itertools.combinations(basket, r=2))
(('Bread', 'Biscuits'), ('Bread', 'Coffee'), ('Biscuits', 'Coffee'))
A combination does not care about order so by default the combinations generated are sorted.
Adding items in a tuple#
We can compute the sum of items in a list using the sum
tool:
sum((1, 2, 3))
6
We can also directly use the sum
without specifically creating the list. This
corresponds to the following mathematical notation:
and is done using the following:
sum(f(object) for object in old_list)
Here is an example of calculating the following sum:
sum(n ** 2 for n in range(11))
385
Finally we can compute conditionally sums by only summing over elements that meet a given condition using the following:
sum(f(object) for object in old_list if condition)
Here is an example of calculating the following sum:
sum(n ** 2 for n in range(11) if n % 2 == 1)
165
Directly computing \(n!\)#
The math
library has a factorial
tool.
Tip
math.factorial(N)
import math
math.factorial(5)
120
Directly computing \({n \choose i}\)#
The scipy.special
library has a comb
tool.
Tip
scipy.special.comb(n, i)
For example:
import scipy.special
scipy.special.comb(3, 2)
3.0
Directly computing \(^n P_i\)#
The scipy.special
library has a perm
tool.
Tip
scipy.special.perm(n, i)
scipy.special.perm(3, 2)
6.0