Contents
Python Interview Questions
Python Interview Questions
1. What is Python, and what are its main features?
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
2. What is PEP 8, and why is it important?
- 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.
3. What are Python’s built-in data types?
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
4. What is the difference between list
, tuple
, and set
?
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.
5. What is a dictionary in Python, and how is it different from a list?
- 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.
6. What are *args
and **kwargs
in functions?
Answer:
*args
allows a function to accept a variable number of positional arguments as a tuple.**kwargs
allows 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}
7. What is a lambda function in Python?
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: 25
8. Explain Python’s Global Interpreter Lock (GIL).
Answer: 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.
9. What is the difference between deepcopy
and shallow copy
?
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)
10. Explain map()
, filter()
, and reduce()
functions.
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 (requiresfunctools
library).
Related Chapters
- Python Introduction
- Basic Concepts
- Text Formating
- Links and Navigation
- Multimedia Elements
- Tables
- Forms and Input
- Python Features
- Web Graphics and Multimedia
- Encapsulation and Abstraction
- Embedded Content
- Scripting and Python
- Python APIs
- Virtual Functions
- Accessibility and Python
- SEO Best Practices
- Performance Optimization
- Resources and Further Learning