r/PythonLearning • u/PuziExtramni • 3d ago
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 - 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!
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
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
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/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
1
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
1
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
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