Python Model Question Paper
Python Model Question Paper
To merge two dictionaries without overwriting keys, you can use a method that allows keeping track of both sets of keys. The option in Source 1, 'dict3 = dict1.copy(); dict3.update(dict2)' merges dict2 into dict1 and handles overlapping keys by overwriting them in this case, but to prevent overwriting, each key can be handled manually or included in a list under the same key .
The XOR operation ('x^y') toggles bits that are different between x and y. In the loop, 'x = x^y' progressively accumulates the XOR result of the current x and y. For the termination condition 'while not x or not y', it ensures that the loop executes only while at least one of x or y is zero. The final printed results are obtained after specific bitwise manipulations leading to x being 3 and y reaching 0, exhibiting the properties of XOR in modifying binary states .
Python's memory management system uses reference counting, which tracks object references in memory. In the example with 'list1' and 'list2', since 'list2 = list1', both lists reference the same memory address. Thus, altering an element via 'list2[0] = 'Guava'' affects both, whereas 'list3 = list1[:]' creates a shallow copy with independent memory space, not affecting 'list1' when modified .
Using list comprehensions keeps with functional programming principles by minimizing side effects. '[ele for ele in reversed(lst)]' creates a new reversed list without modifying the original, adhering to functional programming ideologies by avoiding mutation of the original list .
If you try to pass a list object to a comparison method expecting individual elements, such as in 'if lst in b:', it will raise a 'TypeError' because a list is unhashable and cannot be directly checked for existence in a set which expects hashable and immutable objects .
The function can use a loop to iterate through each sublist, checking for the presence of a specific element using 'if keyword in lst'. Appending the original sublist when the keyword exists ensures correct filtering. It's critical to append the sublist not the element, to preserve the intended structure while meeting the filtering criteria .
Python allows list slicing 'lst[::-1]' to achieve a reversed list without altering the original, unlike 'lst.reverse()' which mutates it. Slicing respects functional programming paradigms by offering an efficient, clear syntax for obtaining modified forms, promoting immutability and thread safety, similar to slicing lists to add elements at specific indices .
The code snippet in option 1, 'd = {'me': 1, 'you': 2, 'we': 3} while len(d) > 3: print(d.popitem(), end=" ") print('Boom!')', will fail to execute the loop body because the condition 'len(d) > 3' is false from the start, given the initial length of dictionary 'd' is 3. Therefore, the loop is never entered, and the only output will be 'Boom!' .
Using assignment operations as part of the loop's condition can inadvertently alter loop behavior. For example, setting conditions like 'if x > 4' followed by an assignment 'x = 0' directly manipulates x, affecting subsequent conditional evaluations. This tight coupling between loop control variables and internal logic can lead to non-intuitive flows and termination paths that are difficult to debug .
The example shows a tuple, 'tup = ("Item_1", 0.5, name_lst)', containing a mutable list 'name_lst'. When 'name_lst.append("Vishal")' is called, the list within the tuple is modified because tuples don't encapsulate the immutability of their contained objects' contents. Only the structure of the tuple itself (the identity of the objects contained) is immutable .