Advanced Python Flashcards
What is a generator?
Specialized code that is able to return a series of values and to control the iteration process.
what is a iterator in python?
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.
Define a custom iterator
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
What is comprehension?
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]
What are lambda Functions?
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.
What is the syntax for a lambda function?
lambda arguments: expression
add = lambda x, y: x + y
print(add(3, 5)) # Output: 8
What are the 3 characteristics of lambda functions?
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.
What is a map function?
- 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).
Give me an example of a map function
numbers = [1, 2, 3, 4]
squares = map(lambda x: x**2, numbers)
print(list(squares)) # Output: [1, 4, 9, 16]
What is a lambda filter function?
Using Lambda with filter()
The filter() function filters items out of an iterable based on a function that returns True or False.
Give an example of a lambda filter function
numbers = [1, 2, 3, 4, 5, 6]
evens = filter(lambda x: x % 2 == 0, numbers)
print(list(evens)) # Output: [2, 4, 6]
Explain using Lambda with sorted() or sort() with an example:
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)]
explain using Lambda with reduce()
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
What are closures in python?
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.
Give an example of a python closure
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
what are the usecases for a python closure?
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.
What is concurrency?
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.
What is parallelism?
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.
How does concurrency differ from parallelism?
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.
What is the Global Interpreter Lock (GIL) in Python?
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.
How can you achieve concurrency in Python?
Concurrency in Python can be achieved using threading, asyncio for asynchronous programming, and multiprocessing.
What is the threading module in Python?
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.
What is the asyncio module in Python?
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.
What is the multiprocessing module in Python?
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.
How does the multiprocessing module achieve parallelism?
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.
What is the difference between process-based and thread-based concurrency?
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.
What is the concurrent.futures module in Python?
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.
How do you create a thread in Python using the threading module?
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()
How do you create a process in Python using the multiprocessing module?
**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()
What is an event loop in the context of asyncio?
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.
How do you run an asynchronous function using asyncio?
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())