When do we use : = () []


I know this might be a very broad question, but what is the difference between statement() and statement[] ??
Also When do we use "smth" = 9 or "smth" : 9.

I know it might not be a proper question but what is the word in your mind when u translating the [] and () or even "." .


The main purposes for these is for lists, arrays, indexing, storing mixed items i.e (strings, integers, and floats) []

But this has only a few main uses which is storing parameters, nesting strings, parts of a for loop, calling functions with parameters, and passing multiple arguments.

I can't explain everything in depth but that is a basic outline of their capabilities. :grin:



[] () .
Are all operators, what they do is defined by the values that they are used on.

[] makes use of __getitem__ or __setitem__ depending on how it's used, () uses __call__ and . uses __getattr__ or __getattribute__
= is assignment, what exactly that means varies a lot based on what's on the left side of it.
: is not an operator, it's just used in various places in the syntax

More about those special names here: https://docs.python.org/2/reference/datamodel.html

Generally those names are used to implement various language features and aren't meant to be used directly. For example, a value that can be iterated through in a for-loop will need to have an __iter__ method

Those do not exist in Python, statements are language constructs, they can't be called. Expressions evaluate to values, and you can use operators with values.