NCERT Computer Science Class #11 – Chapter #7 Solutions – Q #5

Chapter #7 – Functions

NCERT Computer Science Class #11 – Chapter #7 Solutions – Q #5

Chapter #7 – Q #5

Using an example show how a function in Python can return multiple values.



You can return multiple values through a tuple:

# Creating a tuple with multiple values
coordinates = (10, 20, 30)

# Unpacking the tuple into individual variables
x, y, z = coordinates

print("X coordinate:", x)  # Output: X coordinate: 10
print("Y coordinate:", y)  # Output: Y coordinate: 20
print("Z coordinate:", z)  # Output: Z coordinate: 30

Return multiple values through a function:

def get_student_info():
    # Returning multiple values as a tuple
    return "John Doe", 21, "Computer Science"

# Unpacking the returned tuple
name, age, major = get_student_info()

print("Name:", name)   # Output: Name: John Doe
print("Age:", age)     # Output: Age: 21
print("Major:", major) # Output: Major: Computer Science

Swap variables using a tuple:

# Initial values
a = 5
b = 10

# Swapping values using tuple unpacking
a, b = b, a

print("A:", a)  # Output: A: 10
print("B:", b)  # Output: B: 5

Combining tuples:

# Creating two tuples
tuple_1 = (1, 2)
tuple_2 = (3, 4)

# Concatenating tuples
combined_tuple = tuple_1 + tuple_2
print(combined_tuple)  # Output: (1, 2, 3, 4)

Function that returns sum and average:

def sum_and_avg(x, y, z):
    s = x + y + z
    a = s / 3
    return s, a  # Returning multiple values as a tuple

# Calling the function and unpacking results
(Sum, Avg) = sum_and_avg(3, 8, 5)

print('Sum =', Sum)   # Output: Sum = 16
print('Avg =', Avg)   # Output: Avg = 5.333333333333333

NCERT Computer Science Class 11 Chapter 7 Solutions Q 5

Posts created 29

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top