Python Tricky Interview Questions and Answers

Python Tricky Interview  Questions and Answers

Python Tricky Interview Questions and Answers

I’m listing some of the Python tricky questions. Going through these questions will help you in many ways.

  • You learn some Python basic and advance concepts that sometimes you miss while going through the tutorials.
  • Some of the tricky questions are also asked in the job interview.
  • I have explained each of the questions. It clears your doubts.

Note: If you are new to Python, refer complete Python tutorial.

Python Tricky Interview Questions

You can make the best out of it by challenging yourself and solving Python tricky questions. Let’s see one by one.

1. Shallow Copy of the Python List

What is the output of the following code?

first = [1, 2, 3, 4, 5]
second = first
second.append(6)
print(first)
print(second)

Output:

[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]

Explanation:

You are appending a new element to the second list. Then, why do you see a new element even in the first list?

This is really confusing.

Here is actual fact.

The code line “second = first ” creates the shallow copy of the list. It means there will be a single list in memory. The first and second variables are pointed to the same list.

It does not maintain two separate lists, but only one. If you modify using one list variable, it will make the changes in the list for both variables.

If you want to maintain two separate lists for two list objects, you need to create a deep copy of the list. Refer a shallow and deep copy

You can also print the identity of the objects (first and second) using id() inbuilt function.

first = [1, 2, 3, 4, 5]
second = first
print(id(second))
print(id(first))

Output:

140683246201032
140683246201032

The identity value of both the list objects will be the same.

2. Python List Slicing Using Negative Indexing

What is the output of the following Python code snippet?

a = [1,2,3,4,5,6,7,8,9]
print(a[-1:-5])

Output:

[]

It will return the empty list.

Explanation:

Prerequisite: Python List

The slicing syntax for the list is [a:b:c]. Here a and b are start and end indexes. The ‘c’ is the step value.

In our example, both the start and end indexes are negative values.

  • a = -1 (It points to the last element in the list.)
  • b = -5 (It is the index of the fifth last element in the list.)
  • c = 1 (The step value is not mentioned. By default, step value is ‘1’ )

As step blue is ‘1’, we can move those values from left to right.

Actually, it first checks the start index and starts traversing to the right to find the end index. As it doesn’t find the end index it returns an empty list. Basically, if it doesn’t find a start or end index, every time it will return an empty list.

So, the output will be an empty list

For further understanding try running the same code with step ‘-1’

a = [1,2,3,4,5,6,7,8,9]
a[-1:-5:-1]

Output:

 [9, 8, 7, 6]

3. Tuple and String Type

Here is the code snippet. You have to find the type of the given three objects.

A = ("Python" , "Java")
print(type(A))
 
B = ("Python", )
print(type(B))
 
C = ("Python")
print(type(C))

Output:

<class 'tuple'>
<class 'tuple'>
<class 'str'>

Explanation:

These objects are classified into different data types.

  • tuple- It stores the element just like an array. (It is slightly different from list.)
  • str- it is a string that is a set of characters.

Let’s take a scenario one by one.

  • Object ‘A’ is a tuple. It’s very clear from its syntax.
  • Object ‘B’ is again tuple. Despite having only one string, it has comma ‘,’ after the string. So it can not be a string.
  • Object ‘C’ is a string. It has only one string though it has enclosed inside the brackets. So, Python considers it as a string.

4. Differentiating List extend() Method

What is the output of print statements 1 and 2 in the following code snippet?

listA = [1, 2, 3]
def funcA(listA):
    listA = listA * 2
    return None

funcA(listA)
print(listA) # print Statement 1
 
listB = [1, 2, 3]
def funcB(listB):
    listB=listB.extend([1, 2, 3])
    return None

funcB(listB)
print(listB) # print Statement 2

Output:

[1, 2, 3]
[1, 2, 3, 1, 2, 3]

Explanation:

The statement listA = listA * 2 inside the funcA creates a new list instead of modifying the original one.

Whereas the list extend() the method modifies the original list.

Other Python Tricky Questions:

What’s Next?

There is only one way to improve your coding skills. That is Practicing! No one has become an expert without practicing.

If you have any doubts or want more clarification for any of the Python tricky interview questions, let me know in the comment.

I will keep adding more Python questions. Stay tuned!

Leave a Reply

Your email address will not be published. Required fields are marked *