python-interview-practice-questions

7 Python Interview Questions to Practice

10/27/2020

This article is part of a series by our friends at Career Karma, focused on tech industry careers. Check out previous posts on JavaScript technical interview prep, tech salaries, and technical resumes.

There is no better way to prepare for a technical interview than to practice a few coding questions. It may be impossible to account for every question you may be asked but any practice you do will help you build confidence and refine your programming skills.

The technical interview is not just about your coding skills. It’s about whether you can explain your code well and document what you have done. That’s another reason practice is so important. You’ll learn how to talk about Python programming and the code that you write.

Python interview questions

In this article, we’ve drafted seven Python interview questions to help you prepare for your next technical interview. For more interview prep, check out our guide to interviewing in the tech industry, including technical and behavioral interview tips.

Describe the concept of scope in Python with reference to local and global scope.

Scope is the region in which a variable is accessible in a program. There are two scopes in Python. Local scope is a variable that is defined inside a Python function. Variables with local scope can only be accessed inside a function.

Global scope is a variable that is declared in the main part of a program. Global variables are accessible throughout a program, including within functions. You can make a local variable global by using the “global” keyword inside a function.

What is the difference between a tuple and a list?

Both Python tuples and lists are used to store collections of data.

A list is declared as a collection of comma-separated values enclosed within square brackets whereas tuples are declared as a collection of values enclosed in curly brackets.

A list is mutable. This means their contents can be modified. Tuples are immutable. You cannot change the contents of a tuple. You must declare a new tuple instead if you want to modify the contents of a value inside a tuple.

Write a list comprehension that takes the following list and creates a new list containing the names of students whose names are four letters long:

students = [“Hannah”, “Peter”, “Luke”]

A list comprehension is a more “Pythonic” way to create a list. You can use list comprehensions to create lists based on the contents of an existing collection.

We can use the following code to create a list of students whose names contain four letters:

four_letter_names = [name for name in students if len(name) == 4]
# [“Luke”]

Our list comprehension iterates over all the students in the “students” variable. The len() string method calculates the length of each name. If the length of a name is equal to four, that name is added to our “four_letter_names” list. Otherwise, the name is not added to our list.

What is a lambda function? Why are they used?

A lambda function is a function that has no name. Lambda functions are commonly referred to as anonymous functions.

You can use a lambda function as an alternative to declaring a regular function as long as the function can fit onto one line. Consider the following example:

prices = [2.50, 3.10, 2.70]

over_three_dollars = list(filter(lambda price: (price > 3), prices))

This code goes through the “prices” list and returns only the prices that are greater than $3.00. In this example, we use a lambda function to define a rule for our filter() method. The rule states that a price should be added to a list if that price is greater than $3.00.

Our code returns [3.1] because there is only one value in our “prices” list that is greater than $3.00.

What is the __init__ method? Write an example of an __init__ method for the following properties:

Class Name: Student
Properties: name, date of birth, grade

The __init__ method defines a constructor in an object. This method is called when an object is created. __init__ is a reserved keyword.

__init__ methods can accept as many arguments as you specify when you declare the constructor. The __init__ method lets you define values that will be associated with an object.

An __init__ method should appear at the start of a new class, before any methods are declared. Consider the below example:

class Student():
	def __init__(self, name, date_of_birth, grade):
		self.name = name
		self.date_of_birth = date_of_birth
		self.grade = grade

We have declared a class called Student. Our class has three attributes: name, date_of_birth, and grade.

In our constructor, we assign the value of the arguments that the user specifies to their respective values in the “self” variable. This makes each value accessible in our object when we reference “self”.

We can declare an object of our class like so:

mark = Student(“Mark”, “19/02/2007”, 8)

We have created a student called Mark. Mark’s date of birth is 19/02/2007. He is in grade eight.

What is *args and **kwargs? Give an example of **kwargs in use.

The *args and **kwargs statements let you accept a variable number of arguments in a function.

The *args statement returns a tuple of arguments. You can access the items in the tuple using indexing. The **kwargs statement returns a dictionary of arguments. Arguments are mapped using keys and values.

*args is short for “arbitrary arguments” whereas **kwargs is short for “arbitrary keyword arguments”. This is because **kwargs arguments are associated with keywords.

We can use **kwargs to refer to an argument by a label that we assign when we pass a value as an argument:

def show_age(**student):
	print(“{} is {} years old.”.format(student[“name”], student[“age”]))

show_age(age=9, name=”Lynn”)

This function uses **kwargs to accept information about a student. Rather than accessing each argument as a variable, we use the “student” variable. The “student” variable is a dictionary of all the arguments we have passed.

The **kwargs statement is commonly used if you do not know how many arguments you are going to pass.

Explain the main features that make Python an object-oriented programming language.

Python, like Java and many other languages, is referred to as an object-oriented programming language. This means you can create objects and classes to help organize your code.

A class is a blueprint. They are used to define the structure of an object. A class can include the values that an object can store and the methods that can be run on an object.

An object is an instance of a class. When you initialize, or create, an object, you can specify attributes (values and methods) that are to be associated with that object.

For instance, “Student” could be the name of a class. This class would store all the values that can be associated with a student record at a school. “Alex” could be an object. This object would store details on a student whose name is Alex.

Wrapping up

Do not worry about preparing for every possible question that could come up. You’ll only be asked a few questions in your interview. Interview practice is about covering the essentials and building up your confidence.

As you ask and answer more interview questions, you’ll refine the craft of explaining your work. You’ll learn how to write more efficient code that will help set you aside as the best candidate for a job.

Whether you are doing your first technical interview, or whether you are already a senior engineer, practice will help you improve your chances of landing your dream job.

James Gallagher is a writer at Career Karma. He leads technical content on the Career Karma publication. James has authored dozens of articles for an audience of code newbies on Python, HTML, CSS, JavaScript, Java, and Linux.

Related articles

7 articles