Title: Theory / Unimportant / CS / Programming / Python / Lib / built-in types / Range
Tags: computer_to_instruction automatedNewLineAfterCommaSpace
On every note of this deck, unless a note says otherwise:
Page: https://docs.python.org/3.7/library/stdtypes.html#sequence-types-list-tuple-range
Language: Python

Name: Basic sequence type
Definition: List
Page: https://docs.python.org/3.7/library/stdtypes.html#sequence-types-list-tuple-range
Definition2: Tuple
Definition3: Range

Instruction: \(s+t\)
Effect: Concatenation of \(s\) and \(t\)
Variables: \(s,t\) sequences
Extra: Don't use it to iteratively extends sequence. It would have quadratic cost
Assuming: \(s,t\) are the same kind of sequences

Instruction: s*n
n*s
Returns: \(n\) copies of \(s\) concatenated
Variables: \(s\) sequence
\(n\) int

Instruction: min(s)
Returns: The smallest item of \(s\)
Variables: s a sequence

Instruction: max(s)
Returns: The largest item of \(s\)
Variables: s a sequence

Instruction: s.index(x)
Returns: index of the first occurrence of x in s
Variables: s a sequence
x an object
Exception: ValueError if x not in s

Instruction: s.index(x,i)
Returns: index of the first occurrence of x in s at position at least i
Variables: s a sequence
x an object

Instruction: s.index(x,i,j)
Returns: index of the first occurrence of x in s at position at least i less than j
Variables: s a sequence
x an object

Instruction: s[i:j] = t
Effect: slice of s from i to j is replaced by the contents of the iterable t
Variables: s a mutable sequence
t iterable

Instruction: del s[i:j]
Effect: same as s[i:j] = []
Variables: s a mutable sequence
t iterable

Instruction: del s[i:j:k]
Effect: remove the elements of s[i:j:k] from the list
Variables: s a mutable sequence
t iterable

Instruction: s[i:j:k] = t
Effect: the elements of s[i:j:k] are replaced by those of t
Variables: s a mutable sequence
t iterable
Assuming: len(s[i:j:k])== len(t)

Instruction: s.append(x)
Effect: appends x to the end of the sequence
Variables: s a mutable sequence
t iterable
x an object

Instruction: s.extend(t)
Effect: extends s with the contents of t
Variables: s a mutable sequence
t iterable
x an object
Abbreviation: s+=t

Effect: updates s with its contents repeated n times
Variables: s a mutable sequence
t iterable
x an object
Abbreviation: s*=n
Assuming: n either an integer, or implementing __index__()

Instruction: s.insert(i,x)
Effect: inserts x into s at the index given by i
Variables: s a mutable sequence
t iterable
x an object

Instruction: s.pop(i=-1)
Effect: retrieves the item at i and also removes it from s
Variables: s a mutable sequence
t iterable
x an object
i an int

Instruction: s.remove(x)
Effect: remove the first item from s where s[i] is equal to x
Variables: s a mutable sequence
t iterable
x an object
i an int

Instruction: s.reverse()
Effect: reverses the items of s in place
Variables: s a mutable sequence
t iterable
x an object
i an int

Instruction: [a_0,...,a_n-1]
Returns: A list of size n whose i element is a_i

Instruction: [t for x in foo]
Returns: A list of the same size than foo, whose i-th element is t in which x is evaluated as the i-th element of foo

Instruction: list(iterable=[])
Returns: A list of the same size than foo, whose i-th element is the i-th element of iterable

Instruction: l.sort(
  *,
  key=None,
  reverse=False
)
Returns: Sorts stably the list in place, using only < comparisons between items, on which the key function is applied.
Variables: l a list
Extra: key function is applied a single time
Exception: Any exception raised during a comparison

Instruction: range(i)
Returns: An immutable sequence of numbers from 0 to i-1
Assuming: i a int, or with __index__ method

Instruction: range(i,j)
Returns: An immutable sequence of numbers from i to j-1
Assuming: i,j ints, or with __index__ method

Instruction: range(i,j,k)
Returns: The sequence i, i+k,...,i+n*k with n maximal such that i and i+n*k are both smaller than k (or greater if n<0)
Assuming: i,j ints, or with __index__ method

Instruction: r.start()
Returns: start parameter of the range
Variables: r a range object

Instruction: r.stop()
Returns: stop parameter of the range
Variables: r a range object

Instruction: r.step()
Returns: step parameter of the range
Variables: r a range object

Question: Why use range instead of list
Answer: It takes a constant small size, since it's compute the i th value with arithmetic operation instead of saving it
Page: https://docs.python.org/3.7/library/stdtypes.html#sequence-types-list-tuple-range
Variables: r a range object