Positional Arguments in Python
Positional Arguments in Python
The first positional argument always needs to be listed first when the function is
called. The second positional argument needs to be listed second and the third
positional argument listed third, etc.
def add ( a, b, c ):
return (a + b + c)
print (add( 10 , 20 , 30 ))
# Output: 60
In this example, we have a function called add that takes three positional
arguments: a, b, and c. These arguments represent the values that will be
added together.
When we call the add function with add(10, 20, 30), the values 10, 20, and
30 are passed as arguments in the same order they are defined in the
function.
In this part, we define the info function. It takes two parameters: name and age. Inside the function, we
use an f-string to format a string that includes the values of name and age. The expression age * 365.25
calculates the approximate number of days the person has lived based on their age.
info("Alice", 23.0)
Here, we call the info function with two positional arguments. The string "Alice" is passed as the first
argument, which corresponds to the name parameter in the function. The number 23.0 is passed as the
second argument, which corresponds to the age parameter in the function.
The string interpolation in the print statement substitutes the values of name and age * 365.25 into the
string, resulting in the output shown above.
Now let's consider the case where the arguments are passed in the wrong order:
info(23.0, "Alice")
Here, the arguments "Alice" and 23.0 are passed in reverse order compared to the previous function
call.
When this code is executed, it raises a TypeError with the following error message:
This error occurs because the code attempts to multiply a string ("Alice") by a float (23.0), which is not a
valid operation. It highlights the importance of providing arguments in the correct order when using
positional arguments. The function expects the first argument to be a name (a string) and the second
argument to be an age (a number), and passing them in the wrong order leads to unexpected behavior
or errors.
LIVE….
Let's see an example to understand how positional arguments work:
def greet(name, age):
print("Hello", name)
print("You are", age, "years old.")
greet("Alice", 25)
In this example, we define a function called greet that takes two positional
parameters: name and age. When we call the greet function and pass the
arguments "Alice" and 25, the argument "Alice" is assigned to the name
parameter, and 25 is assigned to the age parameter. The function then prints the
greeting message using these values.
Hello Alice You are 25 years old.
It's important to note that when using positional arguments, the order of the
arguments matters. If you swap the order of the arguments when calling the
function, the values will be assigned to the parameters in the same order,
resulting in different output:
greet(25, "Alice")
Output:
Hello 25 You are Alice years old.
Here, 25 is assigned to the name parameter, and "Alice" is assigned to the age
parameter, which produces unexpected output.
QUESTIONS
Write a Python function called calculate_area that calculates the area of a
rectangle. The function should take two positional arguments: length and width. It
should return the calculated area. Test the function with different values for
length and width.
def calculate_area(length, width):
area = length * width
return area
# Test the function
length = 5
width = 3
area = calculate_area(length, width)
print("The area of the rectangle is:", area)
Output:
The area of the rectangle is: 15
Create a function called print_info that takes three positional arguments: name,
age, and occupation. The function should print out the information in the
following format: "Name: [name], Age: [age], Occupation: [occupation]". Test the
function with different values for the arguments.
def print_info(name, age, occupation):
print("Name:", name)
print("Age:", age)
print("Occupation:", occupation)
# Test the function
name = "John"
age = 30
occupation = "Engineer"
print_info(name, age, occupation)