Python Interview Questions

Prepare for your Python interview with these commonly asked questions and answers.

Beginner Level Questions

1. What is Python?

Python is a high-level, interpreted programming language known for its simplicity and readability. It's widely used for web development, data analysis, machine learning, automation, and more.

2. What is the difference between lists and tuples?

Lists are mutable, meaning they can be modified after creation, while tuples are immutable. Lists use square brackets `[]`, while tuples use parentheses `()`.

Intermediate Level Questions

1. Explain list comprehension with an example.

List comprehension provides a concise way to create lists. For example, to create a list of squares from 1 to 10: squares = [x**2 for x in range(1, 11)]

2. What is a lambda function?

A lambda function is an anonymous function defined with the lambda keyword. It's often used for short, simple functions. Example: add = lambda x, y: x + y

Advanced Level Questions

1. What are Python decorators, and how are they used?

Decorators are a way to modify or extend the behavior of functions or methods without changing their code. They are defined with the @ symbol and are often used to add functionalities like logging or authentication.

2. Explain the Global Interpreter Lock (GIL) in Python.

The GIL is a mutex that protects access to Python objects, preventing multiple native threads from executing Python bytecodes at once. This can be a limitation for multi-threaded Python programs, especially in CPU-bound tasks.