When to use () or []?

Parentheses

() parentheses in python are used for grouping statements (e.g. mathematics), designating function arguments, and creating tuples

In [1]:
def damping_ratio(m, b, k):  # parentheses used to declare function inputs
    wn = (k/m)**(1/2)  # parentheses are used to group terms in a math expression
    zeta = b/2/m/wn
    return zeta
In [2]:
damping_ratio(2.0, 0.2, 10.0)  # parentheses are used to provide inputs to a function
Out[2]:
0.022360679774997897
In [3]:
a_tuple = (1, 2, 2)  # parentheses are used to group objects into a tuple
type(a_tuple)
Out[3]:
tuple
In [4]:
import numpy
In [5]:
type(numpy)
Out[5]:
module
In [6]:
numpy.mean(a_tuple)  # calling a function from the numpy module
Out[6]:
1.6666666666666667
In [7]:
a_tuple.count(2)  # calling a function associated with an object
Out[7]:
2

A tuple of the right length and order can be supplied as inputs to a function like so:

In [8]:
damping_ratio(*a_tuple)
Out[8]:
0.7071067811865475
In [9]:
damping_ratio(1, 2, 2)
Out[9]:
0.7071067811865475

Brackets

[] are used for accessing values from container types (dictionaries, tuples, arrays, lists, etc.) and creating lists

Accessing values from dictionaries with []:

In [10]:
my_dict = {'mass': 12.0,
           'inertia': 13.0,
           'mars': 'planet'}  # note that braces are used to create dictionaries
In [11]:
type(my_dict)
Out[11]:
dict
In [12]:
my_dict['mass']
Out[12]:
12.0
In [13]:
my_dict['mars']
Out[13]:
'planet'
In [14]:
my_second_dict = {1: 1, 2: 4, 3: 9, 5: 25}
In [15]:
type(my_second_dict)
Out[15]:
dict
In [16]:
my_second_dict[3]
Out[16]:
9

Accessing values from tuples with []

In [17]:
my_tuple = (1, 'see', 5.0, my_dict, damping_ratio)  # you can store anything in a tuple
In [18]:
type(my_tuple)
Out[18]:
tuple
In [19]:
my_tuple[3]  # 4th item from the tuple
Out[19]:
{'mass': 12.0, 'inertia': 13.0, 'mars': 'planet'}
In [20]:
my_tuple[4]
Out[20]:
<function __main__.damping_ratio(m, b, k)>
In [21]:
type(my_tuple[4])
Out[21]:
function
In [22]:
my_tuple[2]
Out[22]:
5.0

Accessing values from lists with []:

In [23]:
my_list = ['first thing', 'second thing', 'third thing', 1200, my_tuple]  # you can store anything in lists
In [24]:
my_list[4]
Out[24]:
(1,
 'see',
 5.0,
 {'mass': 12.0, 'inertia': 13.0, 'mars': 'planet'},
 <function __main__.damping_ratio(m, b, k)>)
In [25]:
type(my_list[4])
Out[25]:
tuple
In [26]:
my_list[1]
Out[26]:
'second thing'

Accessing values from arrays with []:

In [27]:
import numpy as np
In [28]:
my_array = np.array([15, 15, 90, 40, 60])  # everything in an array must have the same type (all integers in this case)
In [29]:
my_array
Out[29]:
array([15, 15, 90, 40, 60])
In [30]:
type(my_array)
Out[30]:
numpy.ndarray
In [31]:
my_array[2]
Out[31]:
90
In [32]:
my_array[4]
Out[32]:
60

Slicing with []. You can select subsets from tuples, lists, and arrays.

In [33]:
my_array[1:3]
Out[33]:
array([15, 90])
In [34]:
my_array[0:3]
Out[34]:
array([15, 15, 90])
In [35]:
my_array[3:5]
Out[35]:
array([40, 60])
In [36]:
my_list[0:2]
Out[36]:
['first thing', 'second thing']
In [37]:
my_tuple[0:2]
Out[37]:
(1, 'see')

More about indexing and slicing can be found here: https://scipy-lectures.org/intro/numpy/array_object.html#indexing-and-slicing

[] are used to access columns from a Pandas data frame, similiar to the underlying dictionary and rows from a column (Series).

More about accessing data from a DataFrame can be found here: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html

In [38]:
import pandas as pd
In [39]:
col_dict = {'col1': [1, 2, 3],
            'col2': ['a', 'b', 'c'],
            'col3': [1.2, 3.4, 5.6]}
In [40]:
col_dict['col1']
Out[40]:
[1, 2, 3]
In [41]:
df = pd.DataFrame(col_dict)
In [42]:
df
Out[42]:
col1 col2 col3
0 1 a 1.2
1 2 b 3.4
2 3 c 5.6
In [43]:
df['col1']
Out[43]:
0    1
1    2
2    3
Name: col1, dtype: int64
In [44]:
col2 = df['col2']
col2
Out[44]:
0    a
1    b
2    c
Name: col2, dtype: object
In [45]:
col2[1]  # second item from col2
Out[45]:
'b'

Square brackets also let you take "slices" of the rows:

In [46]:
df[1:]
Out[46]:
col1 col2 col3
1 2 b 3.4
2 3 c 5.6