Multiple Return Values in Python
Sometimes you need to return multiple objects from a function. You can do this in two ways:
Using a List or Tuple
Create a list or tuple that contains all required objects inside the function and return it as the function's result.
123456789101112# Define a function def return_multiple_objects(): obj1 = 'Hello' obj2 = 42 obj3 = [1, 2, 3] # Return all objects packed into list return [obj1, obj2, obj3] # Get the list with corresponding objects result_list = return_multiple_objects() for obj in result_list: print(obj)
You created three different objects inside the function and returned a list containing them as the function's output. Then, you iterated through this list to access each object.
Using Multiple Return Values
You can directly return multiple values separated by commas. When function is called, the results are captured in separate variables.
12345678910def return_multiple_objects(): obj1 = "Hello" obj2 = 42 obj3 = [1, 2, 3] # Return objects separated by comma return obj1, obj2, obj3 # Get the result of the function into three different values result1, result2, result3 = return_multiple_objects() print(result1, result2, result3)
In the code above, you returned three variables separately. When using this approach, it is important to know the order in which the variables are returned to use them correctly in the code.
Swipe to start coding
Implement a validate_registration function that validates user registration details by checking the username, email, and password. If any validation rule is not met, the function should return a list of error messages. Otherwise, it should confirm successful validation.
- Define the function
validate_registrationwith the parametersusername,email, andpassword. - Create an empty list called
errorsto collect all validation error messages. - Check whether the
usernameis at least 3 characters long, and if not, add"Username must be at least 3 characters long."to theerrorslist. - Verify that the
emailcontains the@symbol, and if it does not, add"Invalid email format."to theerrorslist. - Check whether the
passwordis at least 6 characters long, and if it is not, add"Password must be at least 6 characters long."to theerrorslist. - Return the result of
len(errors) == 0as the first value and theerrorslist as the second value.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 6.67
Multiple Return Values in Python
Swipe to show menu
Sometimes you need to return multiple objects from a function. You can do this in two ways:
Using a List or Tuple
Create a list or tuple that contains all required objects inside the function and return it as the function's result.
123456789101112# Define a function def return_multiple_objects(): obj1 = 'Hello' obj2 = 42 obj3 = [1, 2, 3] # Return all objects packed into list return [obj1, obj2, obj3] # Get the list with corresponding objects result_list = return_multiple_objects() for obj in result_list: print(obj)
You created three different objects inside the function and returned a list containing them as the function's output. Then, you iterated through this list to access each object.
Using Multiple Return Values
You can directly return multiple values separated by commas. When function is called, the results are captured in separate variables.
12345678910def return_multiple_objects(): obj1 = "Hello" obj2 = 42 obj3 = [1, 2, 3] # Return objects separated by comma return obj1, obj2, obj3 # Get the result of the function into three different values result1, result2, result3 = return_multiple_objects() print(result1, result2, result3)
In the code above, you returned three variables separately. When using this approach, it is important to know the order in which the variables are returned to use them correctly in the code.
Swipe to start coding
Implement a validate_registration function that validates user registration details by checking the username, email, and password. If any validation rule is not met, the function should return a list of error messages. Otherwise, it should confirm successful validation.
- Define the function
validate_registrationwith the parametersusername,email, andpassword. - Create an empty list called
errorsto collect all validation error messages. - Check whether the
usernameis at least 3 characters long, and if not, add"Username must be at least 3 characters long."to theerrorslist. - Verify that the
emailcontains the@symbol, and if it does not, add"Invalid email format."to theerrorslist. - Check whether the
passwordis at least 6 characters long, and if it is not, add"Password must be at least 6 characters long."to theerrorslist. - Return the result of
len(errors) == 0as the first value and theerrorslist as the second value.
Solution
Thanks for your feedback!
single