Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is a positional argument in Python

Understanding Positional Arguments

When you start learning programming, especially in a language like Python, you encounter a variety of terms that may seem confusing at first. One of these terms is "positional argument." Let's break this down into simpler concepts to understand what it really means.

Imagine you're telling a friend how to make a sandwich. You might say, "First, take a slice of bread, then add lettuce, followed by a slice of tomato, and finally top it with another slice of bread." You're giving instructions in a specific order, and your friend is expected to follow that order to make the sandwich correctly. In programming, when we talk about positional arguments, we're referring to a similar concept. The order in which you provide information to a function is crucial.

Functions and Arguments

Before we delve into positional arguments, let's briefly talk about functions and arguments in general. A function is like a mini-program or a set of instructions within your code that you can use over and over again. It's like a recipe for a specific dish that you can follow whenever you want to make that dish.

Arguments are the specific ingredients or pieces of information that you give to a function so that it can do its job. For example, if you have a function that adds two numbers together, the two numbers are the arguments.

Positional Arguments in Action

Now, let's look at some actual code examples to see positional arguments in action. Here's a simple function in Python that takes two arguments and adds them together:

def add_numbers(first_number, second_number):
    return first_number + second_number

result = add_numbers(3, 5)
print(result)  # This will print 8

In this example, 3 and 5 are positional arguments. The function add_numbers expects the first value to be associated with first_number and the second value with second_number, based on their positions.

Why Position Matters

Why does the order matter? Let's modify our function a bit to see what happens:

def subtract_numbers(first_number, second_number):
    return first_number - second_number

result = subtract_numbers(5, 3)
print(result)  # This will print 2

If we call subtract_numbers(3, 5) instead of subtract_numbers(5, 3), the result will be -2 instead of 2. This illustrates the importance of the order of the arguments. In subtraction, the position of the numbers changes the answer, which is why we care about positional arguments.

More Complex Examples

As you get more comfortable with functions, you might encounter ones that take more than two arguments. For instance:

def create_greeting(greeting, name, punctuation):
    return greeting + ', ' + name + punctuation

welcome_message = create_greeting('Hello', 'Alice', '!')
print(welcome_message)  # This will print "Hello, Alice!"

In the create_greeting function, the order in which you pass in the greeting, the name, and the punctuation is crucial to forming the correct sentence.

Intuitions and Analogies

To help you understand positional arguments better, think of them as seats on a bus. Each seat is labeled and passengers are supposed to sit according to their ticket number. If they sit in the wrong order, confusion ensues. Similarly, in Python, when you define a function with certain parameters, you're essentially creating a set of labeled seats for your arguments. When you call the function, you need to place the arguments in the correct order, or "seats," for the function to work as expected.

What Happens When Order is Ignored?

Ignoring the order of positional arguments can lead to errors or unexpected behavior. Let's see what happens when we mix up arguments:

def mix_ingredients(ingredient1, ingredient2, ingredient3):
    return ingredient1 + ' mixed with ' + ingredient2 + ' and ' + ingredient3

cake_recipe = mix_ingredients('flour', 'eggs', 'sugar')
print(cake_recipe)  # Expected: 'flour mixed with eggs and sugar'

confused_recipe = mix_ingredients('eggs', 'sugar', 'flour')
print(confused_recipe)  # Unexpected: 'eggs mixed with sugar and flour'

In the confused_recipe, we've mixed up the order of ingredients. While it may still make sense in English, in programming, this could lead to completely different outcomes or even errors.

Tips for Working with Positional Arguments

  1. Always follow the order in which the parameters are defined in a function.
  2. Double-check the function definition if you're unsure about the order of arguments.
  3. Use clear and descriptive names for your function parameters to avoid confusion.

Conclusion: The Importance of Being Orderly

In conclusion, positional arguments are a fundamental concept in Python that can be likened to following a recipe or giving directions. The order in which you provide these arguments matters because it determines how the function processes them. As you continue on your programming journey, remember to be mindful of the order, just as you would when giving someone directions to your favorite restaurant. The clarity and precision you apply will not only make your code work effectively but also make it easier for others (and future you) to understand and maintain it. Keep practicing, and soon, dealing with positional arguments will become as natural as following a well-loved recipe.