Grok the Python interview with confidence: 15 questions and answers
Review essential Python concepts and show off your understanding at your next interview!
Hey Grokking Python readers, and happy Thursday!
When was the last time you brushed up on your core Python knowledge? You know, the details you probably learned when you were starting to pick up the language, but don't typically use in your day-to-day coding? It may seem like a chore, but this kind of review can pay off when you've got an interview looming. Even if you're content in your job, it never hurts to be prepared in case your circumstances change.
Today, we're picking up where we left off in October and talking more about Python interview prep. We'll share 15 additional questions, along with concise answers. Like last time, these will not be coding problems, but questions about fundamental knowledge and concepts that will show off your understanding of Python.
Let's begin!
Common Python interview questions
Your technical interview will almost certainly include live coding exercises. But before you start coding, you'll likely face technical questions about the language itself. Review the following 15 questions to build up your familiarity and comfort with Python.
1. What is a Python decorator?
A Python decorator is a language feature that allows developers to "wrap" or "decorate" functions and methods to modify their behavior. Decorators are typically used to add functionality to existing functions or methods but can also create new ones.
Decorators use a function or method as an argument and then return a modified version of that function or method.
2. What is __init__
in Python?
__init__
is a constructor method that allocates memory and runs automatically whenever a class creates an object or instance. It is used exclusively within classes to let the class initialize the created object's attributes.
3. What is a lambda function in Python?
A lambda function works like any other Python function but anonymously (meaning it has no name) and is contained in a single line of code. Lambda functions can accept multiple parameters but can only accept one expression.
Syntax of a lambda function:
lambda parameter(s): expression
The function evaluates the expression and returns its results.
Example of a lambda function with a single parameter:
x = lambda y : y + 4
The function takes a single parameter
y
and returnsy+4
.
Example of a lambda function with multiple parameters:
x = lambda y, z : y * z
Here, the function takes two parameters
y
andz
. It returns their product.
4. What's the difference between an iterable and an iterator in Python?
In Python, the iterator is implemented via two methods: _iter_
and _next_
.
An iterable is a container object that contains other objects known as "items". For example, a list is iterable because lists contain other list items as well as other types of objects like numbers and strings. An iterable will have its _iter_
method defined.
An iterator is a way to iterate over the items in a container, such as a list of integers. Each item in the list can be iterated over one at a time using the iterator function. An iterator will have both _iter_
and _next_
defined, where _iter_
returns the iterator object, and _next_
returns the next element in the iteration.
Iterables are an important concept to master because they're often used to loop over the contents of a container object.
Reflect: What other iterables can you name?
5. What is a Python generator?
Python generators are functions that yield a series of values. When you call a generator function, it returns a special iterator that can be used to access the generated items. So, you can think of a generator as a function for building iterators.
The biggest benefit of a generator is that it can iterate over large datasets. When working with a dataset that would otherwise take up your computer's memory, a generator can break up that data set into chunks and return it piece by piece.
Note: Generators are sometimes referred to as co-routines.
6. What is the difference between ==
and is
in Python?
Equality testing uses the values of objects to determine whether they are equal.
Identity testing uses the memory location of the objects to determine if they are equal.
==
checks if two values are the same (i.e., tests for equality)is
checks for identity, which checks to see if a string is the same as another string while taking into account whitespaces and other differences
For example, two lists can be equal if they have the same contents, but they aren't necessarily identical.
7. What is the difference between str()
and repr()
in Python?
Both str()
and repr()
are used to return a string representation of an object.
str()
prioritizes readability for humans. The string representation of an object's raw data may look different from its actual raw data. It is generally used for creating output that humans will readrepr()
prioritizes readability for machines. The string representation of an object's raw data is represented as is. Generally used for debugging and development.
8. What is GIL?
A Global Interpreter Lock, or GIL, is a mechanism used in computer programming to ensure that only one thread can access the Python interpreter at any time. This lock allows Python programs to be written in a way that is safe from race conditions, which can occur when multiple threads try to access the same data simultaneously.
9. What is the difference between a shallow copy and a deep copy?
A shallow copy returns a reference to the original object and makes no additional copies. For example, making a shallow copy of a list object creates a new list that contains references to all the items of the source list.
A deep copy copies the items in the list and returns a reference to the newly created copies.
10. What is the difference between serialization and pickling?
Serialization refers to a feature where an object is transformed into a format to be stored, which can then be deserialized to obtain the original object.
Serialization in Python is made possible through the process of pickling. Pickling converts and compresses the data of a Python object to a sequence of bytes that can be safely stored in a file while preventing accidental data corruption by system crashes.
Pickling also makes data transfer between systems that use different file formats possible.
The function for pickling is pickle.dump()
.
11. What do the split()
and join()
functions do?
split()
is used to split a string into a list of stringsjoin()
joins a list of strings to return a single string
12. What is the difference between *args
and **kwargs
?
*args
is short for arguments**kwargs
is short for keyword arguments
Both *args
and **kwargs
pass arguments to a function. However, **kwargs
are used when you want to specify keyword arguments in a function call, whereas *args
are used when you just want to pass a positional argument to a function.
13. What is a Python docstring?
A Python docstring is a string literal used to document a Python module, class, function, or method. It occurs as the first statement in a module, function, class, or method definition.
Modules: Documents the contents of a module (variables, classes, functions, etc.)
Classes: Documents the class attributes and methods
Functions: Documents the parameters, return values, and exceptions of a function
Methods: Documents the method parameters and return values
It's a best practice to include docstrings in your code because it helps other developers quickly read and understand what your code does. Although it isn't a strict requirement, it does reflect well to have a habit of documenting your code.
14. What is the difference between range()
and xrange()
?
range()
returns a list of numbersxrange()
returns an iterator that generates numbers on demand using a technique known as yielding
xrange()
is generally used when working with large numbers, as it doesn't store the entire list in memory.
15. What makes Python an interpreted language?
Python doesn't need to be compiled before it runs. Instead, it runs through an interpreter. Interpreters run your code line by line, whereas compilers run the code all at once, so interpreted languages are generally slower than compiled ones. However, code written in interpreted languages is easier to debug and portable across different platforms.
25 more interview questions to review
Other Python questions could certainly come up in your interview. Here are 25 more to review.
What are local and global variables?
How are
continue
,break
, andpass
statements used in loops?What is a negative index in Python?
What functions can be used to add elements to a Python list?
What is the Python
zip()
function?How do you import and interact with an OS in Python?
Which module is used to work with regular expressions in Python?
What is the
doctest
module used for?What is the difference between
_iter_
and_next_
?What is a magic method?
What is the
yield
statement used for?How do you define private variables in Python?
What Python modules have you used?
How does Python implement garbage collection?
What is reference counting in Python?
What is packing and unpacking in Python used for?
What are some things you like and dislike about using Python?
What are some common Python libraries?
What is unpickling?
Can Python be used for functional programming?
What are negative indexes?
What is the difference between
import
andfrom
?How do you access values in a dictionary?
What is the difference between a public and a private member?
What is memoization?
Take your interview prep to the next level
Upcoming interviews can be stressful. Being prepared helps relieve the pressure. If you know what types of questions you’ll likely face, you can practice answering them, familiarizing yourself with their related concepts, and connecting them to your personal experience.
The 25 questions we covered today and in our previous interview-prep edition are some of the most common Python interview questions employers ask. You likely won't memorize specific answers to these questions to regurgitate in your interview. But having a general understanding of the topics they cover and being able to speak intelligently about them will help you make a strong impression.
Your next step should be to incorporate coding problems into your interview preparations. But don't try drilling endless practice questions. Instead, simplify your interview prep by unlocking the patterns behind the industry’s most common questions. We'll be writing about coding interview patterns in a December edition of Grokking Python, but to get started now, check out Educative's all-new course, Grokking Coding Interview Patterns in Python. Covering 24 patterns, this course was developed by FAANG hiring managers to prepare you for the typical rounds of interviews at major tech companies.
With enough preparation, you'll be able to face your interview confidently and get one step closer to your dream job.
As always, happy learning!