>>> import math
>>> import numpy as np
>>> import scipy
>>> math.pi == np.pi == scipy.pi
True
So it doesn't matter, they are all the same value.
The only reason all three modules provide a pi value is so if you are using just one of the three modules, you can conveniently have access to pi without having to import another module. They're not providing different values for pi.
python - Is there a difference between scipy.pi, numpy.pi, or math.pi? - Stack Overflow
How to use pi in python?
I'm a beginner in Python and I write code for finding Pi to the N'th digit
realized during a recent project pythons math.pi is only acurate to 15 digits, I now understand the python dislike
Is math.pi the same as numpy.pi?
Can I use pi in Python without importing math?
How many digits of pi does Python store?
Videos
>>> import math
>>> import numpy as np
>>> import scipy
>>> math.pi == np.pi == scipy.pi
True
So it doesn't matter, they are all the same value.
The only reason all three modules provide a pi value is so if you are using just one of the three modules, you can conveniently have access to pi without having to import another module. They're not providing different values for pi.
One thing to note is that not all libraries will use the same meaning for pi, of course, so it never hurts to know what you're using. For example, the symbolic math library Sympy's representation of pi is not the same as math and numpy:
import math
import numpy
import scipy
import sympy
print(math.pi == numpy.pi)
> True
print(math.pi == scipy.pi)
> True
print(math.pi == sympy.pi)
> False
I wanted to code a calculator as I'm a beginner. Now its really simple and inefficient, I suppose, but I can't use pi or any other number such as i or e in it. Do you guys have any suggestions on how to simply solve this issue?
Code:
import math
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
def powerfunction(x, y):
return pow(x, y)
def logarithms(x, y):
return math.log(x, y)
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
print("5.Powerfunction")
print("6.Logarithm")
while True:
choice = input("Enter choice(1/2/3/4/5/6): ")
if choice in ('1', '2', '3', '4', '5', '6'):
try:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
except ValueError:
print("Invalid input. Please enter a number.")
continue
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
elif choice == '5':
print(num1, "^", num2, "=", powerfunction(num1, num2))
elif choice == '6':
print("log", num1, num2, "=", logarithms(num2, num1))
next_calculation = input("Let's do next calculation? (yes/no): ")
if next_calculation == "no":
break
else:
print("Invalid Input")
Here is my code but when I search on github Some other codes which is not similar to mine but does the same job, so the question does my way is right or not?
import math
Pi = math.pi
ans = int(input("Enter the number of decimals to calculate to: "))
print(f"{pi:.{ans+1}}")
This is the code I saw on github:
https://github.com/MrBlaise/learnpython/blob/master/Numbers/pi.py