To get the golden ratio constant phi in your Python code, the easiest way is with the equation one plus the square root of five all divided by two.

phi = (1 + 5 ** 0.5) / 2

print(phi)

#Output:
1.618033988749895

The golden ratio constant phi is also available in the scipy module.

import scipy.constants

phi = scipy.constants.golden

print(phi)

#Output:
1.618033988749895

In the world of mathematics, there are many interesting constants and special numbers which are important in many applications. One such number is phi, or the golden ratio.

While the Python math module has many constants available to us, it does not have phi.

With that said, we can easily get the golden ratio in Python with the equation one plus the square root of five all divided by two.

Below is a piece of code which allows you to get the golden ratio constant phi in Python.

phi = (1 + 5 ** 0.5) / 2

print(phi)

#Output:
1.618033988749895

Get Golden Ratio from scipy Module Constants in Python

Another way you can get the golden ratio in Python is with the scipy module. The scipy module has many different pre-defined constants you can use in your Python code.

Below is how you can get the golden ratio constant using the scipy module in Python.

import scipy.constants

phi = scipy.constants.golden

print(phi)

#Output:
1.618033988749895

Hopefully this article has been useful for you to be able to get the golden ratio in your Python code.

Categorized in:

Python,

Last Update: February 26, 2024