{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# When to use `()` or `[]`?\n", "\n", "## Parentheses\n", "\n", "`()` parentheses in python are used for grouping statements (e.g. mathematics), designating function arguments, and creating tuples" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def damping_ratio(m, b, k): # parentheses used to declare function inputs\n", " wn = (k/m)**(1/2) # parentheses are used to group terms in a math expression\n", " zeta = b/2/m/wn\n", " return zeta" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.022360679774997897" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "damping_ratio(2.0, 0.2, 10.0) # parentheses are used to provide inputs to a function" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_tuple = (1, 2, 2) # parentheses are used to group objects into a tuple\n", "type(a_tuple)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import numpy" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "module" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(numpy)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.6666666666666667" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "numpy.mean(a_tuple) # calling a function from the numpy module" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a_tuple.count(2) # calling a function associated with an object" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A tuple of the right length and order can be supplied as inputs to a function like so:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7071067811865475" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "damping_ratio(*a_tuple)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.7071067811865475" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "damping_ratio(1, 2, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Brackets\n", "\n", "`[]` are used for accessing values from container types (dictionaries, tuples, arrays, lists, etc.) and creating lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Accessing values from dictionaries with `[]`:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "my_dict = {'mass': 12.0,\n", " 'inertia': 13.0,\n", " 'mars': 'planet'} # note that braces are used to create dictionaries" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_dict)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12.0" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict['mass']" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'planet'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_dict['mars']" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "my_second_dict = {1: 1, 2: 4, 3: 9, 5: 25}" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_second_dict)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_second_dict[3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Accessing values from tuples with `[]`" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "my_tuple = (1, 'see', 5.0, my_dict, damping_ratio) # you can store anything in a tuple" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_tuple)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'mass': 12.0, 'inertia': 13.0, 'mars': 'planet'}" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_tuple[3] # 4th item from the tuple" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_tuple[4]" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "function" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_tuple[4])" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_tuple[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Accessing values from lists with `[]`:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "my_list = ['first thing', 'second thing', 'third thing', 1200, my_tuple] # you can store anything in lists" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1,\n", " 'see',\n", " 5.0,\n", " {'mass': 12.0, 'inertia': 13.0, 'mars': 'planet'},\n", " )" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list[4]" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tuple" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_list[4])" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'second thing'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Accessing values from arrays with `[]`:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "my_array = np.array([15, 15, 90, 40, 60]) # everything in an array must have the same type (all integers in this case)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([15, 15, 90, 40, 60])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_array" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(my_array)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "90" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_array[2]" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "60" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_array[4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Slicing with `[]`. You can select subsets from tuples, lists, and arrays." ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([15, 90])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_array[1:3]" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([15, 15, 90])" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_array[0:3]" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([40, 60])" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_array[3:5]" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['first thing', 'second thing']" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list[0:2]" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 'see')" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_tuple[0:2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "More about indexing and slicing can be found here: https://scipy-lectures.org/intro/numpy/array_object.html#indexing-and-slicing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`[]` are used to access columns from a Pandas data frame, similiar to the underlying dictionary and rows from a column (Series).\n", "\n", "More about accessing data from a `DataFrame` can be found here: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "col_dict = {'col1': [1, 2, 3],\n", " 'col2': ['a', 'b', 'c'],\n", " 'col3': [1.2, 3.4, 5.6]}" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "col_dict['col1']" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame(col_dict)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
col1col2col3
01a1.2
12b3.4
23c5.6
\n", "
" ], "text/plain": [ " col1 col2 col3\n", "0 1 a 1.2\n", "1 2 b 3.4\n", "2 3 c 5.6" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 1\n", "1 2\n", "2 3\n", "Name: col1, dtype: int64" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df['col1']" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 a\n", "1 b\n", "2 c\n", "Name: col2, dtype: object" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "col2 = df['col2']\n", "col2" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'b'" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "col2[1] # second item from col2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Square brackets also let you take \"slices\" of the rows:" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
col1col2col3
12b3.4
23c5.6
\n", "
" ], "text/plain": [ " col1 col2 col3\n", "1 2 b 3.4\n", "2 3 c 5.6" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[1:]" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.7" } }, "nbformat": 4, "nbformat_minor": 4 }