#1 – Core Python Concepts Flashcards

(8 cards)

1
Q

What does enumerate() do in a for loop?

A

It allows you to loop over an iterable and get both the index and the item.

for i, val in enumerate(['a', 'b', 'c']):
    print(i, val)

0 a 1 b 2 c

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

What does zip() do in Python?

A

It pairs elements from two or more iterables into tuples.

for a, b in zip([1, 2], ['x', 'y']):
    print(a, b)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is list comprehension in Python?

A

A concise way to create a list using a single line of code.
squares = [x**2 for x in range(10) if x % 2 == 0]

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

What is dict comprehension in Python?

A

A compact way to create a dictionary from an iterable.
mapping = {c: ord(c) for c in 'abc'}

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

What are *args used for in a function definition?

A

*args lets you pass a variable number of positional arguments as a tuple.

def func(*args):
    print(args)
func( 2, 3)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are **kwargs used for in a function definition?

A

**kwargs lets you pass a variable number of keyword arguments as a dictionary.
Example:

def func(**kwargs): 
    print(kwargs)
func(a=1, b=2)
{'a': 1, 'b': 2}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What will the following code output?

def func(a, *args, **kwargs):
    print(a)
    print(args)
    print(kwargs)
func(1, 2, 3, x=4, y=5)
A

1
(2, 3)
{‘x’: 4, ‘y’: 5}

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

What is a lambda function in Python?

A

wynik: [(2, ‘a’), (3, ‘b’), (1, ‘c’)]

An anonymous (unnamed) function defined with the lambda keyword.

data = [(1, 'c'), (2, 'a'), (3, 'b')]
sorted(data, key=lambda x: x[1])

wynik: [(2, ‘a’), (3, ‘b’), (1, ‘c’)]

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