Python Lab Exam Questions 2021
Python Lab Exam Questions 2021
Python lists are well-suited for insertion and deletion because they provide native methods (`insert()`, `pop()`, and `remove()`) that facilitate these operations. However, efficiency can be a concern for large lists due to the O(n) complexity for operations that resize the list, such as insertions and deletions not near list ends. In contrast, Python's `collections.deque` offers more efficient O(1) operations for additions and removals from both ends of a sequence.
While simple interest calculation follows a direct formula involving principal, rate, and time, which can be computed in one step, compound interest, especially with compounding periods, typically requires iteratively or recursively applying interest over each period to the principal. This compounded amount becomes the new principal for subsequent periods, thus involving a looped or recursive approach for accurate computation.
Finding the greatest of five numbers involves comparing each number with a current maximum being tracked. This requires a fixed number of comparisons (five comparisons in the worst case), making it a simple application of direct comparison logic. It scales linearly with the number of elements, as each added element increases the number of comparisons by one, maintaining an O(n) time complexity.
Checking for an Armstrong number involves calculating the power of its digits (raised to the number of digits) and summing them, then comparing this sum to the original number. This process requires multiple exponentiation operations and digit extractions, which are more computationally intensive than palindromes, which merely involve reversing digits and comparing, leading to increased computational complexity in the former.
Concatenating strings in Python uses the '+' operator, which, unlike numerical operations, does not perform mathematical addition but rather joins the strings end-to-end to form a new string. This operation is computational in string handling and does not result in any arithmetic sum, differentiating it significantly from arithmetic operations where '+' operates mathematically.
The benefits of using Python lists for linear search include ease of implementation and direct access to elements via indices. However, a demerit includes suboptimal efficiency for large data sets due to O(n) time complexity, making it less suitable for performance-critical applications compared to more efficient data structures or search algorithms such as binary search on sorted data, which performs better with O(log n) complexity.
A linear search on a list has a time complexity of O(n) because it involves traversing the list once to find the target integer. In contrast, sorting a list has higher time complexity; typical sorting algorithms like Merge Sort have a time complexity of O(n log n). Hence, linear search is generally more computationally efficient for single searches compared to complete sorting, which rearranges the entire list even if only a single element needs to be located.
Optimizing search for greatest and smallest elements involves reducing unnecessary comparisons. A common optimization is using a single pass through the list or tuple to update both greatest and smallest values, reducing the number of traversals to one. However, since tuples are immutable, the process doesn’t affect them differently than lists, aside from access operations. Efficient algorithms may employ early termination or parallel processing for large datasets to further optimize.
Conceptually, both programs involve repetitive processes: checking each condition (characters) in the case of palindromes and iterative multiplication in factorials. The practical difference lies in the nature of these processes; palindrome checks involve symmetric comparison, while factorial calculations require iterative or recursive numerical multiplications, thus necessitating different logic implementations and operation complexity.
Finding the sum of elements in a Python list or a tuple involves similar logic, wherein you iterate over the elements and increment a cumulative sum variable. However, the primary difference lies in the data structure's mutability; lists are mutable, allowing elements to be modified, added, or removed, whereas tuples are immutable. Therefore, the process of summing elements in a tuple must handle immutability without altering the tuple itself.

