In Python, ** is the exponentiation operator

WBOY
Release: 2023-08-20 23:10:30
forward
1513 people have browsed it

In Python, ** is the exponentiation operator

In this article, we will learn about the ** operator in Python.

Double Star (**) is an arithmetic operator in Python (such as , -, *, **, /, //, %). The exponentiation operator is another name for it.

What Order/Precedence Do Arithmetic Operators Take?

The rules for both Arithmetic operators and Mathematical operators are same, which are as follows: exponential is run first, followed by multiplication and division, and then addition and subtraction.

Following are the priority orders of arithmetic operators used in decreasing mode −

() >> ** >> * >> / >> // >> % >> + >> -
Copy after login

Use of double star (**) operator

Using ** As Exponentiation Operator:

It is also known for performing exponential operations in numerical data

Example

The following program uses the ** operator as the power operator in expressions −

# using the double asterisk operator as an exponential operator
x = 2
y = 4

# getting exponential value of x raised to the power y
result_1 = x**y

# printing the value of x raised to the power y
print("result_1: ", result_1)

# getting the resultant value according to the
# Precedence of Arithmetic Operators
result_2 = 4 * (3 ** 2) + 6 * (2 ** 2 - 5)

print("result_2: ", result_2)
Copy after login

Output

On executing, the above program will generate the following output −

<font face="Liberation Mono, Consolas, Menlo, Courier, monospace"><span style="font-size: 14px;">result_1:  16
result_2:  30</span></font>
Copy after login

Using **As Arguments in Functions and Methods:

Double asterisks are also called **kwargs in function definitions. It is used to pass a variable length dictionary of keywords to a function

We can print the **kwargs parameters using the small function shown in the example below:

Example

The following program shows how to use kwargs in a user-defined function -

# creating a function that prints the dictionary of names.
def newfunction(**kwargs):
   # traversing through the key-value pairs if the dictionary
      for key, value in kwargs.items():
   # formatting the key, values of a dictionary
   # using format() and printing it
      print("My favorite {} is {}".format(key, value))
# calling the function by passing the any number of arguments
newfunction(language_1="Python", language_2="Java", language_3="C++")
Copy after login

Output

On executing, the above program will generate the following output −

My favorite language_1 is Python
My favorite language_2 is Java
My favorite language_3 is C++
Copy after login

We can easily use keyword arguments in our code through **kwargs. The best part is that when we use **kwargs as parameters, we can pass a large number of parameters to the function. Creating functions that accept **kwargs is the best option when a relatively small number of inputs are expected in the argument list.

in conclusion

This article taught us about Python's ** operator. We learned about the precedence of operators in the Python compiler, as well as how to utilize the ** operator, which functions like a kwargs and may accept any amount of arguments for a function and is also used to calculate the power.

The above is the detailed content of In Python, ** is the exponentiation operator. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
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!