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

book
Arguments

In Python, function arguments are the inputs you provide to a function when you call it. They allow you to supply data or values that the function can utilize. These arguments can be individual values or objects like lists, tuples, dictionaries, or custom objects.

For example, when we use the print() function, we provide a string as an argument. Here's another example:

Suppose we want to write a function that calculates the sum of two different numbers and prints the result. To achieve this, we need to pass these two numbers to the function using parentheses ().

# 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)
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

We have passed num_1 and num_2 as arguments to the function and then used them to calculate the sum. You can set an arbitrary number of function arguments.

Let's consider an example where we use a list as an argument for the function:

# 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)
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.

Uppgift

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.

Lösning

def calculate_triangle_perimeter(a, b, c):
# Calculate perimeter by summing all sides
perimeter = a + b + c
# Print the calculated perimeter
print(f'The perimeter is {perimeter}')

#Testing the result
calculate_triangle_perimeter(2, 3, 5)
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 3
def calculate_triangle_perimeter(___):
# Calculate perimeter by summing all sides
perimeter = ___
# Print the calculated perimeter
print(f'The perimeter is {perimeter}')

#Testing the result
calculate_triangle_perimeter(2, 3, 5)

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt