
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Star Operator in Python
The asterisk (*) operator in Python has more than one meaning attached to it. We can use it as a multiplication operator, a repetition operator, for unpacking the iterables, and as a function *args.
A single asterisk, as used in a function declaration, allows a variable number of arguments to be passed from the calling environment. Inside the function, it behaves as a tuple.
As the multiplication operator
Generally, the start (*) operator is used for multiplication purposes. For numeric data, the asterisk (*) is used as a multiplication operator. Let's take an example and see how the star operator works on numeric operands.
Example
In the following example, we have performed a multiplication operation between 100 and 29, which results in 2900 -
a = 100 b = 29 result = a * b print("Output: ", result)
Following is the output of the above code -
Output: 2900
Used as an Exponential operator
In Python, the ** operator is used for exponentiation, which means raising a number to the power of another. The syntax is x**y, which means x is raised to the power y.
Example
Here, we have found the exponential value of 9 to the power of 3 -
a=9 #operand 1 b=3 #operand 2 result=a**b #exponential value print("Exponential value of 9 power 3 is",result)
Following is the output of the above code -
Exponential value of 9 power 3 is 729
As the repetition operator
For sequences like lists, strings and tuples, the star operator is used as a repetition operator. It repeats the sequence a specified number of times.
Example
In the following example, we repeat a string, a list, and a tuple using the asterisk operator followed by an integer value -
s = "Python" result = s * 3 print("Output for string sequence: ", result) l = [1, 'a', 3] l_result = l * 2 print("Output for list sequence: ", l_result) t = (1, 2, 3) t_result = t * 4 print("Output for tuple sequence: ", t_result)
Following is the output of the above code -
Output for string sequence: PythonPythonPython Output for list sequence: [1, 'a', 3, 1, 'a', 3] Output for tuple sequence: (1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3)
To unpack the iterables
When calling a function, the asterisk operator can be used as a parameter to unpack the argument iterables.
Example
In the following example, we unpack a list into separate arguments for the function using the asterisk operator -
def foo(a, b, c): return(a, b, c) l = [1, 2, 3] print(' ',foo(*l))
Following is the output of the above code -
(1, 2, 3)
Example
The list iterable is unpacked by using the start (*) operator. It is also the same while sending iterables like tuples and dictionaries as parameters.
def foo(a, b, c): return(a, b, c) t = (1, 4, 6) print('Output tuple unpacking: ',foo(*t)) d = {'a': 10, 'b': 20, 'c': 30} print('Output dictionary unpacking: ',foo(*d))
Following is the output of the above code -
Output tuple unpacking: (1, 4, 6) Output dictionary unpacking: ('a', 'b', 'c')
The iterables, tuples, and dictionaries are also unpacked by using the asterisk (*) operator while calling a function.
To create function *args (positional arguments):
While defining a function, the single asterisk (*) is used to pass a variable number of arguments to a function. It is nothing but creating a function with non-keyword arguments.
Example
The above example is defined to take any number of values and is able to add them all together using *args.
def add_values(*args): return sum(args) print(add_values(1, 3, 4, 2)) print(add_values(10, 2, 4, 5, 6, 7, 8, 10))
Following is the output of the above code -
10 52