What are positional arguments?

QuestionsQuestions8 SkillsProDec, 22 2025
0106

In Python, positional arguments are values passed to a function that are assigned to the parameters based on their physical order (position) in the function call.

Here is a simple breakdown:

1. The Order Matters

When you use positional arguments, the first value goes to the first parameter, the second value goes to the second parameter, and so on.

def describe_pet(animal_type, pet_name):
    print(f"I have a {animal_type} named {pet_name}.")

# Positional arguments: 
# 'hamster' goes to animal_type
# 'Harry' goes to pet_name
describe_pet('hamster', 'Harry')

If you switch the order: describe_pet('Harry', 'hamster'), the output would be "I have a Harry named hamster," which doesn't make sense!

2. Characteristics

  • Sequential: You must remember the exact order the function expects.
  • Concise: They are shorter to write than keyword arguments because you don't type the parameter names.
  • Required: Unless default values are defined, you must provide a value for every positional argument in the correct sequence.

3. Comparison with Keyword Arguments

Based on your current lab step:

  • Positional: say_hi('John', 'Hello') (Order is strict).
  • Keyword: say_hi(greeting='Hi', name='Anna') (Order doesn't matter because you explicitly named them).

Think of positional arguments like a line at a movie theater: your place in the line determines which seat you get. Keyword arguments are like assigned seating: it doesn't matter when you arrive; your name is already on the ticket

0 Comments

no data
Be the first to share your comment!