Python Interview Questions

In this Python Interview Questions blog, I will introduce you to the most frequently asked questions in Python interviews. This blog is the perfect guide for you to learn all the concepts required to clear a Python interview. Our Python Interview Questions is the one-stop resource from where you can boost your interview preparation.

Q1. What is the difference between list and tuples?

List is mutable and tuples is immutable. The main difference between mutable and immutable is memory usage when you are trying to append an item. When you create a variable, some fixed memory is assigned to the variable. However, if you know that you are not frequently add new elements, then you should use tuples

Apart from tuples being immutable there is also a semantic distinction that should guide their usage. Tuples are heterogeneous data structures (i.e., their entries have different meanings), while lists are homogeneous sequences. Tuples have structure, lists have order.

LIST vs TUPLES

LISTTUPLES
Lists are mutable i.e they can be edited. Tuples are immutable (tuples are lists which can’t be edited).
Lists are homogeneous sequences Tuples are heterogeneous data structures i.e their entries have different meanings
lists have order. Tuples have structure.
Lists are slower than tuples. Tuples are faster than list.
Syntax:
list_ex = ["Monday", "Tuesday"]
Syntax:
tuple_ex = ("Monday", "Tuesday")
Accessing:
list_ex[1]
Accessing:
tuple_ex[0]

Q2. What are the key features of Python?

Q3. What is the difference between deep and shallow copy?

Shallow copy is used when a new instance type gets created and it keeps the values that are copied in the new instance. Shallow copy is used to copy the reference pointers just like it copies the values. These references point to the original objects and the changes made in any member of the class will also affect the original copy of it. Shallow copy allows faster execution of the program and it depends on the size of the data that is used.

Deep copy is used to store the values that are already copied. Deep copy doesn’t copy the reference pointers to the objects. It makes the reference to an object and the new object that is pointed by some other object gets stored. The changes made in the original copy won’t affect any other copy that uses the object. Deep copy makes execution of the program slower due to making certain copies for each object that is been called.

Let's take an example where we create a list named old_list and pass an object reference to new_list using = operator.

Result

Old List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
ID of Old List: 52620464
New List: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
ID of New List: 52620464

Adding new nested object using Shallow copy

Result

Old list: [[1, 1, 1], [2, 'AA', 2], [3, 3, 3]]
New list: [[1, 1, 1], [2, 'AA', 2], [3, 3, 3]]

Adding a new nested object in the list using Deep copy

Result

Old list: [[1, 1, 1], ['BB', 2, 2], [3, 3, 3]]
New list: [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

Q4. How is Multithreading achieved in Python?

Q5. How can the ternary operators be used in python?

The Ternary operator is the operator that is used to show the conditional statements. This consists of the true or false values with a statement that has to be evaluated for it.

Ternary operators also known as conditional expressions are operators that evaluate something based on a condition being true or false. It simply allows to test a condition in a single line replacing the multiline if-else making the code compact.

Result

10
10
10
10
b is greater than a
b is greater than a
b is greater than a