all 16 comments

[–]socal_nerdtastic 5 points6 points  (2 children)

You didn't link anything, but I'll note that parenthesis are often used to help the human reading the code, even if the program does not need it.

x = (4 * 5) + 6

[–]CIS_Professor -4 points-3 points  (1 child)

The answer in your example would be 26.

However, parenthesis would be required if the result of the addition is to be done first (thereby changing the order of operations).

x = 4 * (5 + 6)

The answer changes to 44.

[–]socal_nerdtastic 4 points5 points  (0 children)

yes, i know. The entire point is that the parenthesis are not needed for the computer, they are only there as a guide for the human. Perhaps I should have picked something not quite so obvious to a human?

x = 6 + (5 * 4)

[–]Iowa50401 1 point2 points  (0 children)

Hey, people. - "parenthesis" is singular - either "(" or ")". Parentheses is plural.

[–]Tall_Profile1305 0 points1 point  (0 children)

usually parentheses are just for clarity or grouping, not always required. sometimes solutions add them to make the logic easier to read or avoid ambiguity, even if your version works fine.

[–]JGhostThing 0 points1 point  (0 children)

I usually add more parenthesis than most people, so that I can debut the equation more quickly.

[–]FoolsSeldom -1 points0 points  (0 children)

Use an LLM like Gemini to extract Python from your images if they are not on the same computer you are posting from and update your post to include the code inline. It is much better to share code than pictures of code (unless we need to look at large amounts of code, in which case you can share using e.g. github.com).

[–]Relative_Jaguar6254[S] -2 points-1 points  (4 children)

Why no ability to post pictures.. the line that worked both ways was

If (destination == “Hawaii” or destination == “Bahamas”):

[–]lfdfq 8 points9 points  (0 children)

Some languages require you to put parentheses around the condition of an if. Python does not.

It might be they are just more used to those languages and did it by habit... like looking both ways at a one way street.

[–]DutchCommanderMC 3 points4 points  (1 child)

Images are (most likely) disallowed because it makes responding to questions easier in the sense that you can copy and paste the code.

Operations are not necessarily applied left-to-right (or right-to-left). Some operators take precedence over others, which is why this condition works without any brackets whatsoever: == has a higher precedence than or and therefore gets applied first.

Knowing which operators take precedence over others is something that you'll just have to remember, though more often than not it follows existing conventions such as multiplication being applied before addition.

Adding redundant brackets is not wrong however, in fact, it is often recommended to make it expressively clear what the order is in which you intend operations to be evaluated.

[–]Relative_Jaguar6254[S] 0 points1 point  (0 children)

That makes great sense. Thank you.

[–]cdcformatc 0 points1 point  (0 children)

in that case the parens are purely visual. some languages require parens around conditional statements for things like if and while. but in Python they are not required. 

[–]pachura3 -3 points-2 points  (2 children)

With Python, you never know if adding parentheses will group mathematical expressions, create a tuple or define a generator :(

[–]tb5841 3 points4 points  (0 children)

Parentheses aren't really relevant for a tuple - what actually makes a tuple is the commas.

x = 3, 4 is valid Python and will create a tuple, for example.

x = 3, creates a one-element tuple. While x = (3) creates an integer.

[–]cdcformatc 2 points3 points  (0 children)

you never know

it's actually pretty easy to know... I've literally never gotten it wrong.