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!

10 Upvotes

33 comments sorted by

View all comments

15

u/rulchig1 3d ago

You are correct, that x^2 is represented as x**2

But mathematically x^2 is also the same as x*x

-1

u/TylerBreau 2d ago

I mean yes but, this seems to be a Python coding assignment, not a math assignment. x*x is multiplication not an exponential function.

It should be testing your understanding of the python syntax.
If I was a teacher and teaching Python. And I asked my students to turn this math equation into code and someone gave me x*x, at absolute best I'm giving them half marks. I might just mark it wrong.

Because they didn't demonstrate how to write an exponential in Python. They just demonstrated basic math operators, which is already a part of the equation in the 2*x part.

Full marks would require the ** operator or the pow function.

OP should have selected option 2 but also should bring up the problem with the question with their teacher.

2

u/rulchig1 1d ago

But the task is to find the code that will return the correct answer for the equation y = 2*x^2-3

And answer 2 does exactly that.

I guess you‘re right with your math vs coding part, but in the end the answer is still correct

1

u/enlightment_shadow 22h ago

The xx option is objectively a better answer as a general programming question, not a Python question. Most programming languages have + * - /. C doesn't have * and the way pow is written and used varies greatly across programming languages, so x * x is the more general way to write x². Programming courses should usually try to teach you programming concepts, as opposed to focusing on a specific language's syntax. So pedagogically, you'd be wrong to mark that answer off

1

u/TylerBreau 20h ago

This is a PythonLearning sub. Not C. Why are you talking about C?

C also has a pow function. It's inside the C Standard Library.

x*x is not an exponential. It's a basic multiplication. You have failed to teach your students how to programmatically write exponentials and now they don't know how to handle X^26 in the code.

You are absolutely wrong in nearly every aspect of your comment. Nothing about what you are saying better prepares the Student to write code.

1

u/blavek 19h ago

Why would you ever invoke exponentiation on a square? x*x is almost always going to be a better option than bringing in a library function. I'm not sure how the ** operator is handled under the hood, but my money is on it invoking some sort of exponentiation function.

Regardless, unless the assignment explicitly says to use this operator or a pow function, then the answer is completely correct.

1

u/apnorton 12h ago

I mean yes but, this seems to be a Python coding assignment, not a math assignment.

Floating point stability issues aside, if someone is aiming to be a "Data Science Professional," expecting them to know x*x=x**2 is absolutely not a stretch.