Basics Flashcards
Learn basic knowledge of Python
1
Q
Return x if it is positive or 0
without if
A
max(0, x)
2
Q
Variable name for assigning useless value
A
_
underscore
3
Q
Function arguments order
A
fun(arg1, arg2, *args, kw1, kw2, **kwargs)
4
Q
Add padding do string
A
.ljust(width, fillchar=’ ‘)
.rjust(width, fillchar=’ ‘)
5
Q
Function only keyword argument
cannot be entered as positional
A
def fun(item, *, arg): ...
6
Q
Can you use return in generator definition?
A
Yes, it ends generator execution (no more yields)
7
Q
iterator
vs
iterable
A
Iterator can be passed to next() function to return another item.
Iterable returns when passes to iter() function. Iterable can be looped over with for loop.
8
Q
What is generator?
A
Function with yield keyword that works like iterator