Python Programming Code
Python Programming Code
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:
Performance: Slower iteration compared to tuples but better for insertion and deletion
operations.
Example:
a_list.append("Session")
Powered By
Tuple:
Performance: Faster iteration compared to lists but lacks the flexibility of lists.
Example:
Powered By
Purpose:
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
self.title = title
# Sample method
def book(self):
b = book_shop('Sandman')
b.book()
Powered By
3. What is the difference between a mutable data type and an immutable data type?
Definition: Mutable data types are those that can be modified after their creation.
Use Case: Suitable for collections of items where frequent updates are needed.
Example:
# List Example
a_list = [1, 2, 3]
a_list.append(4)
# Dictionary Example
a_dict = {'a': 1, 'b': 2}
a_dict['c'] = 3
Powered By
Definition: Immutable data types are those that cannot be modified after their creation.
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
print(a_num) # Output: 20
# String Example
a_str = "hello"
# Tuple Example
a_tuple = (1, 2, 3)