Computer Science
Assertion (A): The return statement in Python allows a function to return more than one value at a time.
Reasoning (R): Python functions can return multiple values as a tuple, which is unpacked only if necessary.
Answer
Both A and R are true, and R is the correct explanation of A.
Explanation — In Python, a function can return more than one value at a time by returning multiple values separated by commas. These values are packed into a tuple. If needed, the tuple can be unpacked into separate variables.
Related Questions
State whether the following statement is True or False:
The
finallyblock in Python is executed only if no exception occurs in the try block.The code provided below is intended to swap the first and last elements of a given tuple. However, there are syntax and logical errors in the code. Rewrite it after removing all errors. Underline all the corrections made.
def swap_first_last(tup) if len(tup) < 2: return tup new_tup = (tup[-1],) + tup[1:-1] + (tup[0]) return new_tup result = swap_first_last((1, 2, 3, 4)) print("Swapped tuple: " result)Assertion (A): Positional arguments in Python functions must be passed in the exact order in which they are defined in the function signature.
Reasoning (R): This is because Python functions automatically assign default values to positional arguments.
- Both A and R are true, and R is the correct explanation of A.
- Both A and R are true, and R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.