Advanced Python Flashcards

1
Q

What is a generator?

A

Specialized code that is able to return a series of values and to control the iteration process.

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

what is a iterator in python?

A

methods: __iter__() and __next__().

Key Concepts
Iterable: An object that can return an iterator. Examples include lists, tuples, dictionaries, and sets. These objects have an __iter__() method that returns an iterator.

Iterator: An object that represents a stream of data. It has a __next__() method that returns the next item from the collection. When there are no more items to return, it raises a StopIteration exception.

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

Define a custom iterator

A

class MyIterator:
def __init__(self, data):
self.data = data
self.index = 0

def \_\_iter\_\_(self):
    return self

def \_\_next\_\_(self):
    if self.index < len(self.data):
        result = self.data[self.index]
        self.index += 1
        return result
    else:
        raise StopIteration

Create an instance of MyIterator
my_iter = MyIterator([1, 2, 3])

Use the iterator
for item in my_iter:
print(item) # Output: 1 2 3

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

What is comprehension?

A

Comprehensions in Python provide a concise way to create sequences such as lists, dictionaries, and sets. They allow you to generate new sequences by applying an expression to each item in an existing sequence or iterable, optionally filtering elements with a conditional statement. Comprehensions are known for their readability and efficiency.

Types of Comprehensions:
List Comprehension
Dictionary Comprehension
Set Comprehension
Generator Comprehension

[expression for item in iterable if condition]

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

What are lambda Functions?

A

Lambda functions in Python are small, anonymous functions defined with the lambda keyword. They are often used for short, simple functions that are not reused elsewhere, providing a concise way to create function objects.

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

What is the syntax for a lambda function?

A

lambda arguments: expression

add = lambda x, y: x + y
print(add(3, 5)) # Output: 8

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

What are the 3 characteristics of lambda functions?

A

Anonymous: Lambda functions do not have a name, unlike regular functions defined using def.

Single Expression: They are limited to a single expression. The result of the expression is implicitly returned.

Inline Usage: Commonly used in places where a small function is required for a short period and is typically used inline.

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

What is a map function?

A
  1. Using Lambda with map()
    The map() function applies a function to all items in an input list (or any iterable) and returns a map object (an iterator).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Give me an example of a map function

A

numbers = [1, 2, 3, 4]
squares = map(lambda x: x**2, numbers)
print(list(squares)) # Output: [1, 4, 9, 16]

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

What is a lambda filter function?

A

Using Lambda with filter()
The filter() function filters items out of an iterable based on a function that returns True or False.

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

Give an example of a lambda filter function

A

numbers = [1, 2, 3, 4, 5, 6]
evens = filter(lambda x: x % 2 == 0, numbers)
print(list(evens)) # Output: [2, 4, 6]

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

Explain using Lambda with sorted() or sort() with an example:

A

Lambda functions can be used to specify a key for sorting.

points = [(1, 2), (4, 1), (5, 3), (2, 4)]
sorted_points = sorted(points, key=lambda x: x[1])
print(sorted_points) # Output: [(4, 1), (1, 2), (5, 3), (2, 4)]

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

explain using Lambda with reduce()

A

The reduce() function from the functools module applies a function cumulatively to the items of an iterable.

from functools import reduce
numbers = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, numbers)
print(product) # Output: 24

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

What are closures in python?

A

A closure in Python refers to a function that retains access to the variables from its enclosing scope, even after the outer function has finished executing. Closures are a powerful feature used to create functions with extended lifetimes for their enclosing scopes’ variables.

Nested Functions: A closure involves at least two functions: an outer function and an inner function defined within it.

Enclosing Scope: The inner function captures variables from the outer (enclosing) function’s scope.

Lifetime Extension: The captured variables persist even after the outer function has finished executing.

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

Give an example of a python closure

A

Define the Outer Function:
The outer function has a local variable and an inner function.

def outer_function(x):
def inner_function(y):
return x + y
return inner_function

Return the Inner Function:
The outer function returns the inner function, which forms the closure.

add_five = outer_function(5)

Invoke the Closure:
The returned inner function retains access to x from the outer function.

print(add_five(3)) # Output: 8

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

what are the usecases for a python closure?

A

Encapsulation: Closures help in data hiding by encapsulating data within the function’s scope.

Function Factories: They can create multiple functions with different behaviors based on the captured variables.

Callback Functions: Useful for creating callback functions that retain state.

17
Q

What is concurrency?

A

Concurrency is the ability of a program to make progress on more than one task at the same time. It involves managing multiple tasks that can run independently but not necessarily simultaneously.

18
Q

What is parallelism?

A

Parallelism is the simultaneous execution of multiple tasks. It involves splitting a task into smaller sub-tasks and executing them concurrently on multiple processors to achieve faster results.

19
Q

How does concurrency differ from parallelism?

A

Concurrency involves managing multiple tasks that may be in progress at the same time but not necessarily executing simultaneously. Parallelism involves the simultaneous execution of multiple tasks, often achieved through multiple processors.

20
Q

What is the Global Interpreter Lock (GIL) in Python?

A

The Global Interpreter Lock (GIL) is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecode simultaneously. This can limit the performance of CPU-bound programs in Python.

21
Q

How can you achieve concurrency in Python?

A

Concurrency in Python can be achieved using threading, asyncio for asynchronous programming, and multiprocessing.

22
Q

What is the threading module in Python?

A

The threading module in Python provides a way to create and manage threads. It allows for concurrent execution of code, though it is limited by the GIL when it comes to CPU-bound tasks.

23
Q

What is the asyncio module in Python?

A

The asyncio module in Python is used for writing single-threaded concurrent code using the async/await syntax. It is designed for IO-bound and high-level structured network code.

24
Q

What is the multiprocessing module in Python?

A

The multiprocessing module in Python allows the creation of processes, which bypass the GIL, enabling true parallelism by using separate memory spaces for each process.

25
Q

How does the multiprocessing module achieve parallelism?

A

The multiprocessing module achieves parallelism by creating separate processes, each with its own Python interpreter and memory space. This allows for true parallel execution on multiple CPU cores.

26
Q

What is the difference between process-based and thread-based concurrency?

A

Process-based concurrency uses separate memory spaces for each process, enabling true parallelism but with higher memory overhead. Thread-based concurrency uses shared memory space, which is more efficient in terms of memory but can be limited by the GIL in Python.

27
Q

What is the concurrent.futures module in Python?

A

The concurrent.futures module provides a high-level interface for asynchronously executing callables. It includes the ThreadPoolExecutor and ProcessPoolExecutor classes for managing thread and process pools, respectively.

28
Q

How do you create a thread in Python using the threading module?

A

A12: You can create a thread in Python using the threading module by creating an instance of threading.Thread and passing a target function to it. For example:

import threading

def worker():
print(“Thread is working”)

thread = threading.Thread(target=worker)
thread.start()
thread.join()

29
Q

How do you create a process in Python using the multiprocessing module?

A

**A13: You can create a process in Python using the multiprocessing module by creating an instance of multiprocessing.Process and passing a target function to it. For example:

import multiprocessing

def worker():
print(“Process is working”)

process = multiprocessing.Process(target=worker)
process.start()
process.join()

30
Q

What is an event loop in the context of asyncio?

A

An event loop is a central component of the asyncio module that manages and distributes the execution of asynchronous tasks. It continuously checks for and executes completed tasks, handling IO operations and scheduling callbacks.

31
Q

How do you run an asynchronous function using asyncio?

A

You can run an asynchronous function using asyncio by using the asyncio.run() function. For example:

import asyncio

async def main():
print(“Hello, asyncio”)

asyncio.run(main())