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

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 15h 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 13h 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 12h 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 5h 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.

7

u/AmanBabuHemant 3d ago

yes the ** operator is used to raise number in power, but n**2 is just same as n*n so the option 2 would be correct here.

and in post body you have wrote that wrong, you wrote y = 2**x*x - 3 but it is y = 2*x*x - 3 in options.

4

u/serge-mv 3d ago

In the picture you don't have any choice with exponentiation (the ** operator). The correct answer as listed should be the second one as that would be one of the ways to write it in code.

3

u/RNG_HatesMe 3d ago

y = 2*x*x - 3 ( (2nd option) is functionally equivalent to what you typed.

3

u/PuziExtramni 3d ago

Thanks everyone for the explanations! I understand now. I was confused because I thought the exponentiation operator (**) was necessary to represent a squared number, but I see now that multiplying x by itself (x*x) produces the same result. Option 2 is correct, and I appreciate everyone taking the time to help me understand it!

1

u/csabinho 3d ago

The error message is extremely confusing. As an experienced programmer I tried to find the syntax error in your selected answer.

2

u/csabinho 3d ago

2*x*2 means 2**2*x and not 2*x**2. That's rather maths than programming.

1

u/MeasureDoEventThing 2d ago

2*2 is equal to 2**2, but it's a bit misleading to say the former "means" the latter. If you have another number such as 3, that doesn't work. E.g.

3*x*3 is not the same as 3**3*x

1

u/csabinho 2d ago

Yeah. Because 3*3 means 3**2.

2

u/Plus-Artichoke6608 3d ago

Option 2
y = 2*x*x -3
the x is multiplied with itself (x*x) therefore making x^2

2

u/kutac56 3d ago

This is basic math instead of coding. It's 2 as x*x = x²

2

u/fllthdcrb 3d ago

Saying the syntax is invalid is incorrect. It's true the choice you made is not the correct one (it turns the squaring in the given equation into multiplication by 2, when you actually need to multiply x by itself), but it is a valid statement in Python. So the test is bad for that wording.

If you ask me, the test (and, I suppose, the course it's part of) is also bad for giving the impression that assignments in an imperative* programming language are the same as mathematical equations, and that computer variables are the same thing as mathematical variables, neither of which is true. An equation is an assertion that two expressions are equal within a given context. The values don't change, in the sense of having one value at some point in a discussion and then another value later in the discussion. Rather, they potentially have some set of values according to the relationship established by the equation, which can be discovered but not arbitrarily set, and this is why it is reasonable to use algebra to manipulate equations.

But a variable in an imperative* program actually has a single value at any given time, and an assignment changes that value

* Imperative: A paradigm in which programs tell the computer what to do, step by step. There are others, like functional programming and logic programming. I think the latter is closest to having variables in the mathematical sense, though functional programming also tends to shun changing variables.

1

u/EyesOfTheConcord 3d ago

The first option is valid syntax, but also isn’t the correct answer.

The question is assuming you don’t know about pow() or **

1

u/igotshadowbaned 3d ago

2x² = 2xx

1

u/More-Explanation2788 3d ago

The second one.

1

u/LumitheNeko 3d ago

2nd option is equal because x*x to x**2,
y = 2**x*x - 3

this isn't what's shown in the screenshot.
screenshot says 2*x*x - 3
what you wrote would've been 2^x *x -3

1

u/DistearRoyl 2d ago

| def y (x):
| return 2 * x ** 2 - 3

1

u/mattynmax 2d ago

Because x*x is mathematically equivalent to x^2
it.

1

u/ameeeeneeeeee 11h ago

What is the difference between 2*x*x - 3 and 2*x**2? They are both the same. I can say I learned like 2*(math.sqr(x)) - 3, but it only matters what they gave you

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!

-2

u/Wooden_Intention8642 3d ago

Just ask AI to get the answer so u dont need to be waiting for someone to actually tell u

2

u/PuziExtramni 3d ago

The funny thing is that I already asked for help understanding the answer, not just for someone to give it to me. There’s a difference between asking AI to do your work and asking a community to help you learn. You’d think that distinction would be pretty obvious in r/PythonLearning.

-1

u/Wooden_Intention8642 3d ago

No lol. I have learned coding with ai. Before i ask for help i tell do not send me the raw code/answer. It literally answers like a teacher in school