Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Single Return Value | Function Return Value Specification
Python Functions Tutorial

bookSingle Return Value

A single return value of a function in Python refers to returning a single object or value from a function. This type of return value has been used in previous sections.

12345
def add_numbers(a, b): return a + b result = add_numbers(3, 5) print(result) # outputs: 8
copy

The function add_numbers takes two arguments, adds them, and returns a single value β€” their sum. In this example, the result is stored in the variable result and printed to the console.

Note
Note

The join() function in Python combines elements of an iterable, such as a list, into a single string, using a specified separator.
For example, ", ".join(["a", "b"]) returns "a, b".

1234
# Example of join() words = ["red", "green", "blue"] combined = ", ".join(words) print(combined)
copy
Note
Note

The strip() method removes leading and trailing whitespace by default, or any characters you specify, from both the start and end of the string. For example, " text ".strip() returns "text" without spaces, and "a,b,c,".strip(",") removes commas from the ends, returning "a,b,c".

1234
# Example of strip() raw_text = ",a,b,c," cleaned_text = raw_text.strip(',') print(cleaned_text)
copy
Task

Swipe to start coding

Imagine that each of your friends has given you their own shopping list. To make shopping easier, you need to combine all the lists into a single list of items β€” formatted as a single string.

Follow these steps:

  1. Initialize a variable named merged_list as an empty string to store the final combined result.
  2. Loop through each list inside the shopping_lists argument.
  3. Inside the loop, merge all items of the current list into a single string using the join() method with a comma and space separator ', '.
  4. After each merged list, append a comma and space to separate it from the next one.
  5. Before returning the result, use the strip(', ') method to remove the extra comma and space at the end.
  6. Return the final merged shopping list string.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 1
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

Awesome!

Completion rate improved to 4.35

bookSingle Return Value

Swipe to show menu

A single return value of a function in Python refers to returning a single object or value from a function. This type of return value has been used in previous sections.

12345
def add_numbers(a, b): return a + b result = add_numbers(3, 5) print(result) # outputs: 8
copy

The function add_numbers takes two arguments, adds them, and returns a single value β€” their sum. In this example, the result is stored in the variable result and printed to the console.

Note
Note

The join() function in Python combines elements of an iterable, such as a list, into a single string, using a specified separator.
For example, ", ".join(["a", "b"]) returns "a, b".

1234
# Example of join() words = ["red", "green", "blue"] combined = ", ".join(words) print(combined)
copy
Note
Note

The strip() method removes leading and trailing whitespace by default, or any characters you specify, from both the start and end of the string. For example, " text ".strip() returns "text" without spaces, and "a,b,c,".strip(",") removes commas from the ends, returning "a,b,c".

1234
# Example of strip() raw_text = ",a,b,c," cleaned_text = raw_text.strip(',') print(cleaned_text)
copy
Task

Swipe to start coding

Imagine that each of your friends has given you their own shopping list. To make shopping easier, you need to combine all the lists into a single list of items β€” formatted as a single string.

Follow these steps:

  1. Initialize a variable named merged_list as an empty string to store the final combined result.
  2. Loop through each list inside the shopping_lists argument.
  3. Inside the loop, merge all items of the current list into a single string using the join() method with a comma and space separator ', '.
  4. After each merged list, append a comma and space to separate it from the next one.
  5. Before returning the result, use the strip(', ') method to remove the extra comma and space at the end.
  6. Return the final merged shopping list string.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 1
single

single

some-alt