In this tutorial, you will learn how to perform multiplication in Python. Multiplication is a crucial operation in programming and mathematics, and Python provides several ways to multiply numbers, lists, and other data types.
By the end of this tutorial, you’ll be familiar with the various methods to implement multiplication in Python.
Step 1: Multiplying Numbers
Multiplying numbers in Python is easy and straightforward, using the asterisk (*) operator. Here’s an example demonstrating this:
1 2 3 4 |
number1 = 4 number2 = 3 result = number1 * number2 print("Multiplication Result:", result) |
This code will output:
Multiplication Result: 12
You can also multiply floating-point numbers in the same way:
1 2 3 4 |
float_number1 = 4.3 float_number2 = 2.1 result = float_number1 * float_number2 print("Float Multiplication Result:", result) |
The output will be:
Float Multiplication Result: 9.03
Step 2: Repeating Strings
You can use the asterisk (*) operator to repeat a string multiple times. The code snippet below shows you how to do this:
1 2 3 4 |
string = "Python " repeat_times = 3 repeated_string = string * repeat_times print("Repeated String:", repeated_string) |
The output of this code will be:
Repeated String: Python Python Python
Step 3: Multiplying Lists
In Python, you can also repeat elements of a list using the asterisk (*) operator. This is useful when you want to create a new list with multiple copies of the elements of an existing list:
1 2 3 4 |
list1 = [1, 2, 3] repeat_times = 3 multiplied_list = list1 * repeat_times print("Multiplied List:", multiplied_list) |
The output will be:
Multiplied List: [1, 2, 3, 1, 2, 3, 1, 2, 3]
Step 4: Multiplying Arrays – NumPy
If you need to perform element-wise multiplication of arrays, you can use NumPy – a widely-used Python library for numerical operations. Before using NumPy, make sure to install it using pip:
1 |
pip install numpy |
Now, you can perform element-wise multiplication of two NumPy arrays:
1 2 3 4 5 6 |
import numpy as np array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) result = array1 * array2 print("Element-wise multiplied arrays:", result) |
Here’s the output:
Element-wise multiplied arrays: [ 4 10 18]
Step 5: Matrix Multiplication
To perform matrix multiplication in Python, you can use the matmul() function from the NumPy library. First, create two matrices using NumPy arrays, and then apply the np.matmul()
function.
1 2 3 4 5 6 |
import numpy as np matrix1 = np.array([[1, 2], [3, 4]]) matrix2 = np.array([[5, 6], [7, 8]]) result = np.matmul(matrix1, matrix2) print("Matrix multiplication result:\n", result) |
The output will be:
Matrix multiplication result: [[19 22] [43 50]]
Full code
Below is the full Python code for this tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
number1 = 4 number2 = 3 result = number1 * number2 print("Multiplication Result:", result) float_number1 = 4.3 float_number2 = 2.1 result = float_number1 * float_number2 print("Float Multiplication Result:", result) string = "Python " repeat_times = 3 repeated_string = string * repeat_times print("Repeated String:", repeated_string) list1 = [1, 2, 3] repeat_times = 3 multiplied_list = list1 * repeat_times print("Multiplied List:", multiplied_list) import numpy as np array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) result = array1 * array2 print("Element-wise multiplied arrays:", result) matrix1 = np.array([[1, 2], [3, 4]]) matrix2 = np.array([[5, 6], [7, 8]]) result = np.matmul(matrix1, matrix2) print("Matrix multiplication result:\n", result) |
Conclusion
In this tutorial, you learned various methods for applying multiplication in Python, including multiplying numbers, repeating strings, and lists, as well as performing element-wise multiplication and matrix multiplication using the NumPy library. With this knowledge, you are now equipped to work with multiplication in Python effectively.