# Python * Single And **Double Asterisk

Updated Apr 24, 2024

This article explains the double-asterisk operator or double-star operator (**) and the difference between single-asterisk(*) and double asterisk(**) in different scenarios.

## What does ** mean in Python?

In the case of numerical data values, ** works as an exponentiation operator.
For example, follow the below code:

Code:

``````a=2
b=6
c=2**6
print("The required output is {0}".format(c))``````

Output:

In function, using ** allows inserting multiple arguments.
For example, follow the below code:

Code:

``````def fun(**nos):
for a in nos:
print (a,nos[a])
fun(a=1, b=3, c=2)``````

Output:

## Difference between using double asterisks (**) and pow():

We already know the working of double asterisks (**) from the above topics. Pow() also computes x**y. First, it transforms the argument into float and then calculates the power.

The pow() returns only float values while double asterisks (**) return in every type.

``````x = pow(2, 3)
print(x)``````

## Which one is faster?

The double asterisks (**) is faster and more employed than pow(). It is because pow() is a function that takes a lot of memory compared to an operator, inside which a overloading of operator is taking place.

pow() function also takes parameters that are added to memory locations. Operator ** takes 2 operands and running operands are faster compared to parameters.

## What are *args and **kargs

In the Python programming language, some special symbols allow passing multiple arguments. They are:

1. *args for non-keyword arguments
2.  **kargs for keyword arguments

With the operator * or ** in the prefix, one can use any other name instead of args and kargs.

## Difference between single asterisks(*)and double asterisks(**)

1. In the case of numeric data values, the single asterisks (*) work as a multiplication operator, while the double- asterisks (**) work as an exponentiation operator.
2. In a function definition, the single asterisk (*) takes an iterator like a list and extends it into a series of arguments, whereas the double-asterisk (**) takes dictionary only and expands it.
3. In a function call, both * and ** allow entering multiple arguments. The single-asterisks (*) enable to pass multiple non-keyword arguments, while double asterisks (**) enables to pass keyword arguments

Applications of single asterisks(*) and double asterisks(**) with examples

## FOR UNPACKING INTO A FUNCTION CALL

single-asterisks (*) unpacks an iterable into the arguments in a function call:

Code:

``````fruits = ['apple', 'banana', 'grape', 'orange']
print(fruits[0], fruits[1], fruits[2], fruits[3])
print(*fruits)``````

Output:

Double-asterisks (**) also unpacks an iterable into the arguments in a function but only for keyword arguments. The Double-asterisks (**) operator permits to take a dictionary of key-value pairs and unpack it in keyword arguments in the function call

Code:

``````date_inf = {'year': "2000", 'month': "03", 'day': "04"}
file = "{year}-{month}-{day}.txt".format(**date_inf)
print(file) ``````

Output:

## Use the Single-asterisk(*) and Double-asterisks (**) multiple times

One can use * multiple times in a function

Code:

``````fruits = ['Apple', 'Banana', 'watermelon', 'Grape']
numbers = [1, 2, 3, 4, 5]
print(*numbers, *fruits)``````

Output:

Use double asterisks (**) multiple times to merge two dictionaries.

Code:

``````A = {'val1': 10, 'val2': 20}
B = {'val3': 30, 'val4': 40}
# Merging d1 and d2
C = {**A, **B}
print("AFTER MERGING: ", C)``````

Output:

Conclusion:

It has numerous features, advantages, and uses in different tech fields. This article describes the double-asterisks. Double asterisks (**) acts as an exponentiation operator for numeric values.

When used in a function definition, it allows the programmer to input multiple arguments and parameters. When it comes to power, ** operator is lightweight as compared to the pow() function because functions takes more processing and compilation power (because it takes parameters and has multiple statements embedded within it) than this operator.

Recommended Posts:

Contact Information

#3940 Sector 23,
Gurgaon, Haryana (India)
Pin :- 122015

contact@stechies.com

Top Tutorials

Top Interview Questions