Data Types Flashcards
Literal
notation for representing a fixed value of a specific data type
Iterable
object that can be iterated over in a for loop
object that you can pass to the iter function to get an iterator from it
Iterator Protocol
__iter__()
__next__()
Sequence
Ordered collection of elements.
Any sequence is iterable
Mutable and immutable sequences
Mutable: list, bytearray
Immutable
tuple, range, string, bytes
support hash() therefore can be used as dict keys and stored in set
Sequence operations
- indexing
- slicing
- membership (in, not in)
- concatenation (+) (except range)
- repetition (*) (except range)
Sequence methods
Common
index(index)
count()
Mutable
append(value)
extend(value)
insert(value, index)
pop(index)
remove(value)
reverse()
clear()
copy()
List
sort()
Iterator
object that implements the iterator protocol
iterators are iterables
single-use
next(iterator[, default])
calling next on exhausted iterator raises StopIteration
List
list()
Tuple
()
tuple()
Bytes
bytes()
Generator function
returns generator object
yield instead of return
PEP 255
Numeric Types
int
float
complex
Generator
Generator function returns generator object - type of iterator
generator expression
yield from
return smth == StopIteration(smth)
PEP 380
Type Hints
def func(arg1: type1, arg2: type2 = default, ...) -> return_type: ...
o.__annotations__
inspect.get_annotations(obj, *, globals=None, locals=None, eval_str=False)
from typing import Any
mypy, pyright, typeguard
Float
scientific notation NeM
any operation involving a float returns a float
Set
class set([iterable])
class frozenset([iterable])
unordered collection of distinct hashable objects
Sequence functions
len(s)
min(s)
max(s)