Answer:
Threads in Python are created using the threading module.
Example:
import threading
def display():
print("Hello Thread")
t = threading.Thread(target=display)
t.start()Threads are useful for I/O-bound tasks such as file handling, networking, and API calls.
| Thread | Process |
|---|---|
| Lightweight | Heavyweight |
| Shares memory | Separate memory |
| Faster execution | Slower than threads |
| Suitable for I/O tasks | Suitable for CPU-intensive tasks |
Created using threading | Created using multiprocessing |
Answer:
Python provides the json module for working with JSON data.
Common methods:
json.dumps() → Converts Python object to JSON string.json.loads() → Converts JSON string to Python object.json.dump() → Writes JSON to a file.json.load() → Reads JSON from a file.Example:
import json
data = {"name":"John"}
json_string = json.dumps(data)logging module?Answer:
The logging module is used to record application events, errors, warnings, and debugging information.
Logging Levels:
It helps monitor applications and troubleshoot issues.
async and await keywords in Python.Answer:async and await are used for asynchronous programming.
async defines a coroutine.await pauses execution until an asynchronous task completes.Example:
import asyncio
async def hello():
print("Hello")
asyncio.run(hello())They improve performance for I/O-bound applications.
Answer:
Context Managers automatically manage resources like files, database connections, and network sockets.
They implement:
__enter__()__exit__()Used with the with statement.
Example:
with open("file.txt") as f:
print(f.read())Answer:
Python provides several profiling tools:
Optimization techniques:
itertools module?Answer:
The itertools module provides fast and memory-efficient iterator functions.
Common functions:
It is widely used in data processing and algorithm development.
any() and all() functions.Answer:
Returns True if at least one element is True.
Example:
any([False, True, False])Output:
TrueReturns True only if every element is True.
Example:
all([True, True, True])Output:
Truefunctools module?Answer:
The functools module provides higher-order functions.
Popular functions:
It is mainly used for decorators, caching, and function manipulation.
Answer:
Circular imports occur when two modules import each other.
Solutions include:
ctypes module?Answer:
The ctypes module allows Python programs to call C libraries directly.
It is used for:
Answer:
Memory leaks can be minimized by:
gc)typing module.Answer:
The typing module provides support for type hints.
Example:
from typing import List
def square(nums: List[int]) -> List[int]:
return [x*x for x in nums]Benefits:
heapq module?Answer:
The heapq module implements a priority queue (min-heap).
Common functions:
Used in scheduling, shortest path algorithms, and priority queues.
Answer:
A Singleton ensures that only one object of a class exists.
Common implementation methods:
__new__()Singletons are commonly used for:
__slots__ attribute?Answer:__slots__ restricts the attributes that instances of a class can have.
Benefits:
Example:
class Student:
__slots__ = ['name', 'age']Answer:
An iterator is created by implementing:
__iter__()__next__()Example:
class Numbers: def __iter__(self): return self def __next__(self): raise StopIteration
bisect module.Answer:
The bisect module provides binary search operations on sorted lists.
Common functions:
Advantages:
concurrent.futures module?Answer:
The concurrent.futures module provides a high-level interface for asynchronous task execution.
It supports:
Benefits:
Example:
from concurrent.futures import ThreadPoolExecutor
def task():
return "Task Completed"
with ThreadPoolExecutor() as executor:
future = executor.submit(task)
print(future.result())