Basic Python Interview Questions
These are some of the questions you might encounter during an entry-level Python interview.
Lists and tuples are fundamental Python data structures with distinct characteristics and use cases.
List:
Mutable: Elements can be changed after creation.
Memory Usage: Consumes more memory.
Performance: Slower iteration compared to tuples but better for insertion and deletion
operations.
Methods: Offers various built-in methods for manipulation.
Example:
a_list = ["Data", "Camp", "Tutorial"]
a_list.append("Session")
print(a_list) # Output: ['Data', 'Camp', 'Tutorial', 'Session']
Powered By
Tuple:
Immutable: Elements cannot be changed after creation.
Memory Usage: Consumes less memory.
Performance: Faster iteration compared to lists but lacks the flexibility of lists.
Methods: Limited built-in methods.
Example:
a_tuple = ("Data", "Camp", "Tutorial")
print(a_tuple) # Output: ('Data', 'Camp', 'Tutorial')
Powered By
Learn more in our Python Lists tutorial.
2. What is __init__() in Python?
The __init__() method is known as a constructor in object-oriented programming (OOP) terminology.
It is used to initialize an object's state when it is created. This method is automatically called when a
new instance of a class is instantiated.
Purpose:
Assign values to object properties.
Perform any initialization operations.
Example:
We have created a `book_shop` class and added the constructor and `book()` function. The
constructor will store the book title name and the `book()` function will print the book name.
To test our code we have initialized the `b` object with “Sandman” and executed the `book()`
function.
class book_shop:
# constructor
def __init__(self, title):
self.title = title
# Sample method
def book(self):
print('The tile of the book is', self.title)
b = book_shop('Sandman')
b.book()
# The tile of the book is Sandman
Powered By
3. What is the difference between a mutable data type and an immutable data type?
Mutable data types:
Definition: Mutable data types are those that can be modified after their creation.
Examples: List, Dictionary, Set.
Characteristics: Elements can be added, removed, or changed.
Use Case: Suitable for collections of items where frequent updates are needed.
Example:
# List Example
a_list = [1, 2, 3]
a_list.append(4)
print(a_list) # Output: [1, 2, 3, 4]
# Dictionary Example
a_dict = {'a': 1, 'b': 2}
a_dict['c'] = 3
print(a_dict) # Output: {'a': 1, 'b': 2, 'c': 3}
Powered By
Immutable data types:
Definition: Immutable data types are those that cannot be modified after their creation.
Examples: Numeric (int, float), String, Tuple.
Characteristics: Elements cannot be changed once set; any operation that appears to modify
an immutable object will create a new object.
Example:
# Numeric Example
a_num = 10
a_num = 20 # Creates a new integer object
print(a_num) # Output: 20
# String Example
a_str = "hello"
a_str = "world" # Creates a new string object
print(a_str) # Output: world
# Tuple Example
a_tuple = (1, 2, 3)
# a_tuple[0] = 4 # This will raise a TypeError
print(a_tuple) # Output: (1, 2, 3)