
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How does the * operator work on a tuple in Python?
In Python, a tuple is similar to a list. The distinction between the two is that once a tuple is assigned, its elements cannot be changed, whereas list items can i.e, the tuples are immutable while the lists are mutable.
Repetition Operator(*):
A repetition operator is supported by sequence datatypes (both mutable and immutable). * The repetition operator * creates several copies of that object and joins them together. When used with an integer, * performs multiplication, but when used with a list, tuple, or strings, it performs repetition.
In this article, we will show you how the * operator works on a tuple in python. Below are the different examples to understand how * works on a python tuple −
Repetition Operator(*) on Tuple Elements
Repetition Operator(*) on Nested Tuple Items
Using the * operator to unpack a function.
When the Repetition Value is given 0
Method 1: Repetition Operator(*) on Tuple Elements
When you multiply a tuple by any integer, you get another tuple with all the elements from the input tuple repeated x times. For example, inputTuple*4 indicates that the items of tuple input tuple will be repeated 4 times.
Example
The following program repeats the tuple the given number of times using the * operator−
# input tuple inputTuple = (3, 4, 2) # repeating the input tuple 4 times and printing the result tuple print(inputTuple*4)
Output
On executing, the above program will generate the following output −
(3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2)
Here, we took a tuple of random values and multiplied it four times with the * operator, so that the output consists of the given tuple repeated four times.
Method 2: Repetition Operator(*) on Nested Tuple Items
Example
# input nested tuple nestedTuple =((4,9,8),(3,6)) # repeating the input nested tuple 3 times and printing the result tuple print(nestedTuple*3)
Output
On executing, the above program will generate the following output −
((4, 9, 8), (3, 6), (4, 9, 8), (3, 6), (4, 9, 8), (3, 6))
We used a nested tuple of random values in this case. Then we multiplied the tuple three times, and the output tuple contains the given nested tuple multiplied three times. Instead of multiplying each element of the tuple, we can see that it multiplies the nested tuple as a whole three times.
Method 3: When Repetition Value is given 0
When a value less than or equal to 0 is provided, an empty sequence of the same type is returned.
Example 1
The following program returns an empty tuple when the input tuple is multiplied by 0 −
# input tuple inputTuple =(4,9,8) # returning an empty tuple when repetition Value is given 0 print(inputTuple*0)
Output
On executing, the above program will generate the following output −
()
We used 0 as the repetition value here, so we get an empty tuple () because something multiplied by 0 equals 0 (empty)
Example 2
The following program returns an empty tuple when input is multiplied with any number less than 0 −
# input tuple inputTuple =(4,9,8) # returning an empty tuple when repetition Value is less than 0 print(inputTuple*-32)
Output
On executing, the above program will generate the following output −
()
Because the * operator only accepts positive values, we get an empty tuple when we pass -32 as the repetition value. If there are any negative values, it cannot multiply them and thus returns an empty tuple.
Method 4: Using a positional argument to unpack a function.
The star(*) operator unpacks the sequence/collection into positional arguments. So, instead of indexing each element separately, you could just use the * operator if you have a tuple and want to pass the elements of that tuple as arguments for each position as they are in the tuple.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task −
Create a variable to store the input tuple and give it some random values.
To print tuple elements separated by spaces without brackets[] first we convert the tuple to a string by passing the str and tuple as arguments to the map() function. It converts each element of the tuple to the string type and returns the resultant tuple 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 tuple to a string.
Instead of the previous method, we can use the asterisk operator (*) to print the tuple separated by spaces.
Example
The following program prints the tuple elements with spaces −
# input tuple inputTuple = ('TutorialsPoint', 'Python', 'Codes', False, 3.4, 5, 'hello', 'everyone') # Converting tuple elements to string using map() # Applying the join() function to convert a tuple to a string # Without using the asterisk (*) operator print('Without Using * Operator :') print(' '.join(map(str,inputTuple))) # Using the asterisk (*) operator print('Using * operator : ') print (*inputTuple)
Output
On executing, the above program will generate the following output −
Without Using * Operator : TutorialsPoint Python Codes False 3.4 5 hello everyone Using * operator : TutorialsPoint Python Codes False 3.4 5 hello everyone
Conclusion
This article covered every case in which the * repetition operator on a tuple was used. We also discussed how it will behave in different scenarios. We learned how to print tuple elements separated by spaces using the * operator. This can be useful in a variety of situations; such as interviews where you want to return a tuple separated by spaces or programming contests where you want to save time instead of writing multiple functions. We also learned how the * operator behaves on nested tuples, and how it multiplies the nested tuple multiple times rather than multiplying every element in the nested tuple multiple times.
- Related Articles
- How does the repetition operator work on a tuple in Python?
- How does the del operator work on a tuple in Python?
- How does concatenation operator work on tuple in Python?
- How does the 'in' operator work on a tuple in Python?
- How does * operator work on list in Python?
- How does in operator work on list in Python?
- How does concatenation operator work on list in Python?
- How does repetition operator work on list in Python?
- How does tuple comparison work in Python?
- How does the Comma Operator work in C++
- How does tuple destructuring work in TypeScript?
- JavaScript : Why does % operator work on strings? - (Type Coercion)
- How does comparison operator work with date values in MySQL?
- How does == operator works in Python 3?
- How does underscore "_" work in Python files?
