Basics Flashcards

Learn basic knowledge of Python

1
Q

Return x if it is positive or 0

without if

A

max(0, x)

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

Variable name for assigning useless value

A

_

underscore

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

Function arguments order

A

fun(arg1, arg2, *args, kw1, kw2, **kwargs)

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

Add padding do string

A

.ljust(width, fillchar=’ ‘)

.rjust(width, fillchar=’ ‘)

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

Function only keyword argument

cannot be entered as positional

A
def fun(item, *, arg):
...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can you use return in generator definition?

A

Yes, it ends generator execution (no more yields)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.

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

What is generator?

A

Function with yield keyword that works like iterator

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