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

有帮助吗?

解决方案

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.

其他提示

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).

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top