diff options
Diffstat (limited to 'python/atms-310/notebooks/Week 02 M.ipynb')
| -rwxr-xr-x | python/atms-310/notebooks/Week 02 M.ipynb | 380 |
1 files changed, 380 insertions, 0 deletions
diff --git a/python/atms-310/notebooks/Week 02 M.ipynb b/python/atms-310/notebooks/Week 02 M.ipynb new file mode 100755 index 0000000..a9fbf99 --- /dev/null +++ b/python/atms-310/notebooks/Week 02 M.ipynb @@ -0,0 +1,380 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h1>2. Lists and Dictionaries</h1>\n", + "<h2>10/02/23</h2>" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h2>2.0 Last Time...</h2>\n", + "\n", + "<ul>\n", + " <li>Python has a variety of data types, many of which we saw today!</li>\n", + " <li>An <b>integer</b> variable is a number with no decimal point.</li>\n", + " <li>A <b>floating point</b> variable is a number with a decimal point</li>\n", + " <li>A <b>string</b> variable is text.</li>\n", + " <li>A <b>Boolean</b> variable can take on the values <b>True</b> and <b>False</b>.</li>\n", + " <li>A <b>NoneType</b> variable can take on the value <b>None</b>.</li>\n", + " <li>Python uses dynamic typing, which means you don't need to define a variable's type when you first use that variable.</li>\n", + " <li>The type() command lets you see what type a given variable is. int(), float(), and str() can be used to convert to different variable types.</li>\n", + " <li>Combining a float and an integer results in a float.</li>\n", + " <li>Combining a string and an integer or float results in a TypeError.</li>\n", + " <li>Floats are often prone to rounding errors.</li>\n", + " <li><b>and</b> and <b>or</b> can be used to combine Boolean variables.</li>\n", + "</ul>" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h2>2.1 Lists and Tuples</h2>\n", + "\n", + "A <b>list</b> is simply an ordered sequence of values, similar to arrays in other programming languages. Each element of a list can be assigned to anything (you can have a mix of variable types within a given list), including another list.\n", + "\n", + "Lists are defined using square brackets (\"[]\"), and individual elements have commas between them.\n", + "\n", + "Counting in Python starts with <b>0</b>. This can lead to some confusion, but always remember that the first element in a list is actually element 0, the second is actually element 1, and so on.\n", + "\n", + "You can also count from the end, so that element <b>-1</b> is actually the last element in a list, <b>-2</b> is the second-last element, and so on.\n", + "\n", + "Finally, to find the length of a list, use the len() command." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Create a complicated list that has a list as one of its elements.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# As a side note, if you want to split a long line of code across\n", + "# multiple lines, just use a backslash with nothing after it:\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Find the length of this list.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# To get a value from a list within a list:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Likewise, you can make use of the reverse numbering:\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A list can be sliced to just obtain a subsection using a colon to separate the lower and upper limits. The lower limit of the range is <b>inclusive</b> and the upper range is <b>exclusive</b>, so that a [1:3] subset will consist of the second and third elements in a list." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Values within a list can be changed; lists are <b>mutable</b>. Changing by assignment is the most basic way to do this." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Let's change 'hello' to 'goodbye' in the original list.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Lists in python actually have some built-in functions called <b>methods</b> that can change the properties of the list. We'll talk about these more when we get into object-oriented programming later in this course, but for now, we'll consider <b>insert</b>, <b>remove</b> and <b>append</b>" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 'Insert' places the requested element into the list \n", + "# before the specified element of the list.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# 'Remove' gets rid of the first occurrence of the specified element.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Append adds the specified element to the end of the list.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To an extent, you can treat an individual string as a list." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "A <b>tuple</b> is simply a list that cannot be changed (i.e., <b>immutable</b>), and an error will be returned by Python if you do attempt to change an element within a tuple. Defining a tuple is exactly the same as defining a list, except that you use parentheses instead of square brackets." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Create a simple tuple:\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# But note that you cannot change a value within a tuple.\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h2>2.2 Dictionaries</h2>\n", + "\n", + "A dictionary, as opposed to a list/tuple, is an <b>unordered</b> list: elements are referenced by <b>keys</b>, not position. A key can be anything, as can the value it refers to.\n", + "\n", + "A dictionary is delimited by curly braces (\"{}\"), and each element is a key:value pair separated by a colon. In order to refer to a particular element in a dictionary, we proceed much as we do with a list, except using a key instead of an element address." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Let's create a dictionary similar to our list from earlier.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Here, our keys are 'value_a','value_b',and 'value_c', and we can refer to \n", + "# each of them accordingly.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Just like lists, dictionaries have <b>methods</b> - and again, we'll go over this in more detail as we get to the object-oriented programming part of this course, but for now, consider <b>keys</b> and <b>values</b>." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# \"Keys\" returns a list of all the keys of a.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# \"Values\" returns a list of all the values of a.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# To check whether a particular key is used in a dictionary, use the following\n", + "# syntax:\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note that <b>keys</b> and <b>values</b> will return key:value pairs in no particular order. To make sure one corresponds to the other, you can first sort the dictionary into a desired order using something like <b>sorted</b>, which we'll explore soon." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "<h1>TAKE-HOME POINTS</h1>\n", + "\n", + "<ul>\n", + " <li>A list is a <b>mutable</b>, <b>ordered</b> sequence of elements.</li>\n", + " <li>Python counts starting with 0, not 1.</li>\n", + " <li>You can also count backwards from the end of a list using negative numbers.</li>\n", + " <li>A list can be sliced, where the slice is defined by a range that has an inclusive lower bound and an exclusive upper bound.</li>\n", + " <li>Values in a list can be changed by assignment or by particular methods.</li>\n", + " <li>A tuple is an <b>immutable</b>, <b>ordered</b> sequence of elements.</li>\n", + " <li>A dictionary is a <b>mutable</b>, <b>unordered</b> sequence of elements consisting of key:value pairs.</li>\n", + "</ul>" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.10.10" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} |
