What is correct name of * operator available in Python?


In this article, we will explain the correct name of the * operator available in python.

In Python, you'll encounter the symbols * and ** used frequently. Many Python programmers, especially those at the intermediate level, are confused by the asterisk (*) character in Python.

The *args argument is called the "variable positional parameter" and **kwargs is the "variable keyword parameter". The * and ** arguments just unpack their respective data structures.

Using Asterisk ( * ) Operator in Multiplication 

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create two variables and store them in two separate variables.

  • Use the * operator to multiply both the input numbers and create a variable to store it.

  • Print the resultant multiplication of two numbers.

Example

The following program returns the multiplication of two numbers using the * operator −

# input number 1 inputNumber_1 = 10 # input number 2 inputNumber_2 = 5 # multiplying both the input numbers using the * operator multResult = inputNumber_1 * inputNumber_2 # printing the resultant multiplication of 2 numbers print("Resultant multiplication result using *:", multResult)

Output

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

Resultant multiplication result using *: 50

Using Asterisk ( ** ) Operator in Exponentiation

  • Create two variables and store them in two separate variables.

  • Use the ** operator to get the exponential value of inputNumber_1 to the power of inputNumber_2 (Here 2^5) and create a variable to store it.

  • Print the resultant exponential value.

Example

The following program returns the exponential result of two numbers using the ** operator −

# input number 1 inputNumber_1 = 2 # input number 2 inputNumber_2 = 5 print("number 1 =",inputNumber_1,"\nnumber 2 =",inputNumber_2) # getting the exponential value of inputNumber_1 to the # power of inputNumber_2 (2^5) using ** operator expResult = inputNumber_1 ** inputNumber_2 # printing the resultant exponential value print("The resultant exponential result using **:", expResult)

Output

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

number 1 = 2
number 2 = 5
The resultant exponential result using **: 32

In the first example, we took two numbers and stored them in two separate variables. When we use the * operator between the two variables, the result is the multiplication of the two variables. When we use the ** operator between the two variables, it acts as the power function for the two variables, where the first number is the base and the second number is the exponent.

Using the * operator for repetition of the list

Python List also includes the * operator, which allows you to create a new list with the elements repeated the specified number of times.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Take a list with some random/dummy values.

  • Get the list multiplies n times(here 2) by multiplying the given list with the * operator

Example

The following program repeats the list the given number of times using the * operator

# input list inputList = [5, 6, 7] print("Input list =",inputList) # Repeating the input list 2 times using the * operator print(inputList * 2)

Output

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

Input list = [5, 6, 7]
[5, 6, 7, 5, 6, 7]

Here, we took a list of random values and multiplied it twice with the * operator, so that the output consists of the given list repeated twice.

Using the * operator to unpack a function.

This method is quite handy when printing data in a raw format (without any commas and brackets ). Many programmers attempt to remove commas and brackets by converging functions, thus this simple prefix asterisk can fix your problem in unpacking them.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list and give it some random values.

  • To print list elements separated by spaces without brackets[] first we convert the list to a string by passing the str and list as arguments to the map() function. It converts each element of the list to the string type and returns the resultant list of items. The join() function(join() is used to join elements of a sequence that are separated by a string separator) is used to convert the result list to a string.

  • Instead of the previous method, we can use the asterisk operator (*) to print the list separated by spaces.

Example

# input list inputList = ['TutorialsPoint', 'Python', 'Codes', 'hello', 5, 'everyone', 10, 5.3] print("Input list =",inputList) # Converting list elements to string using map() # Applying join() function to convert list to string # Without using the asterisk (*) operator print('Without Using * Operator :') print(' '.join(map(str,inputList))) # Using the asterisk (*) operator print('Using * operator : ') print (*inputList)

Output

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

Input list = ['TutorialsPoint', 'Python', 'Codes', 'hello', 5, 'everyone', 10, 5.3]
Without Using * Operator :
TutorialsPoint Python Codes hello 5 everyone 10 5.3
Using * operator :
TutorialsPoint Python Codes hello 5 everyone 10 5.3

Output remains the same using both ways.

Function Passing using an arbitrary number of positional arguments

In this method, the single asterisk( * ) is also used in *args.

It is used to pass a variable number of arguments to a function; it is most commonly used to pass a non−key parameter and a variable−length argument list.

It has various applications, one of which is described below. We create an addNumbers function that accepts any number of arguments and can add them all together using *args.

Example

# creating a function that accepts any number of arguments to it and # returns the sum of arguments passed to it def addNumbers(*args): # getting the sum of any numbers passed to the function return sum(args) # calling the addNumbers() function by passing some # random numbers as arguments to it to get the sum of those. print("Sum of 4,6,15,5 = ", addNumbers(4, 6, 15, 5))

Output

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

Sum of 4,6,15,5 = 30

Output remains the same using both ways.

Conclusion

In this article, we learned the correct name for the * operator and how it may be used in the Python programming language for a variety of applications. We also used an example to demonstrate the difference between the * and ** operators.

Updated on: 09-Nov-2022

122 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements