python_mock
python_mock
6. **What are lists and tuples? What is the key difference between the two?**
- Both are sequence data types in Python. The key difference is that lists are mutable, while tuples are
immutable.
37. **What are negative indexes and why are they used?**
- Negative indexes in Python allow you to access elements from the end of a sequence, where ‘-1‘
represents the last element.
class Child(Parent):
def __init__(self):
super().__init__()
self.value = "Child"
c = Child()
c.show() # Output: Child
‘‘‘
42. **Is it possible to call a parent class without its instance creation?**
- Yes, using ‘super()‘ in a subclass allows you to call a parent class method without creating an
instance of the parent class.
an integer?**
- ‘int(’123’)‘ will convert the string ‘’123’‘ to the integer ‘123‘.
52. **How will you identify and deal with missing values in a DataFrame?**
- You can identify missing values using methods like ‘isnull()‘ or ‘notnull()‘. To deal with them, you can
either fill them using ‘fillna()‘ or drop them using ‘dropna()‘.
64. **Which function is used to read input from the user in Python?**
- The ‘input()‘ function is used to read input from the user.
70. **What are some of the most commonly used built-in modules in Python?**
- Common built-in modules include ‘os‘, ‘sys‘, ‘math‘, ‘datetime‘, and ‘re‘.
71. **What are lambda functions?**
- Lambda functions are small, anonymous functions defined using the ‘lambda‘ keyword, used for
creating quick, throwaway functions.
73. **Can you easily check if all characters in the given string are alphanumeric?**
- Yes, using the ‘isalnum()‘ method of a string.
74. **Which method would you use to remove a specific item from a list?**
- The ‘remove()‘ method can be used to remove a specific item from a list.
80. **How can you update an element at index 2 in a list named ‘my_list‘?**
- You can update the element using assignment: ‘my_list[2] = new_value‘.
82. **Write a program that takes a sequence of numbers and checks if all numbers are unique.**
- Example:
‘‘‘python
def all_unique(numbers):
return len(numbers) == len(set(numbers))
‘‘‘
84. **Write a program to check and return the pairs of a given array ‘A‘ whose sum value is equal to a
target value ‘N‘.**
- Example:
‘‘‘python
def find_pairs(A, N):
pairs = []
for i in range(len(A)):
for j in range(i+1, len(A)):
if A[i] + A[j] == N:
pairs.append((A[i], A[j]))
return pairs
‘‘‘
86. **What will be the result of slicing the list ‘[10, 20, 30, 40, 50]‘ with ‘list[1:4]‘?**
- The result will be ‘[20, 30, 40]‘.
87. **Write a Program to match a string that has the letter ‘a’ followed by 4 to 8 ’b’s.**
- Example using regular expressions:
‘‘‘python
import re
def match_string(s):
pattern = r’a(b{4,8})’
return re.match(pattern, s)
‘‘‘
88. **Which method would you use to get the number of elements in a tuple?**
- The ‘len()‘ function is used to get the number of elements in a tuple.
89. **Write a Program to combine two different dictionaries. While combining, if you find the same keys,
you can add the values of these same keys. Output the new dictionary.**
- Example:
‘‘‘python
def combine_dicts(dict1, dict2):
combined = dict1.copy()
for key, value in dict2.items():
if key in combined:
combined[key] += value
else:
combined[key] = value
return combined
‘‘‘
94. **What will be the result of ‘list[::2]‘ if ‘list = [10, 20, 30, 40, 50]‘?**
- The result will be ‘[10, 30, 50]‘.
95. **How do you remove all elements from a list?**
- You can remove all elements using the ‘clear()‘ method: ‘my_list.clear
()‘.
‘‘‘python
my_list = [10, 20, 30, 40, 50]
max_value = max(my_list)
print(max_value)
‘‘‘
In this example, ‘max_value‘ will be ‘50‘, as it is the highest number in the list.