Ashish Kapil
3 min readJan 31, 2021

--

Hi, I am a Python developer with 5 years of experience. I have given 100s of interviews.

I can guarantee you the questions which you see in top searches are never asked in the interviews for eg. What is PYTHONPATH, what is namespace, is Python scripting or programming language. etc

So, let’s get to know real questions which will help you cracking Python based interview questions:

Que1.) What is the difference between List and Tuple?

Ans1.)

LIST

  • List is mutable
  • Iterations are slower
  • Un-Hashable
  • Cannot be created as Key of the dictionary
  • Consume more memory

TUPLE

  • Tuple is immutable
  • Iterations are faster
  • Hashable
  • Can be created as the key of the dictionary
  • Consume less memory

`

The basic difference between list and tuple is that List is mutable and Tuple is immutable data-structure.

Lets try explaining with an example.

Let’s take a list:

list1 = [0,1,2,3,4]

Let’s do:

list1[0] = 10print(list1)

Output:

>>> [10,1,2,3,4]

Now try:

del list[3:]

print(list1)

Output:

>>> [10, 1, 2, 3, 4]

Let’s get to Tuple

tuple1 = (1,2,3,4)

tuple1[0] = 10

Output : TypeError : ‘tuple’ object does not support item assignment.

And

del tuple[3:]

Output : TypeError : ‘tuple’ object does not support item assignment.

Que2.) What are decorators in Python?

Ans2.)

  • As we know functions are objects in Python. Functions can be defined within another function and can be passed as argument to another function.
  • Therefore,function defined inside another function is a decorator.
  • We use ‘@’ for using a decorator.

Example:

def demo_decorator(func):   def inner():
print('Hi I am starting of decorator')
func()
print('Hi I am ending of decorator')
return inner
@demo_decorator
def foo():
print('Hi I am the foo function')

Output:

>>> Hi I am starting of decorator    Hi I am the foo function    Hi I am ending of decorator

Que 3.) Difference between Deep copy and Shallow copy?

Ans 3.) First of all we have to keep in mind that ‘=’ (assignment) operator does not copy objects, it just creates a new variable which points to the same reference to the previously defined variable.

Example:

a = [10,9,8,7]
b = a
a[0] = 0
print('a = ',a)
print('b = ',b)

Output:

>>> a = [0,9,8,7]
>>> b = [0,9,8,7]

So the changes made in list ‘a’ will also be reflected in list ‘b’. So it means we have not copied it.

Shallow Copy: It copies the only the references of the data to the new collection object.

Deep Copy: It copies the data and references recursively of the child objects.

Still confused?

Example:

Shallow copy:

import copy
a = [10,9,8,7]
b = copy.copy(a) #used for shallow copy
a[0] = 0
print('a = ',a)
print('b = ',b)

Output:

>>> a = [0,9,8,7]>>> b = [10,9,8,7]

So far so good, but now let’s see the difference:

import copy
a = [10,9,['a','b','c'],8,7]
b = copy.copy(a) #shallow copy
a[2][0] = 'z'
print('a = ',a)
print('b = ',b)

Output:

>>> a = [0,9,['z','b','c'],8,7]>>> b = [10,9,['z','b','c'],8,7]

Here you can see, the element at 0th index is not changed but ‘a’ inside the list of list did get changed. So shallow copy just copied the reference of the internal list and so the changes in the internal list are reflected in the copied list too.

Deep Copy:

import copy
a = [10,9,['a','b','c'],8,7]
b = copy.deepcopy(a)
a[0] = 0
a[2][0] = 'z'
print('a = ',a)
print('b = ',b)

Output:

>>> a = [0,9,['z','b','c'],8,7]>>> b = [10,9,['a','b','c'],8,7]

Now, we can see that List ‘b’ is copied recursively and the changes in ‘a’ does not reflect in ‘b’.

For more questions and detailed answers please visit : http://www.getechready.com/python-interview/

--

--