51

What do the 3 different brackets mean in Python programming?

  • [] - Normally used for dictionaries, list items
  • () - Used to identify parameters
  • {} - I don't know what this does

Can these brackets can be used for other purposes?

0

4 Answers 4

82

Square brackets: []

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'

Parentheses: () (AKA "round brackets")

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 expressions: (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))

Curly braces: {}

Dictionaries and sets, as well as in string formatting

  • Dicts: {}, {0: 10}, {i: i**2 for i in range(5)}
  • Sets: {0}, {i**2 for i in range(5)}
    • Except the empty set: set()
  • In string formatting to indicate replacement fields:
    • F-strings: f'{foobar}'
    • Format strings: '{}'.format(foobar)

Regular expressions

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.

Angle brackets: <>

Used when representing certain objects like functions, classes, and class instances if the class doesn't override __repr__(), for example:

>>> print <built-in function print> >>> zip <class 'zip'> >>> zip() <zip object at 0x7f95df5a7340> 

(Note that these aren't proper Unicode angle brackets, like ⟨⟩, but repurposed less-than and greater-than signs.)

Sign up to request clarification or add additional context in comments.

Comments

5

In addition to Maltysen's answer and for future readers: you can define the () and [] operators in a class, by defining the methods:

An example is numpy.mgrid[...]. In this way you can define it on your custom-made objects for any purpose you like.

Comments

2

() parentheses are used for order of operations, or order of evaluation, and are referred to as tuples. [] brackets are used for lists. List contents can be changed, unlike tuple content. {} are used to define a dictionary in a "list" called a literal.

2 Comments

"parentheses ... are referred to as tuples" -- Totally incorrect. Parentheses have a much broader purpose than just for tuples (e.g. calls like print()), and tuples can be defined without parentheses (e.g. t = 1, 2).
'{} are used to define a dictionary in a "list" called a literal.' -- No, literals are immutable by definition, therefore not including dicts, which are mutable. You might be thinking of displays, which includes "lists" and comprehensions.
-2

Tuple is immutable(order inside it can't be changed once created),and are enclosed in parenthesis,separated by ("," or ','). Tuple is used to store multiple items in a single variable.

Example:

thistupple("apple","banana","mango") print(thistupple) 

Output:

('apple', 'banana', 'cherry') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.