Python

What is the meaning of the * operator in Python?

January 18, 2022

What is the meaning of the * operator in Python?

In this article, we will learn the meaning of the * operator in Python, which is widely used in software written in Python.

meaning of the * operator in Python cover

Hey you  programmer, okay? Let’s learn more about Python!

The * operator is used most of the time to do multiplication operations.

Check it out:

print(5 * 9)

a = 2

b = 3

print(a * b)

We can also use it to calculate a power, using ** twice, see it out:

print(2 ** 4)

a = 2;

b = 8;

print(a ** b)

But another way we can use the * is like a special syntax in functions

Indicating that that function can take an indeterminate number of arguments

Note: when the parameter is used in this way, it must be the last in position

Take a Look:

def test(*params):
 for i in params:
  print(i)

test(1,2,3,4,5) # 1 2 3 4 5

There is also the ** syntax in functions, which makes it possible to get the parameter by name, if it is passed

See an example:

def test(**params):
 if params['name']:
  print(params['name'])

test(name="Matheus", age=29)

And these are the different ways to use the * parameter in Python!

Conclusion

In this article we saw what the * operator means in Python

We have several possibilities of use, the most common being multiplication

And then two cases for functions that work with parameters

If used once with the parameter name, *param, is expecting undefined parameters

If used twice with the parameter, **param, wait indefinite and can select by name, if passed

Want to learn more about Python? Click here!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x