6

I'm a non programmer who just started learning python (version 3) and am getting a little confused about when a square bracket is needed in my code vs round bracket.

Is there a general rule of thumb?

vaultah
  • 44,105
  • 12
  • 114
  • 143
aqct15
  • 79
  • 1
  • 1
  • 4
  • 3
    square bracket is used for indexing an array/list/dict, round brackets are used in function calls. – username123 Oct 10 '17 at 20:33
  • 1
    Square and round brackets mean different things in different instances, and are almost never interchangeable. You should learn the specific uses from most general tutorial websites – Aaron Oct 10 '17 at 20:33
  • @username123 what about `tuples`? – MooingRawr Oct 10 '17 at 20:34
  • @MooingRawr For indexing elements in tuples, you should also use square brackets – username123 Oct 10 '17 at 20:35
  • my point is your statement is not really answering this super broad question. there are more than what you have given – MooingRawr Oct 10 '17 at 20:36
  • It's an extremely broad question. Hard to answer with just a bunch of examples – kjmerf Oct 10 '17 at 20:36
  • I do not know if there is a complete cheatsheet on how and when should [] and () be used, but you may read a tutorial and you will be able to figure it out. – username123 Oct 10 '17 at 20:38

3 Answers3

14

They are part of the Python syntax and unlike using single (') or double (") quotes, they can pretty much never be interchanged.

Square and rounded brackets can mean so many different things in different circumstances. Just to give an example, one may think that both the following are identical:

a = [1,2,3]
a = (1,2,3)

as a[0] gives 1 in both cases. However, the first one is creating a list whereas the second is a tuple. These are different data types and not knowing the distinction can lead to difficulties.

Above is just one example where square and rounded brackets differ but there are many, many others. For example, in an expression such as:

4 * ((12 + 6) / 9)

using square brackets would lead to a syntax error as Python would think you were trying to create a nested list:

4 * [[12 + 6] / 9]

So hopefully you can see from above, that the two types of brackets do completely different things in situations which seem identical. There is no real rule of thumb for when one type does what. 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.

Hope this helps you out a bit!

Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
6

It's hard to answer succinctly, but I can give you some common examples.

Square brackets define lists:

my_list = [1, 2, 3, 4]

They are also used for indexing lists. For instance:

print(my_list[1])

Returns 2. Additionally, they are frequently used to index dictionaries, which are defined with curly brackets:

my_dict = {5:'a', 6:'b', 7:'c'}

The indexing for dictionaries requires that I input the "key" as follows:

print(my_dict[6])

Returns b.

Functions are called using round brackets. For instance, if I want to add an element to my list, I can call the append() function:

my_list.append(8)

I have just added 8 to my list. You will notice that when I called the print function I also used curved brackets.

This is by no means comprehensive, but hopefully it will give a starting point.

kjmerf
  • 4,275
  • 3
  • 21
  • 29
4

These are parts of the syntax:

  • Square [] brackets are used for:

    • defining lists: list = [ 1, 2, 3]
    • array indexing: ages[3] = 29
    • and more
  • Round () brackets are used for:

    • defining tuples: retval = ( x, y, z )
    • operator precedence: result = (x + y) * z
    • class/function definitions and invocations: def func(x, y) or func(3,7)
    • and more
Daniel Trugman
  • 8,186
  • 20
  • 41
  • To say that parenthesis are used for parenthesized forms is a bit circular, isn't it? I realize that's the terminology, but I think this answer would be better if it had a bit more than just links. – Bryan Oakley Oct 10 '17 at 20:54