[]: Lists and indexing/lookup/slicing
- Lists:
[], [1, 2, 3], [i**2 for i in range(5)]
- Indexing:
'abc'[0] → 'a'
- Lookup:
{0: 10}[0] → 10
- Slicing:
'abc'[:2] → 'ab'
(): Tuples, order of operations, generator expressions, function calls and other syntax.
- Tuples:
(), (1, 2, 3)
- Although tuples can be created without parentheses:
t = 1, 2 → (1, 2)
- Order of operations:
(n-1)**2
- Generator expression:
(i**2 for i in range(5))
- Function or method calls:
print(), int(), range(5), '1 2'.split(' ')
- with a generator expression:
sum(i**2 for i in range(5))
{}: Dictionaries and sets, as well as string formatting
- Dicts:
{}, {0: 10}, {i: i**2 for i in range(5)}
- Sets:
{0}, {i**2 for i in range(5)}
- Inside f-strings and format strings, to indicate replacement fields:
f'{foobar}' and '{}'.format(foobar)
All of these brackets are also used in regex. Basically, [] are used for character classes, () for grouping, and {} for repetition. For details, see The Regular Expressions FAQ.