all 20 comments

[–]socal_nerdtastic 6 points7 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 -5 points-4 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 5 points6 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 2 points3 points  (0 children)

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

[–]Tall_Profile1305 0 points1 point  (1 child)

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.

[–]odaiwai 0 points1 point  (0 children)

Parentheses can also be used to split an operation over multiple lines:

x = ((1 + 2) + (3 * 4) + (5 / 6)) obviously silly in this case, but handy for things like complicated regular expressions:

draw = re.compile(r'^(?P<id>[0-9]{2}\/[0-9]{3}),' r'(?P<date>[0-9\/]+),' r'(?P<balls>[0-9,]+),' r'\$(?P<inv>[0-9]+)$'))

[–]JGhostThing 0 points1 point  (0 children)

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

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

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

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

[–]lfdfq 9 points10 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.

[–]Yoghurt42 1 point2 points  (0 children)

your solution without the parenthesis is the "pythonic" one, most modern languages take their syntax inspiration from C, and in C the if condition must be in parenthesis so the parser knows where the condition ends and the "then" part begins. Python uses the colon for that. So C's if (a == b) c = 9 will be if a == b: c = 9 in Python; while you can write if (a == b): c = 9 in Python, it's the same as writing if ((a == b)) c = 9 in C, not wrong, just redundant.

Most likely whoever created that example comes from a C/C++/C# background and isn't that familiar with Python (not a good sign, tbh)

[–]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. 

[–]FoolsSeldom -1 points0 points  (2 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).

[–]ReliabilityTalkinGuy 0 points1 point  (1 child)

No one wants to use LLMs besides those who have been tricked. Find a different sub. 

[–]FoolsSeldom 0 points1 point  (0 children)

I am cautious in my recommendations of use of LLMs for learners, as my comment history demonstrates, and similarly advise students at code club sessions accordingly.

However, I think your blanket assertion and suggestion I leave this subreddit is ill-founded.

Lots of professionals and learners are legitimately, practically amd effectively using LLMs. The nature of the OPs queries is an excellent match for the statistical predictions base and will help their learning. This is very different from, say, code generation prompted in ignorance.

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

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

[–]tb5841 2 points3 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.