I’m listing some of the Python tricky questions. Going through these questions will help you in many ways.
Note: If you are new to Python, refer complete Python tutorial.
You can make the best out of it by challenging yourself and solving Python tricky questions. Let’s see one by one.
Table of Contents
What is the output of the following code?
first = [1, 2, 3, 4, 5] second = first second.append(6) print(first) print(second)
[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6]
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))
The identity value of both the list objects will be the same.
What is the output of the following Python code snippet?
a = [1,2,3,4,5,6,7,8,9] print(a[-1:-5])
It will return the empty list.
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.
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]
[9, 8, 7, 6]
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))
These objects are classified into different data types.
Let’s take a scenario one by one.
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
[1, 2, 3] [1, 2, 3, 1, 2, 3]
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.
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!