So what would the computer do if you input “6**6” vs. “6 ** 6”?
Basically, what is the difference of inputting the spaces vs. omitting them?
Thank you!
So what would the computer do if you input “6**6” vs. “6 ** 6”?
Basically, what is the difference of inputting the spaces vs. omitting them?
Thank you!
I don’t think there’s a difference at all.
But it’s better to use spaces as if you are writing in a paper to make everything look neat and organized.
Exactly.
To make code readable and mistakes easy to spot, always use whitespace around operators, like the second example. There is no mistaking what operation is taking place, here.
Thanks. This and other beast practices should be part of each lesson, to build good habits, in my opinion.
I agree,
Note to the content creator Instructors
At end of each module/ unite there should be a lesson called best practices to teach how to write codes efficiently and in an understandably way as well … (like with functions, less use of variables, spaces inside math operation, single vs double quotes with print, commenting etc.)
print( 2 ** 2 ** 3)
From this I’m getting 256 instead of 64
why is it so?
Welcome to the forums!
Rather than 2 ** 2
is 4, then 4 ** 3
is 64, the following happens. You print 2 to the power of 2^3. So the second part of the expression, 2 ** 3
, is itself the exponent where the first 2
is the base. In the end, you are calculating 2 ** 8
(think of it as 2 ** (2 ** 3)
.
This is because the expression on the right of the **
operator is evaluated before being applied to the base (left) of the operator. Find out more here.
Because association is right to left with many operations in Python, we need to manually enforce the exponent laws by grouping so operations are ordered correctly.
(a ** m) ** n => a ** (m * n)
I agree. It looks much better to put spaces in between them just like on a piece of paper.
Hahaha I really like this part
Being neat and organized is something that is always helpful!
Yes, thats good idea
Giving proper space between the expression is very important. It improves code readability and efficiency of code maintainability.
2 ** 2 ** 3 giving 64 if i apply math as a ** (m * n), where as it should be 256. Am i missing something?
2 to the 8th is 256. 2 to the 6th is 64.
You’re right that a ** b ** c is a ** (b * c).
What are some best practices for improving code readability and maintainability, and how can developers ensure that their code is easy to understand and modify by others in a team environment?
Are you reading the posts others made?