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

Kursinnhold

Python Functions Tutorial

Python Functions Tutorial

1. What is a Function in Python?
2. Positional and Optional Arguments
3. Arbitrary Arguments
4. Function Return Value Specification
5. Recursion and Lambda Functions

book
Single 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

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".

Oppgave

Swipe to start coding

Imagine your friends have each made a shopping list for you. To make things easier, you need to combine all the lists into one.

  1. Initialize the variable merged_list as an empty string to store the final result.
  2. Iterate through each shopping list in the shopping_lists argument using a loop.
  3. Combine the elements of the list into a single string using join() with the separator, a comma and a space (', ').
  4. Add a comma and a space after the merged string to separate lists.
  5. Use the strip() method to remove the trailing comma and return the final shopping list.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 1
toggle bottom row

book
Single 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

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".

Oppgave

Swipe to start coding

Imagine your friends have each made a shopping list for you. To make things easier, you need to combine all the lists into one.

  1. Initialize the variable merged_list as an empty string to store the final result.
  2. Iterate through each shopping list in the shopping_lists argument using a loop.
  3. Combine the elements of the list into a single string using join() with the separator, a comma and a space (', ').
  4. Add a comma and a space after the merged string to separate lists.
  5. Use the strip() method to remove the trailing comma and return the final shopping list.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 1
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt