Basics Flashcards

1
Q

Range

A

Immutable sequence
range(stop)
range(start, stop[, step])
start=0
step=1
<class ‘range’>
if step=0: ValueError
Only integers
no concatenation and repetition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Slice

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Extended slice

A

slice with a step

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Expression

A

produce a value
list comprehension
conditional (ternary) expression
yield x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Statement

A

do something
return, pass, assert, raise

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

List comprehension

A

[expr for item in iterable if cond]
multiple ifs: if cond1 if cond2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Iterable Unpacking

A

a, b, c = 1, 2, 3
_ — throw-away
tuple = tuple

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Dictionary comprehension

A

{key: value for (key, value) in iterable}
<key if else>: <value if else>
PEP 274

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Unpacking inside tuple, list, set, dict

A
*range(n),
[*range(n)]
{*range(n)}
{**{'a': 1}}

PEP 448

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Throwaway variable

A

_

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Pass by Assignment

A

pass by reference
pass by object reference
pass by sharing

no primitive types
nothing is copied

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Docstrings

A

”"”triple double quotes”””
.__doc__

string literal
first statement in a module, function, class, method
PEP 257

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

F-Strings

A

formatted string literals
~~~
{f_expr[=][!<conversion>][:<format_spec>]}
~~~</format_spec></conversion>

conversion flags

self-documenting expression
» f’{expr=}’
expr=value

format specifiers
PEP 498

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Modulo Operator

A
<format_string> % <values>

conversion specifier:

%[<flags>][<width>][.<precision>]<type>

tuple, dictionary

logging
dictionary interpolation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

str.format()

A
<template>.format(<args>, <kwargs>)

replacement fields
indices, keyword arguments

{[<name>][!<conversion>][:<format_spec>]}

localization
dictionary interpolation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Special Method

A

method that is called implicitly by Python to execute a certain operation on a type
__method__

17
Q

Namespace

A

mapping from names to objects
no relation between names in different namespaces

global, local – dictionary
globals()
locals()

18
Q

Scope

A

textual region of a Python program where a namespace is directly accessible

values have no scope

19
Q

Del

A

unassignment statement
del ref_1[, ref_2…]

Reference:
- identifier
- index
- slice
- key
- attribute or method

from left to right
can’t delete built-in names

20
Q

Nested Unpacking

A
a, (b, c) = 1, [2, 3]
21
Q

Extended Unpacking

A

catch-all unpacking

a, b, *c = 1, 2, 3, 4

starred expression
only one
returns list
can be empty
must be in a list or tuple
PEP 3132

22
Q

Slice Assignment

A

s[i:j:k] = t

replace

delete
s[i:j] = []

insert
s[i:i] = t
s[len(s):] = t append
s[:0] = t prepend

if step not 1, must be same length

23
Q

Else Loop

A

executes if loop completes without break

for ... in ...:
    ...
else:
    ...
while ...:
    ...
else:
    ...
24
Q

Format Specification Mini-Language

A

standard format specifier:

[[<fill>]<align>][<sign>][z][#][0][<width>][<group>][.<prec>][<type>]

fill
align
sign
width
grouping_option
precision
type

25
Q

Conversion Flags

A

!s str()
!r repr()
!a ascii()