-10

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

josh
  • 1,205
  • 4
  • 14
  • 14
  • 1
    What do you expect square brackets and curly braces to do? – senshin Jan 20 '14 at 19:51
  • Square brackets and curly brackets have their own definition in python (lists and sets/dictionaries respectively). – Ffisegydd Jan 20 '14 at 19:51
  • 3
    "I know why they don't work" So what is the quesiton? – tobias_k Jan 20 '14 at 19:51
  • The use square and curly brackets in mathematics are purely for visual contrast, something the Python interpreter does not need. – chepner Jan 20 '14 at 19:52
  • Why is everyone downvoting/voting to close this question? It's neither useless nor "unclear". – Makoto Jan 20 '14 at 19:55
  • What do you expect "math brackets" to do? Give an example. – tobias_k Jan 20 '14 at 19:55
  • 2
    There's no such thing as "math brackets", there's just notational conventions for the use of brackets in mathematics. E.g. a `[]` pair might be used in place of `()` in a deeply nested statement for better readability, but also in subscript with multidimensional indices. Brackets in math, just like in python, have multiple different uses that differ from context to context. – l4mpi Jan 20 '14 at 19:56
  • @senshin and tobias_k I want to know if there is a way in python to make a bracket (or text, or symbol) that would act like a square or curly bracket Sorry for the confusion, didn't explain myself clearly enough – josh Jan 20 '14 at 19:56
  • 1
    @Makoto -- it is pretty unclear -- there's actually no question stated and we're not sure what it is he wants "math brackets" to do that regular parentheses can't. – PurpleVermont Jan 20 '14 at 19:57
  • @Makoto I think I didn't explain myself clearly or a similar question was answered. Now I know that you simply cannot use so called "math brackets" in Python. Thanks for the help guys [closed] – josh Jan 20 '14 at 19:58
  • @Makoto it's extremely unclear (what are "math brackets" supposed to be? I can think of many different uses for each of them) and does not seem to be very useful as it is probably caused by OPs confusion of different concepts. – l4mpi Jan 20 '14 at 19:59
  • **Still waiting** for a solid definition of "Math Brackets" – Mike B Jan 21 '14 at 13:00
  • @MikeB Math brackets, as PurpeVermot brought out, "Humans use [] in writing out complex math expressions to make them more readable to other humans, but this isn't necessary.". I didn't realise that you **don't** need math brackets in pythin math brackets example: 10*[9+(2+3)] We do the math in () before and then [] and then {} – josh Jan 21 '14 at 13:21
  • Sorry if I can't explain myself clearly enough, I'm in 6th grade and English isn't my first language, but still now as I read my question, I see it's **unclear** – josh Jan 21 '14 at 13:26

2 Answers2

6

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.

PurpleVermont
  • 1,179
  • 4
  • 18
  • 46
3

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

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • I know what they do, but doesn't python have any ways to define "math" brackets? – josh Jan 20 '14 at 19:53
  • No. Use parentheses instead. – andars Jan 20 '14 at 19:54
  • If you're looking to do the basic sort of grouping (i.e. in situations where those sorts of brackets would make sense in math), then all you can do is nest parenthesized statements. If you're trying to do more fancy constructs, then you would be better off creating a domain object for it instead. – Makoto Jan 20 '14 at 19:54