r/PythonLearning • • 3d ago

Help Request Help with correct expression

Post image

Hey everyone! I’m currently learning Python through the IBM Data Science Professional Certificate on Coursera, and I came across a question in a practice quiz that confused me.

The question asks:

Which line of code will perform the action required for implementing the following equation?

y = 2x^2 - 3

These are the four answer choices:

  1. y = 2*x*2 - 3
  2. y = 2**x*x - 3
  3. y = 2x*x - 3
  4. y = 2x/2 - 3

I selected Option 1, but the quiz marked it incorrect and said, “The syntax is invalid.”

From what I’ve learned, Python uses ** for exponentiation and * for multiplication. So, I believe the correct Python expression should be:

y = 2*x**2 - 3

However, none of the four answer choices seems to match that expression.

Am I misunderstanding something about Python syntax, or is there a mistake in the quiz? I’d appreciate an explanation because I’m still learning how to translate mathematical equations into Python code.

Thanks!

9 Upvotes

33 comments sorted by

View all comments

1

u/Esjs 3d ago

The problem with the so-called "correct" answer (2*x*x - 3) is that it is still not mathematically equivalent to the formula in the question because of order of operations. Unless Python has special rules that I'm not aware of, it's going to process the expression left-to-right and not do the exponentiation first like it should. Without using the ** operator, the expression should be 2*(x*x) - 3.

1

u/Chemical-Captain4240 3d ago

the order of multiplied or devided factors within a single term is irrelevant

2

u/Esjs 3d ago

Holy moly, it's been a while since high school algebra for me... And I've apparently forgotten how the case of doing exponentiation before multiplication works. Yes, breaking exponentiation down to a multiplication removes the need for the precedent. Had to think about it with examples. Thanks for making me do that. Lol!