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
damping_ratio(2.0, 0.2, 10.0) # parentheses are used to provide inputs to a function
0.022360679774997897
a_tuple = (1, 2, 2) # parentheses are used to group objects into a tuple
type(a_tuple)
tuple
import numpy
type(numpy)
module
numpy.mean(a_tuple) # calling a function from the numpy module
1.6666666666666667
a_tuple.count(2) # calling a function associated with an object
2
A tuple of the right length and order can be supplied as inputs to a function like so:
damping_ratio(*a_tuple)
0.7071067811865475
damping_ratio(1, 2, 2)
0.7071067811865475
[]
are used for accessing values from container types (dictionaries, tuples, arrays, lists, etc.) and creating lists
Accessing values from dictionaries with []
:
my_dict = {'mass': 12.0,
'inertia': 13.0,
'mars': 'planet'} # note that braces are used to create dictionaries
type(my_dict)
dict
my_dict['mass']
12.0
my_dict['mars']
'planet'
my_second_dict = {1: 1, 2: 4, 3: 9, 5: 25}
type(my_second_dict)
dict
my_second_dict[3]
9
Accessing values from tuples with []
my_tuple = (1, 'see', 5.0, my_dict, damping_ratio) # you can store anything in a tuple
type(my_tuple)
tuple
my_tuple[3] # 4th item from the tuple
{'mass': 12.0, 'inertia': 13.0, 'mars': 'planet'}
my_tuple[4]
<function __main__.damping_ratio(m, b, k)>
type(my_tuple[4])
function
my_tuple[2]
5.0
Accessing values from lists with []
:
my_list = ['first thing', 'second thing', 'third thing', 1200, my_tuple] # you can store anything in lists
my_list[4]
(1, 'see', 5.0, {'mass': 12.0, 'inertia': 13.0, 'mars': 'planet'}, <function __main__.damping_ratio(m, b, k)>)
type(my_list[4])
tuple
my_list[1]
'second thing'
Accessing values from arrays with []
:
import numpy as np
my_array = np.array([15, 15, 90, 40, 60]) # everything in an array must have the same type (all integers in this case)
my_array
array([15, 15, 90, 40, 60])
type(my_array)
numpy.ndarray
my_array[2]
90
my_array[4]
60
Slicing with []
. You can select subsets from tuples, lists, and arrays.
my_array[1:3]
array([15, 90])
my_array[0:3]
array([15, 15, 90])
my_array[3:5]
array([40, 60])
my_list[0:2]
['first thing', 'second thing']
my_tuple[0:2]
(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
import pandas as pd
col_dict = {'col1': [1, 2, 3],
'col2': ['a', 'b', 'c'],
'col3': [1.2, 3.4, 5.6]}
col_dict['col1']
[1, 2, 3]
df = pd.DataFrame(col_dict)
df
col1 | col2 | col3 | |
---|---|---|---|
0 | 1 | a | 1.2 |
1 | 2 | b | 3.4 |
2 | 3 | c | 5.6 |
df['col1']
0 1 1 2 2 3 Name: col1, dtype: int64
col2 = df['col2']
col2
0 a 1 b 2 c Name: col2, dtype: object
col2[1] # second item from col2
'b'
Square brackets also let you take "slices" of the rows:
df[1:]
col1 | col2 | col3 | |
---|---|---|---|
1 | 2 | b | 3.4 |
2 | 3 | c | 5.6 |