What does ** mean in python?

醉折花枝作酒筹
Release: 2023-01-05 16:09:21
Original
89655 people have browsed it

In Python, "**" represents a power operation. Just use "**" between two numbers to indicate that the two numbers are subject to a power operation; the first operand is the base. , the second operand is the exponent. For example, "2**3" can represent 2 raised to the third power, and the result is 8.

What does ** mean in python?

The operating environment of this tutorial: windows7 system, python3 version, DELL G3 computer

** represents power operation in python

When passing actual parameters and defined formal parameters (the so-called actual parameters are the parameters passed in when calling the function, and the formal parameters are the parameters defined by the defining function), you can also use two special syntaxes: ``*` `**.

The function of using * **

test(*args)* when calling a function is actually to pass each element in the sequence args as a positional parameter. For example, in the above code, if args is equal to (1,2,3), then this code is equivalent to test(1, 2, 3).

test(**kwargs)** is used to pass dictionary kwargs into keyword parameters. For example, in the above code, if kwargs is equal to {'a':1,'b':2,'c':3}, then this code is equivalent to test(a=1,b=2,c=3).

Use * when defining function parameters **

def test(*args):

...The meaning of * is different when defining function parameters. Here *args means that all the positional parameters passed in are stored in the tuple args. For example, if the function above calls test(1, 2, 3), the value of args will be (1, 2, 3). :

def test(**kwargs):

...Similarly, ** is for keyword parameters and dictionaries. If test(a=1,b=2,c=3) is called, the value of kwargs is {'a':1,'b':2,'c':3}.

Ordinary parameter definition and transfer methods can coexist peacefully with *, but obviously * must be placed at the end of all positional parameters, and ** must be placed at the end of all keyword parameters, otherwise it will There is an ambiguity

Related free learning recommendations: python video tutorial!

The above is the detailed content of What does ** mean in python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!