Python Functions Assignment Questions
Python Functions Assignment Questions
Global variables allow cross-function access by using a common reference point. As shown with 'x = 5' and function 'change()', declaring 'global x' permits direct manipulation of 'x', making 'x = x * 10 + 2' executable, updating the global 'x' to 52 . This can lead to code that's harder to debug due to potential side-effects since any function with 'global' can change the same variable, risking unintended modifications. It highlights the importance of careful global variable usage for encapsulation integrity.
Recursion involves a function calling itself with modified arguments until a base condition stops further calls. In 'fact(n)', it calculates factorials recursively. For 'fact(4)', '4 * fact(3)' is evaluated repeatedly until 'fact(0)', which returns 1, culminating in '4 * 3 * 2 * 1', resulting in 24 . The recursion ends with the base case 'n == 0' which ensures return of 1, facilitating backward calculation.
In Python, a function like 'power(base, exponent=2)' returns a single value representing base^exponent. 'print(power(3))' gives 9. Whereas 'calc(x, y)', returning a tuple '(x + y, x - y)', allows multiple values to be unpacked simultaneously. 'a, b = calc(8, 3)' unpacks results to 'a = 11' and 'b = 5', illustrating tuple unpacking's utility for returning multiple outputs from a single function call . This differentiation encourages concise, efficient value handling.
In Python, a variable defined outside a function is global unless declared otherwise within a function. In 'change()', defining 'x = 20' creates a local variable separate from the global 'x=10'. Therefore, 'Inside function: 20' is printed. If 'global x' is used as in a different snippet, 'x = x * 10 + 2' modifies the global 'x', resulting in '52'. Outside 'change()', the initial 'x' value remains unchanged at '10' unless explicitly updated by 'global' .
Keyword arguments allow explicit pairing of argument names with values, enhancing readability and allowing out-of-order declaration. In 'student(grade=11, name="Riya")', named parameters are used, enabling the call to be made without concern for order. Similarly, 'report(y=5, x="Inactive")' and 'info(b="Ten", a="Zoe")' use keyword arguments to ensure clarity and specificity in assigning values, contrasting positional arguments which require adherence to parameter order .
For input 'Python 3.9', 'funstr(s)' iterates through each character in the string, appending those that are digits to 'T'. Consequently, '39' is created by concatenating numeric characters '3' and '9' . This illustrates the use of loop iteration and conditional checks (e.g., 'i.isdigit()') for selective string reconstruction—a typical string manipulation strategy in Python programming.
Immutable data types like integers or strings don't change after assignment, creating new objects for any modifications, whereas mutable types like lists allow in-place modification. In 'x = 10', integers are immutable. If changed inside 'change()', the new 'x = 20' refers to a distinct object locally. Conversely, function 'add_item(item, my_list=[])' modifies 'my_list' directly through 'append', demonstrating how mutable types persist modifications across calls unless explicitly recreated . This impacts how data is shared and managed in memory during execution.
The document employs 'split' to separate 'S = "Wow Hi Madam"' into a list of words ['Wow', 'Hi', 'Madam']. Loop control via 'for W in L:' iterates over this list. The 'if' conditional checks revese equivalence to determine structure, either '#'-separated or '*'-separated 'upper' representations. This manipulation showcases Python's powerful string handling, employing logical checks and transformations for structured data processing, including palindrome checks and string traversal .
Default arguments allow a function to initialize parameters with default values if no arguments are provided. For example, in 'info(name, city="Delhi")', calling 'info("Anjali")' results in 'Anjali lives in Delhi' because 'city' defaults to 'Delhi' . Similarly, 'greet()' results in 'Welcome Guest' because both 'name' and 'message' have default values. In 'describe(person="Someone", age=18)', calling 'describe()' yields 'Someone is 18 years old' for the same reason .
Using mutable default arguments like lists can lead to unexpected behavior because the default list is shared among all calls to the function. For 'add_item(item, my_list=[])', the list 'my_list' accumulates items across different function calls. For instance, calling 'add_item(1)' then 'add_item(2)' results in the same list being returned with elements [1, 2], not individual lists [1] and [2] as might be expected . This can cause bugs if one assumes a fresh list each time.