Different values ​​of brackets in python

I am curious what three different brackets mean in python programming? Not sure I'm right about this, but please correct me if I am wrong.

[] - # Normally used for dictionaries, list items () - # Used to identify params {} - # I have no idea what this does... 

Or, if these brackets can be used for other purposes, any advice is appreciated! Thanks!

+18
source share
3 answers
  • [] : used to define mutable data types: lists, lists, and indexing / searching / slicing.
  • () : Define tuples, order of operations, generator expressions, function calls, and other syntax.
  • {} : Two types of hash tables - dictionaries and collections.
+27
source
Parentheses

() are used for the order of operations or the order of valuation and are called tuples. [] brackets are used for lists. The contents of the list can be changed, unlike the contents of the tuple. {} are used to define a dictionary in a "list" called a literal.

+2
source

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

An example is numpy.mgrid[...] . That way, you can define it on your custom objects for whatever purpose you like.

0
source

All Articles