#1 – Core Python Concepts Flashcards
(8 cards)
What does enumerate() do in a for loop?
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
What does zip() do in Python?
It pairs elements from two or more iterables into tuples.
for a, b in zip([1, 2], ['x', 'y']): print(a, b)
What is list comprehension in Python?
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]
What is dict comprehension in Python?
A compact way to create a dictionary from an iterable.mapping = {c: ord(c) for c in 'abc'}
What are *args used for in a function definition?
*args lets you pass a variable number of positional arguments as a tuple.
def func(*args): print(args) func( 2, 3)
What are **kwargs used for in a function definition?
**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}
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)
1
(2, 3)
{‘x’: 4, ‘y’: 5}
What is a lambda function in Python?
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’)]