Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python, when to use a square or round brackets? [duplicate]

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?

like image 578
aqct15 Avatar asked Oct 10 '17 20:10

aqct15


People also ask

What is [] used for in Python?

[] (Index brackets) Index brackets ([]) have many uses in Python. First, they are used to define "list literals," allowing you to declare a list and its contents in your program. Index brackets are also used to write expressions that evaluate to a single item within a list, or a single character in a string.

Why do we use double square brackets in Python?

Square brackets are used to define lists, but also to get things from lists. When you have a list of lists and want something from an inner list, you need to get that inner list (using brackets) and then get the desired thing inside (using brackets again).

How do you know which bracket to use in Python?

The standard way to format strings in Python is to use a combination of curly braces and standard parenthesis, by inserting empty curly braces in the place where you want to add something to a string. However, as of Python 3.6, an alternative and more elegant way of formatting strings was introduced using f-strings.

What's the difference between square brackets and round brackets?

Usually we use square brackets - [ ] - for special purposes such as in technical manuals. Round brackets - ( ) - are used in a similar way to commas when we want to add further explanation, an afterthought, or comment that is to do with our main line of thought but distinct from it.

What are square brackets used for in Python?

Square brackets define lists: They are also used for indexing lists. For instance: Returns 2. Additionally, they are frequently used to index dictionaries, which are defined with curly brackets: The indexing for dictionaries requires that I input the "key" as follows: Returns b. Functions are called using round brackets.

What do the square brackets mean in a list comprehension?

The square brackets tell Python that this is a list comprehension, producing a list. If you use curly braces, you’ll get either a set or a dict back, and if you use regular parentheses, you’ll get a generator expression (see above). Requesting individual items

What is the difference between square brackets and round brackets?

square bracket is used for indexing an array/list/dict, round brackets are used in function calls. Square and round brackets mean different things in different instances, and are almost never interchangeable.

What are brackets used for?

Brackets are punctuation marks used in pairs. There are four types of brackets used in writing: Read more about the four main types of brackets. This page covers how to use round brackets (also called parentheses) and square brackets. These two types of brackets are, by far, the most common brackets used in writing.


3 Answers

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!

like image 98
Joe Iddon Avatar answered Oct 16 '22 07:10

Joe Iddon


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.

like image 29
kjmerf Avatar answered Oct 16 '22 09:10

kjmerf


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
like image 4
Daniel Trugman Avatar answered Oct 16 '22 08:10

Daniel Trugman