53 Python Interview Questions and Answers
This article compiles 53 common Python interview questions covering fundamentals, data structures, functions, OOP, and standard library features, providing concise explanations and code examples to help data scientists and developers prepare for technical interviews.
This article lists 53 Python interview questions with answers, aimed at data scientists and software engineers preparing for technical interviews.
1. What is the difference between a list and a tuple?
Lists are mutable and ordered, while tuples are immutable and ordered. Example of a list: ["Seth", "Ema", "Eli"] . Example of a tuple: (2, "Ema", "2020-04-16") .
2. How to perform string interpolation?
Three methods without importing Template: f-strings, % operator, and format.
name = 'Chris'
print(f'Hello {name}')
print('Hey %s %s' % (name, name))
print("My name is {}".format(name))3. Difference between "is" and "=="?
"is" checks object identity (id), while "==" checks value equality. Example demonstrates that two lists with the same contents are equal (==) but not identical (is).
a = [1,2,3]
b = a
c = [1,2,3]
print(a == b) # True
print(a == c) # True
print(a is b) # True
print(a is c) # False
print(id(a))
print(id(b))
print(id(c))4. What is a decorator?
A decorator wraps an existing function to add extra functionality. Example of a logging decorator:
def logging(func):
def log_function_called():
print(f'{func} called.')
func()
return log_function_called
@logging
def my_name():
print('chris')
my_name()5. Explain the range function
Range creates integer sequences and can take 1‑3 arguments. Examples:
[i for i in range(10)] # [0, 1, 2, ..., 9]
[i for i in range(2,10)] # [2, 3, ..., 9]
[i for i in range(2,10,2)] # [2, 4, 6, 8]6. Define a class "Car" with attributes "color" and "speed" and return "speed".
class Car:
def __init__(self, color, speed):
self.color = color
self.speed = speed
car = Car('red', '100mph')
print(car.speed) # '100mph'7. Difference between instance, static, and class methods
Instance methods receive self , static methods use @staticmethod and have no access to class or instance data, and class methods receive cls and can modify class state. Example uses a CoffeeShop class.
class CoffeeShop:
specialty = 'espresso'
def __init__(self, coffee_price):
self.coffee_price = coffee_price
def make_coffee(self):
print(f'Making {self.specialty} for ${self.coffee_price}')
@staticmethod
def check_weather():
print('Its sunny')
@classmethod
def change_specialty(cls, specialty):
cls.specialty = specialty
print(f'Specialty changed to {specialty}')
shop = CoffeeShop('5')
shop.make_coffee()
CoffeeShop.check_weather()
CoffeeShop.change_specialty('drip coffee')
shop.make_coffee()... (questions 8‑53 continue in the same format) ...
The remaining questions cover topics such as function objects, map/reduce/filter, mutable vs immutable objects, list operations, copying, differences between lists and arrays, ORM usage, any/all, dictionary lookup speed, modules vs packages, increment/decrement, binary representation, duplicate removal, string manipulation, control flow statements, list comprehensions, ternary operators, type checking, dictionary comprehensions, exception handling, and more.
Each answer includes a brief explanation and a code snippet wrapped in tags for clarity.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.