0% found this document useful (0 votes)
27 views

Positional Arguments in Python

Uploaded by

jincy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Positional Arguments in Python

Uploaded by

jincy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Positional Arguments in Python

During a function call, values passed through arguments should be


in the order of parameters in the function definition. This is called
positional arguments.

An argument is a variable, value or object passed to a function or method as


input. Positional arguments are arguments that need to be included in the
proper position or order.

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.

Here's how the function execution proceeds:

1. The value 10 is assigned to the parameter a, 20 is assigned to b, and


30 is assigned to c.
2. The expression (a + b + c) is evaluated as (10 + 20 + 30) , resulting in
60.
3. The return statement returns the value 60.
4. Finally, print(add(10, 20, 30)) prints 60 to the console.

The order of the arguments is important when using positional arguments. If


you were to change the order of the arguments in the function call, the
results would be different. For example, add(20, 30, 10) would give you 60
since 20 + 30 + 10 = 60 , but add(30, 10, 20) would give you 60 as well
because the addition is still happening in the same order.
In the example you provided, the info function takes two positional arguments: name and age. These
arguments represent the name and age of a person, respectively. Let's go through the code and explain
what happens line by line:

def info(name, age):

print(f"Hi, my name is {name}. I am {age * 365.25} days old.")

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.

Now let's see how the function is called:

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.

When this code is executed, it prints the following result:

Hi, my name is Alice. I am 8400.75 days old.

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:

TypeError: can't multiply sequence by non-int of type 'float'

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)

You might also like