Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Arguments | What Is a Function in Python?
Python Functions Tutorial

bookArguments

In Python, function arguments are the inputs you provide to a function when calling it. They let the function use specific data or values. Arguments can be single values or objects such as lists, tuples, dictionaries, or custom objects.

For instance, the print() function takes a string as an argument.

If you want to create a function that calculates and prints the sum of two numbers, you must pass both numbers to the function inside parentheses ().

123456789
# Specify two arguments of the function def sum_of_two_numbers(num_1, num_2): # Use arguments to calculate sum result = num_1 + num_2 # Print the result print(f'Sum of two numbers is {result}') # Call the function sum_of_two_numbers(2, 10)
copy

You have passed num_1 and num_2 as arguments to the function and used them to calculate their sum. You can define any number of function arguments.

Now, consider an example where a list is used as an argument for a function:

123456789101112131415
# Define the function `calculate_list_sum` def calculate_list_sum(list): # Initialize the total sum to 0 total = 0 # Iterate through each number in the list for num in list: # Add the current number to the total sum total += num # Print the total sum print(total) # Create a list of numbers my_list = [1, 2, 3, 4, 5] # Call the function calculate_list_sum(my_list)
copy

This code defines a function calculate_list_sum that takes a list of numbers, calculates their sum by adding each number to the total variable, and prints the result. The function is called with the list my_list, containing the numbers [1, 2, 3, 4, 5], and outputs the sum, which is 15.

Task

Swipe to start coding

Assume that you have to calculate perimeter of the triangle with sides a, b and c.

  1. Pass three arguments: a, b, and c to the function calculate_triangle_perimeter.
  2. In the function, calculate the perimeter by adding the three sides together.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 3
single

single

Ask AI

expand

Ask AI

ChatGPT

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

Suggested prompts:

Can you explain the difference between passing a single value and a list as a function argument?

What happens if I pass an empty list to the `calculate_list_sum` function?

Can I use other data types, like dictionaries, as function arguments in Python?

close

Awesome!

Completion rate improved to 4.35

bookArguments

Swipe to show menu

In Python, function arguments are the inputs you provide to a function when calling it. They let the function use specific data or values. Arguments can be single values or objects such as lists, tuples, dictionaries, or custom objects.

For instance, the print() function takes a string as an argument.

If you want to create a function that calculates and prints the sum of two numbers, you must pass both numbers to the function inside parentheses ().

123456789
# Specify two arguments of the function def sum_of_two_numbers(num_1, num_2): # Use arguments to calculate sum result = num_1 + num_2 # Print the result print(f'Sum of two numbers is {result}') # Call the function sum_of_two_numbers(2, 10)
copy

You have passed num_1 and num_2 as arguments to the function and used them to calculate their sum. You can define any number of function arguments.

Now, consider an example where a list is used as an argument for a function:

123456789101112131415
# Define the function `calculate_list_sum` def calculate_list_sum(list): # Initialize the total sum to 0 total = 0 # Iterate through each number in the list for num in list: # Add the current number to the total sum total += num # Print the total sum print(total) # Create a list of numbers my_list = [1, 2, 3, 4, 5] # Call the function calculate_list_sum(my_list)
copy

This code defines a function calculate_list_sum that takes a list of numbers, calculates their sum by adding each number to the total variable, and prints the result. The function is called with the list my_list, containing the numbers [1, 2, 3, 4, 5], and outputs the sum, which is 15.

Task

Swipe to start coding

Assume that you have to calculate perimeter of the triangle with sides a, b and c.

  1. Pass three arguments: a, b, and c to the function calculate_triangle_perimeter.
  2. In the function, calculate the perimeter by adding the three sides together.

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Β 1. ChapterΒ 3
single

single

some-alt