Multiply each element in a tuple by a number in Python is an interesting topic. I will show you some ways you can do it, like using list comprehension, converting to a numpy array, multiplying two tuples using the zip function, and using math.prod() function. Please read the following article.
Multiply each element in a tuple by a number in Python
Use the list comprehension
Using list comprehension is one way you can multiply the elements of a tuple in Python.
- The tuple elements are traversed by the list comprehension and multiplied by 10. Finally, a tuple is returned by the tuple() function.
myTuple = (10, 10) # Use the list comprehension to multiply the elements of a tuple result = tuple([z * 10 for z in myTuple]) print('New tuple:', result)
New tuple: (100, 100)
Use the np.array function
np.array(value1, value2, ….)
- value: value to be formatted.
- Create a tuple.
- Convert tuple to numpy array.
- Multiply the elements of an array by a number.
- Finally, the tuple() function converts the numpy values to tuples.
import numpy as np myTuple = (10, 10) # The Tuple object is converted to a numpy array result = tuple(10*np.array(myTuple)) print('New tuple:', result)
New tuple: (100, 100)
Multiply the values of two tuples together
You can multiply each element of one tuple by the element of another using the zip() function.
- iterable: built-in iterables (like list, string, dict) or user-declared iterable.
The zip() function returns a zip object that is a list iterator, tuples combined with other elements from the iterator.
- Initialize two tuples.
- The two tuples are unpacked using the zip function and iterated by a list comprehension.
- Every element from the first tuple is a multiple of the corresponding element in the second tuple.
- Finally, the tuple() function converts it to a tuple.
myTuple1 = (1, 2, 3) myTuple2 = (4, 5, 6) # Use the zip function result = tuple(elem_1 * elem_2 for elem_1, elem_2 in zip(myTuple1, myTuple2)) print('New tuple:', result)
New tuple: (4, 10, 18)
Use the math.prod() function
Since Python version 3.8, the math.prod() function is included in the math module. You can use this function to calculate the product of the elements of an iterable object.
- iterable: Objects such as LIST, TUPLE, or SET need to produce the elements.
- start: Marks start to calculate elements. The parameter is not required.
The math.prod() function will return the product of the elements of the iterable object.
- Import the math module.
- Multiply the elements of the tuple together using math.prod() function.
import math myTuple = (10, 10) # Multiply the elements of the tuple using math.prod function result = math.prod(myTuple) print('Multiplication result:', result)
Multiplication result: 100
I want to share how you can multiply the elements of a tuple in Python, and I suggest you use the math.prod function to do this. If you like these topics, visit our website to read more. Thank you for reading!
Maybe you are interested:
- Add an element to a tuple in Python
- Get the max value in a Tuple in Python
- Split a tuple into multiple variables in Python
My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Programming Languages: C++, Python, Java