-1

I have a list of polynomial equation constants and I want to write this equation with for loop inside print() function. What is the shortest way (if one-line code exist would be more appropriate)?

EDIT: (reason: adding a sample) The code is below:

cnst=list()
degree=int(input("Enter degree of your polynomial: "))
#degree=int(input("Enter degree of your polynomial: "))
# must use degree+1 to include constant term
for i in range(degree+1):
    print(i)
    print("Enter constant for x^" + str(degree-i) + ": ", end='')
    cnst.append(float(input()))
print (cnst)
print("\nFunction created: ")
#print equation code here <<--
Mir
  • 30
  • 8
user70
  • 597
  • 2
  • 7
  • 24

2 Answers2

1

Welcome to the world of NumPy our Pythonic mathematical lord and savior.

You can add, subtract, calculate derivatives, etc. The np.polynomial module has a lot of functions. numpy.polynomial is now the recommended class when dealing with polynomials. Check out the documentation for more.

from numpy.polynomial import Polynomial

p1 = Polynomial([1,5,2])

p2 = Polynomial([6,1,4,3])

print(p1 * p2)

Polynomial([ 6., 31., 21., 25., 23., 6.], [-1., 1.], [-1., 1.])

Noah M.
  • 310
  • 1
  • 8
0

I could not understand correctly, but I think this code can help you

for x in [-1, 0, 2, 3.4]:
print(x, p(x))
nima amr
  • 603
  • 1
  • 6
  • 13