質問

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