HOWTO · Python
Python Determinant
This tutorial demonstrates finding the determinant of a square matrix in Python.
On this page
A determinant is a scalar associated with a square matrix, meaning a matrix with the same number of rows and columns. For the 2×2 matrix [[7, 5], [2, 4]], use the formula (7 × 4) − (5 × 2) to get a determinant of 18. In Python, NumPy is a convenient choice for numerical arrays, while SymPy can keep integer and symbolic calculations exact. The examples below use both libraries on the same 2×2 and 3×3 matrices so you can choose the approach that fits your calculation.
Use numpy.linalg.det() to calculate a square matrix determinant in Python
Install NumPy in the Python environment where you plan to run the program by using python -m pip install numpy. This command installs the package for the interpreter invoked as python; in a notebook or virtual environment, make sure that command targets the same environment that runs your code. The complete example imports NumPy, creates two square arrays, and calls numpy.linalg.det() for each one. This API is a function that takes an array-like matrix as its input. The example is provided as runnable code, but no runtime output was captured for it.
import numpy as np
matrix_2x2 = np.array([[7, 5], [2, 4]])
matrix_3x3 = np.array([[7, 5, 3], [2, 4, 1], [5, 8, 6]])
print("2x2 determinant:", np.linalg.det(matrix_2x2))
print("3x3 determinant:", np.linalg.det(matrix_3x3))
For these entries, the exact mathematical results are 18 and 65. NumPy computes with floating-point arithmetic, so the printed representation can show a small precision difference from an exact integer. That is expected for numerical computation and does not by itself mean the matrix or calculation is wrong. Keep the returned precision when later operations depend on it. Round for display only when the tolerance of your application supports doing so; rounding an intermediate result can affect later calculations. If your input values are measurements or approximations, the determinant is also an approximation, regardless of how many digits are printed.
NumPy is useful when your matrices are already arrays or when you are working with numerical linear algebra. The function accepts square matrices of different sizes, and NumPy can operate on batches of matrices as well. This article keeps the example to a single 2×2 and a single 3×3 array to make the input and result easy to compare. A determinant is defined only for square matrices, so check that each row and column count agrees before choosing this operation.
Use SymPy to calculate a square matrix determinant in Python
Install SymPy in the Python environment for this example with python -m pip install sympy. As with NumPy, use the interpreter and environment that will execute the code. SymPy represents the input as a Matrix object and calculates the result through its det() method. The example repeats the same entries as the NumPy program, which makes the library behavior straightforward to compare. The example is provided as runnable code, but no runtime output was captured for it.
import sympy as sp
matrix_2x2 = sp.Matrix([[7, 5], [2, 4]])
matrix_3x3 = sp.Matrix([[7, 5, 3], [2, 4, 1], [5, 8, 6]])
print("2x2 determinant:", matrix_2x2.det())
print("3x3 determinant:", matrix_3x3.det())
With integer entries, SymPy returns the exact integer values for these examples instead of a floating-point approximation. Matrix.det() is the method used to obtain the determinant from a SymPy matrix; the lowercase sympy spelling belongs to the Python import, while the library name in prose is SymPy. You can also build matrices from symbolic variables and preserve an expression in the result, which is useful when deriving a formula before substituting particular values.
Choose SymPy when exact arithmetic or symbolic expressions are important, and choose NumPy for numerical arrays and floating-point linear algebra. Both examples use small matrices whose exact results are 18 and 65, so they also provide a simple way to check that your input entries are in the intended row order. These libraries offer different calculation models: SymPy preserves exact forms when possible, whereas NumPy is designed for numerical work. Select based on how the result will be used, and retain enough precision for any later calculations.