Math brackets in python?

22.7k views Asked by At

Now I'm talking about MATH brackets, not python brackets, I know that parentheses () work like in maths, ex:

i = 5*(2+2)
print (i)
#output = 20

But square brackets [] and curly brackets {} don't work... (I know why they don't work)

Thank you, Using Python 3.2.2

2

There are 2 answers

0
PurpleVermont On BEST ANSWER

You don't need "math" brackets -- just use nested parentheses. Humans use [] in writing out complex math expressions to make them more readable to other humans, but this isn't necessary. They don't mean anything different than regular parentheses. So, when writing code, just stick to the parentheses.

3
Makoto On

They don't work as grouping constructs - only parentheses will do that.

Square brackets define a list.

Curly brackets define either a set or a dictionary (if the elements appear as key: value).

Further to this, the extra level of clarity when dealing with multiple nestings is unnecessary, as most good IDEs will let you know when the parentheses count is imbalanced (and, you will also notice when the count is imbalanced from repetition).