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
a_tuple = (1, 2, 2) # parentheses are used to group objects into a tuple
type(a_tuple)
import numpy
type(numpy)
numpy.mean(a_tuple) # calling a function from the numpy module
a_tuple.count(2) # calling a function associated with an object
A tuple of the right length and order can be supplied as inputs to a function like so:
damping_ratio(*a_tuple)
damping_ratio(1, 2, 2)
[]
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)
my_dict['mass']
my_dict['mars']
my_second_dict = {1: 1, 2: 4, 3: 9, 5: 25}
type(my_second_dict)
my_second_dict[3]
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)
my_tuple[3] # 4th item from the tuple
my_tuple[4]
type(my_tuple[4])
my_tuple[2]
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]
type(my_list[4])
my_list[1]
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
type(my_array)
my_array[2]
my_array[4]
Slicing with []
. You can select subsets from tuples, lists, and arrays.
my_array[1:3]
my_array[0:3]
my_array[3:5]
my_list[0:2]
my_tuple[0:2]
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']
df = pd.DataFrame(col_dict)
df
df['col1']
col2 = df['col2']
col2
col2[1] # second item from col2
Square brackets also let you take "slices" of the rows:
df[1:]