Python Programming Exam Paper 2023
Python Programming Exam Paper 2023
In Python, a shallow copy of a list copies the references to the objects in the list, not the actual objects, so changes to nested objects will reflect in both lists. A deep copy creates independent copies of all objects within the list. For list L1 = [100, 200, 300, [400, 500]], modifying nested elements in a shallow copy affects both copies, but not in a deep copy. The document illustrates this using L2 for a shallow copy and L3 for a deep copy .
In Python loops, 'break' exits the loop immediately, skipping any remaining code and subsequent iterations. 'Continue' skips the current iteration and moves to the next iteration, without exiting the loop. 'Pass' is a null operation and simply passes control to the next iteration without affecting the loop conditions. These statements influence how the loop is executed without altering the actual structure of the data being iterated over .
The function `swapTuple(T1, T2)` swaps the values of two tuples, T1 and T2, returning them with their values interchanged. For example, given T1 = (100, 200, 300) and T2 = ('Monday', 'Tuesday', 'Wednesday'), after swapping, T1 becomes ('Monday', 'Tuesday', 'Wednesday') and T2 becomes (100, 200, 300).
In Python, dictionaries can be accessed and modified using keys. For instance, to access a value, use the syntax `dict[key]`. If a key might not exist, use `dict.get(key, default)` to avoid errors and provide a default value, such as -1. To modify a dictionary, you can add, change, or remove key-value pairs. For example, `copy_dict = dict.copy()` creates a shallow copy, and `del dict[key]` removes a key .
The try-except block in Python is used for handling exceptions to prevent the program from crashing. It allows you to execute code that might cause an error, and if an error occurs, the 'except' block catches it. For example, in the function `percentage(marks, total)`, exceptions such as ZeroDivisionError and TypeError are handled to prevent crashes, and a custom message is printed for each error type .
Sets in Python can be manipulated using operations like union, intersection, difference, and symmetric difference. For example, converting lists Lst1 and Lst2 to sets S1 and S2 allows the addition of new elements to S2 using `update()`, and finding the symmetric difference using `symmetric_difference()`, which returns elements present in either set but not in both. Additionally, set comprehensions can create new sets with specific modifications, such as appending 's' to each element of a set .
The function `mergeTuple(T1, T2)` pairs corresponding elements from two tuples and returns a list of these paired tuples. For example, given T1 = ('Monday', 'Tuesday', 'Wednesday') and T2 = (100, 200, 300), the function will return T3 = [('Monday', 100), ('Tuesday', 200), ('Wednesday', 300)]. This function effectively uses the `zip()` function to combine elements from both tuples .
Class-based encapsulation in Python involves bundling data (attributes) and methods (functions) to operate on the data into a single unit, a class. The Rectangle class demonstrates encapsulation by defining attributes like length and breadth, and methods such as `getLength()` and `perimeter()` to manipulate these attributes. The methods control how the attributes can be accessed or modified, promoting data integrity .
During file operations, using 'w' mode does not allow reading from the file, so calling `read()` on a file opened with mode 'w' raises an UnsupportedOperation error. In tuple manipulation, attempting to delete an item using `del tuple1[0]` on a tuple raises a TypeError, as tuples are immutable. These errors can be corrected by opening the file in read mode 'r', and converting the tuple to a list if modification is required .
An algorithm is a step-by-step procedure or formula for solving a problem. To solve a quadratic equation using an algorithm, you can follow these steps: 1) Input coefficients a, b, and c; 2) Calculate the discriminant: D = b^2 - 4ac; 3) If D > 0, calculate two real roots using (-b ± sqrt(D)) / (2a); if D = 0, calculate one real root using -b / (2a); if D < 0, indicate that there are no real roots .