Python Interview Questions
Answer: Python is a high-level, interpreted, and dynamically typed programming language. It supports object-oriented, procedural, and functional programming paradigms. Python’s main features include
- Easy-to-read syntax
- Extensive standard libraries and third-party packages
- Automatic memory management
- Large community support
- Cross-platform compatibility
- Answer: PEP 8 is the Python Enhancement Proposal that outlines the style guide for writing Python code. It includes recommendations on code layout, naming conventions, and coding standards to make Python code more readable and consistent. Following PEP 8 helps maintain a consistent coding style across projects, making collaboration easier.
Answer: Python’s built-in data types include:
- Numeric:
int,float,complex - Sequence:
str,list,tuple - Set:
set,frozenset - Mapping:
dict - Boolean:
bool - NoneType:
None
Answer:
- List: Mutable, ordered collection of elements. Allows duplicate values and supports indexing.
- Tuple: Immutable, ordered collection of elements. Allows duplicate values and supports indexing.
- Set: Mutable, unordered collection of unique elements. Does not support duplicate values and is unindexed.
- Answer: A dictionary is an unordered collection of key-value pairs, defined using
{}with key-value pairs separated by colons. Unlike lists, dictionaries are indexed by unique keys rather than sequential numbers. Dictionaries are useful for associative data, whereas lists are ideal for ordered sequences.
Answer:
*argsallows a function to accept a variable number of positional arguments as a tuple.**kwargsallows a function to accept a variable number of keyword arguments as a dictionary.
Example:
def sample_function(*args, **kwargs):
print(args)
print(kwargs)
sample_function(1, 2, 3, name="John", age=25)
Output:
(1, 2, 3)
{'name': 'John', 'age': 25}Answer: A lambda function is an anonymous function expressed as a single line of code. It uses the lambda keyword and is commonly used for short, throwaway functions that perform simple operations. Syntax: lambda arguments: expression.
Example:
square = lambda x: x * x
print(square(5)) # Output: 25Answer: The Global Interpreter Lock (GIL) is a mutex in the CPython interpreter that allows only one thread to execute Python bytecode at a time, even on multi-core processors. This limits true parallelism in multi-threaded Python programs. However, GIL doesn’t affect multi-processing, and modules like multiprocessing can be used to achieve parallelism.
Answer:
- Shallow Copy: Creates a new object but inserts references to the original objects within it. Changes in nested elements affect both copies.
- Deep Copy: Creates a new object and recursively copies all objects found within the original, creating an independent copy.
Example:
import copy
original = [[1, 2], [3, 4]]
shallow = copy.copy(original)
deep = copy.deepcopy(original)Answer:
map(function, iterable): Applies a function to all items in an iterable.filter(function, iterable): Filters items in an iterable based on a condition.reduce(function, iterable): Applies a rolling computation to sequential pairs of items (requiresfunctoolslibrary).