Skip to Content
Course content

68: Variable-Length Arguments with *args

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've spent years reviewing code from junior developers, and there is one specific pattern that pops up constantly when people first encounter *args. They treat the word args as if it's a reserved keyword in Python—like True, None, or self. They think that if they change the name to *items or *numbers, the code will suddenly break because "Python expects the word args."

The "Magic Keyword" Myth

Let me show you why that's not true. If you believe args is a magic word, you might write a function like this, feeling restricted by the naming convention:

def sum_everything(*args):
    return sum(args)

But here is the reality: the word args means absolutely nothing to the Python interpreter. The only thing that matters is the asterisk (*). The asterisk is the operator that tells Python, "Take all the remaining positional arguments passed to this function and pack them into a tuple."

I personally find it much cleaner to name the variable based on what it actually contains. If I'm building a function to track a series of temperature readings, I'm not going to call them args. I'll call them temps:

def calculate_average_temp(*temps):
    # Python doesn't care that I didn't use the word 'args'
    return sum(temps) / len(temps)

print(calculate_average_temp(72, 75, 68, 70)) # Works perfectly

The Magic is in the Asterisk

When you put * before a parameter name, you are creating a "variable-length" argument list. This is incredibly useful when you don't know ahead of time how many inputs the user will provide. Instead of forcing the user to wrap their data in a list or a tuple before calling your function, you let them just pass the values in naturally.

One thing you need to keep in mind—and this trips people up—is that inside the function, that variable is always a tuple. You can't .append() to it or change its values because tuples are immutable. If you need to manipulate the data, you'll have to convert it to a list first.

Here is a real-world scenario: imagine you're building a simple checkout system for a coffee shop. A customer might buy one latte, or they might buy five different things. You can't define a fixed number of parameters for every possible order.

def generate_receipt(customer_name, *items):
    print(f"Receipt for {customer_name}:")
    for item in items:
        print(f"- {item}")
    print(f"Total items: {len(items)}")

# I can pass two items...
generate_receipt("Alice", "Espresso", "Blueberry Muffin")

# ...or ten items. The function doesn't care.
generate_receipt("Bob", "Latte", "Scone", "Tea", "Bagel", "Water")

Notice how customer_name is a standard positional argument, while *items catches everything else. Just remember: the *args (or *whatever) must come after your fixed positional arguments. If you try to put it first and then add a required argument after it, Python will get confused about which value belongs where, and you'll end up with a SyntaxError.




📋 Practical Task

Building a Tournament Score Aggregator

You are tasked with creating a function called find_tournament_winner. This function needs to handle a varying number of players because no two tournaments have the same number of participants.

Requirements:

  • The function should take one required argument: tournament_name (a string).
  • The function should take a variable number of positional arguments representing player scores (integers).
  • The function should identify the highest score and print a message in this format: "The winner of [tournament_name] scored [highest_score]!"
  • If no scores are provided (meaning the *args tuple is empty), it should print: "No scores recorded for [tournament_name]."

Example usage:

find_tournament_winner("Summer Slam", 45, 88, 92, 71) 
# Output: The winner of Summer Slam scored 92!

find_tournament_winner("Winter Open") 
# Output: No scores recorded for Winter Open.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.