Basics Flashcards
Range
Immutable sequence
range(stop)
range(start, stop[, step])
start=0
step=1
<class ‘range’>
if step=0: ValueError
Only integers
no concatenation and repetition
Slice
start=0
stop=len(x)
step=1
if step<0:
start=-1
stop=-len(x)-1
if step=0: ValueError
list[slice(start, stop, step)]
.indices(length)
Extended slice
slice with a step
Expression
produce a value
list comprehension
conditional (ternary) expression
yield x
Statement
do something
return, pass, assert, raise
List comprehension
[expr for item in iterable if cond]
multiple ifs: if cond1 if cond2
Iterable Unpacking
a, b, c = 1, 2, 3_
— throw-away
tuple = tuple
Dictionary comprehension
{key: value for (key, value) in iterable}<key if else>: <value if else>
PEP 274
Unpacking inside tuple, list, set, dict
*range(n), [*range(n)] {*range(n)} {**{'a': 1}}
PEP 448
Throwaway variable
_
Pass by Assignment
pass by reference
pass by object reference
pass by sharing
no primitive types
nothing is copied
Docstrings
”"”triple double quotes”””
.__doc__
string literal
first statement in a module, function, class, method
PEP 257
F-Strings
formatted string literals
~~~
{f_expr[=][!<conversion>][:<format_spec>]}
~~~</format_spec></conversion>
conversion flags
self-documenting expression
» f’{expr=}’
expr=value
format specifiers
PEP 498
Modulo Operator
<format_string> % <values>
conversion specifier:
%[<flags>][<width>][.<precision>]<type>
tuple, dictionary
logging
dictionary interpolation
str.format()
<template>.format(<args>, <kwargs>)
replacement fields
indices, keyword arguments
{[<name>][!<conversion>][:<format_spec>]}
localization
dictionary interpolation