Can you explain the `*` operator?

QuestionsQuestions8 SkillsProPython Control StructuresNov, 17 2025
084

The * operator in Python has several uses, but one of its most common uses is for unpacking iterables. When used in a function call, it allows you to unpack the elements of an iterable (like a list or a tuple) and pass them as separate positional arguments to the function.

Example of Unpacking with *

Here's a simple example:

numbers = [1, 2, 3]
print(*numbers)

This will output:

1 2 3

In this case, *numbers unpacks the list so that each element is passed as a separate argument to the print() function.

Using * in Function Definitions

You can also use * in function definitions to accept a variable number of positional arguments:

def my_function(*args):
    for arg in args:
        print(arg)

my_function(1, 2, 3)

This will output:

1
2
3

Here, *args collects all the positional arguments passed to my_function into a tuple named args.

Summary

  • Unpacking: Use * to unpack elements from an iterable when calling a function.
  • Variable Arguments: Use * in function definitions to accept a variable number of positional arguments.

If you have more questions about the * operator or anything else, feel free to ask!

0 Comments

no data
Be the first to share your comment!