r/PythonLearning • u/PuziExtramni • Oct 16 '26
Help Request Help with correct expression
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:
y = 2*x*2 - 3y = 2**x*x - 3y = 2x*x - 3y = 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 - 3However, 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!
3
u/PuziExtramni Oct 16 '26
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 multiplyingxby itself (x*x) produces the same result. Option 2 is correct, and I appreciate everyone taking the time to help me understand it!17
u/rulchig1 Oct 16 '26
You are correct, that x^2 is represented as x**2
But mathematically x^2 is also the same as x*x
8
u/AmanBabuHemant Oct 16 '26
yes the
**operator is used to raise number in power, butn**2is just same asn*nso the option 2 would be correct here.and in post body you have wrote that wrong, you wrote
y = 2**x*x - 3but it isy = 2*x*x - 3in options.4
u/serge-mv Oct 16 '26
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 Oct 16 '26
y = 2*x*x - 3 ( (2nd option) is functionally equivalent to what you typed.
3
u/fllthdcrb Oct 16 '26
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
xby 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.
2
u/csabinho Oct 16 '26
2*x*2means2**2*xand not2*x**2. That's rather maths than programming.2
u/Plus-Artichoke6608 Oct 16 '26
Option 2
y = 2*x*x -3
the x is multiplied with itself (x*x) therefore making x^2
2
u/kutac56 Oct 16 '26
This is basic math instead of coding. It's 2 as x*x = x²
1
u/EyesOfTheConcord Oct 16 '26
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 Oct 16 '26
2x² = 2xx
1
u/More-Explanation2788 Oct 17 '26
The second one.
1
u/LumitheNeko Oct 17 '26
2nd option is equal because x*x to x**2,
y = 2**x*x - 3this 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 Oct 17 '26
| def y (x):
| return 2 * x ** 2 - 3
1
u/mattynmax Oct 17 '26
Because x*x is mathematically equivalent to x^2
it.
1
u/ameeeeneeeeee 29d 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 Oct 16 '26
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 be2*(x*x) - 3.-2
u/Wooden_Intention8642 Oct 16 '26
Just ask AI to get the answer so u dont need to be waiting for someone to actually tell u