I am curious, what do the 3 different brackets mean in Python programming? Not sure if I'm correct about this, but please correct me if I'm 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 welcomed! Thanks!
They are parentheses, square brackets, curly brackets, and angle brackets.
The difference between a 'bracket' and a 'parentheses' can be a bit confusing. Generally, 'parentheses' refers to round brackets ( ) and 'brackets' to square brackets [ ]. However, we are more and more used to hearing these referred to simply as 'round brackets' or 'square brackets'.
In general, I guess that square brackets are used mainly for lists and indexing things whereas rounded brackets are for calculations (as you would in maths) and functions etc.
The single and double square brackets are used as indexing operators in R Programming Language. Both of these operators are used for referencing the components of R storage objects either as a subset belonging to the same data type or as an element.
[]
: Lists and indexing/lookup/slicing
[]
, [1, 2, 3]
, [i**2 for i in range(5)]
'abc'[0]
→ 'a'
{0: 10}[0]
→ 10
'abc'[:2]
→ 'ab'
()
: Tuples, order of operations, generator expressions, function calls and other syntax.
()
, (1, 2, 3)
t = 1, 2
→ (1, 2)
(n-1)**2
(i**2 for i in range(5))
print()
, int()
, range(5)
, '1 2'.split(' ')
sum(i**2 for i in range(5))
{}
: Dictionaries and sets, as well as string formatting
{}
, {0: 10}
, {i: i**2 for i in range(5)}
{0}
, {i**2 for i in range(5)}
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.
() 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With