Data Types Flashcards
(18 cards)
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(value)
count(value)
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
function that includes yield statement
returns generator object
yield instead of return
yield from
PEP 255
Numeric Types
int, float, complex, bool
inherit from numbers.Number
Generator
type of iterator
generator function returns generator object
generator object controls execution of generation function
generator expression
return smth == StopIteration(smth)
memory efficient
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)