50+ Python Interview Questions — Part 2

Yashuv Baskota
8 min readDec 29, 2022

Image by Yashuv Baskota

Here we continue with our previous part of Python interview questions.

26. What is the difference between a tuple and a list in Python?

A tuple is an immutable sequence type in Python, while a list is a mutable sequence type. This means that once you create a tuple, you cannot change the values of its elements or add or remove elements from it. On the other hand, you can modify a list by adding, removing, or changing its elements.

27. How do you reverse a string in Python?

To reverse a string in Python, you can use the reversed() function, which returns an iterator that iterates over the characters of the string in reverse order.

For example:

my_string = "hello"
reversed_string = ''.join(reversed(my_string))
print(reversed_string) # prints "olleh"

28. Can you use slicing to reverse a string?

Yes.

You can use slicing to reverse a string as shown below:

my_string = "hello" 
reversed_string = my_string[::-1]
print(reversed_string) # prints "olleh"

29. How do you raise an exception in Python?

To raise an exception in Python, you can use the ‘raise’ keyword followed by an instance of the exception class that you want to throw.

30. What is the difference between a shallow copy and a deep copy in Python?

A shallow copy in Python is a copy of an object that creates a new object with a new reference, but the elements of the object are still references to the original object. A deep copy, on the other hand, creates a new object with new references to all of the elements of the original object.

For example:

import copy

my_list = [1, [2, 3]]
shallow_copy = copy.copy(my_list)
deep_copy = copy.deepcopy(my_list)

my_list[1][0] = 4

print(shallow_copy) # prints [1, [4, 3]]
print(deep_copy) # prints [1, [2, 3]]

31. How do you create a class in Python?

To create a class in Python, you can use the class keyword followed by the name of the class and a set of parentheses containing the base classes, if any. The body of the class should be indented and should contain the class's attributes and methods.

For example:

class MyClass:
def __init__(self, value):
self.value = value

def get_value(self):
return self.value

32. What is the purpose of the ‘else’ clause in a ‘try / except’ block in Python?

The else clause in a ‘try / except’ block in Python is executed if no exceptions are raised in the try block. It is useful for running code that should be executed only when the try block completes successfully.

33. What is a Python slice?

A Python slice is a subset of a sequence. It is defined by a start and stop index and an optional step value. Slices are created using the [start:stop:step] syntax and are used to extract a portion of a sequence.

34. What is a Python lambda function?

A Python lambda function is a small anonymous function that is defined without a name. It is created using the lambda keyword and is used to perform simple tasks or to pass as an argument to other functions.

35. How do you create a generator in Python?

To create a generator in Python, you can use the ‘yield’ keyword in a function definition. When the function is called, it will return a generator object that can be used to iterate over a sequence of values.

36. What is the ‘__init__ ’method in Python?

The __init__ method in Python is a special method that is called when an object is created. It is used to initialize the attributes of the object and to perform any other necessary setup.

37. What is the self keyword in Python?

The self keyword in Python is a special parameter that is used to refer to the current instance of a class. It is used to access the attributes and behaviors of the object from within the class.

38. What is the ‘__str__’ method in Python?

The __str__ method in Python is a special method that is called when the ‘str()’ function is used to convert an object to a string. It is used to implement string representation for the object and to return a string that represents the object.

39. What is the ‘__repr__’ method in Python?

The __repr__ method in Python is a special method that is called when the repr() function is used to represent an object. It is used to return a string representation of the object that is suitable for debugging and development.

40. What is the ‘__call__’ method in Python?

The __call__ method in Python is a special method that is called when an object is treated like a function and called using parentheses. It allows you to define a custom behavior for the object when it is called.

41. What is the ‘__len__’ method in Python?

The __len__ method in Python is a special method that is called when the len() function is used to get the length of an object. It is used to return the length of the object, which is often the number of elements it contains.

42. What is the ‘__getitem__’ method in Python?

The __getitem__ method in Python is a special method that is called when an object is indexed using square brackets. It is used to implement sequence-like behavior for the object and to allow access to its elements.

43. What is the ‘__setitem__’ method in Python?

The __setitem__ method in Python is a special method that is called when an object is indexed using square brackets and assigned a new value. It is used to implement sequence-like behavior for the object and to allow modification of its elements.

44. What is the ‘__delitem__’ method in Python?

The ‘__delitem__’ method in Python is a special method that is called when an element of an object is deleted using the del statement. It is used to implement sequence-like behavior for the object and to allow deletion of its elements.

45. What is the ‘__iter__’ method in Python?

The __iter__ method in Python is a special method that is called when an object is iterated over using a loop or other construct. It is used to implement iteration for the object and to return an iterator that produces the values of the object.

46. What is the ‘__next__’ method in Python?

The __next__ method in Python is a special method that is called when an iterator is advanced to the next value using the next() function or the for loop. It is used to implement iteration for the object and to return the next value of the iterator.

47. What is the ‘__contains__’ method in Python?

The __contains__ method in Python is a special method that is called when the in operator is used to check if an object contains a particular value. It is used to implement membership testing for the object and to return a boolean value indicating whether the value is present.

48. What is the ‘__add__’ method in Python?

The __add__ method in Python is a special method that is called when the ‘+ ’operator is used to add two objects. It is used to implement addition for the objects and to return a new object that is the result of the addition.

49. What is the ‘__sub__’ method in Python?

The __sub__ method in Python is a special method that is called when the — operator is used to subtract one object from another. It is used to implement subtraction for the objects and to return a new object that is the result of the subtraction.

50. What is the __mul__ method in Python?

The __mul__ method in Python is a special method that is called when the ‘*’ operator is used to multiply two objects. It is used to implement multiplication for the objects and to return a new object that is the result of the multiplication.

51. What is the __truediv__ method in Python?

The __truediv__ method in Python is a special method that is called when the / operator is used to divide one object by another. It is used to implement division for the objects and to return a new object that is the result of the division.

52. What is the __floordiv__ method in Python?

The __floordiv__ method in Python is a special method that is called when the ‘//’ operator is used to divide one object by another and round down to the nearest integer. It is used to implement floor division for

53. What is the __mod__ method in Python?

The __mod__ method in Python is a special method that is called when the ‘%’ operator is used to divide one object by another and return the remainder. It is used to implement modulo division for the objects and to return a new object that is the result of the division.

54. What is the __pow__ method in Python?

The __pow__ method in Python is a special method that is called when the ‘** operator is used to raise one object to the power of another. It is used to implement exponentiation for the objects and to return a new object that is the result of the exponentiation.

55. What is the __abs__ method in Python?

The __abs__ method in Python is a special method that is called when the abs() function is used to get the absolute value of an object. It is used to implement absolute value for the object and to return a new object that is the absolute value.

56. What is the __bool__ method in Python?

The __bool__ method in Python is a special method that is called when the bool() function is used to convert an object to a boolean value. It is used to implement boolean value for the object and to return a boolean value indicating whether the object is considered True or False.

57. What is the ‘__int__’ method in Python?

The __int__ method in Python is a special method that is called when the int() function is used to convert an object to an integer. It is used to implement integer value for the object and to return an integer value representing the object.

58. What is the purpose of the ‘self’ parameter in a Python class method?

The self parameter in a Python class method is a reference to the instance of the class that the method is being called on. It is used to access the attributes and methods of the instance from within the method.

For example:

class MyClass:
def __init__(self, value):
self.value = value

def get_value(self):
return self.value

obj = MyClass(5)
print(obj.get_value()) # prints 5

59. How do you create a static method in Python?

To create a static method in Python, you can use the ‘@staticmethod’ decorator on the method definition. A static method is a method that does not have access to the instance of the class and is not bound to it.

For example:

class MyClass:
def __init__(self, value):
self.value = value

@staticmethod
def add(x, y):
return x + y

print(MyClass.add(2, 3)) # prints 5

I am open to your suggestions. Feel free to comment.

Give a Clap and Follow me for more helpful content!

Happy learning and best wishes!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Yashuv Baskota
Yashuv Baskota

Written by Yashuv Baskota

Python | Data Science and Machine Learning Practitioner | Learner | "The only way to do great work is to love what you do." - Steve Jobs

No responses yet

Write a response