From 7b21727285e50b0b050c4ddd70d07542229c5366 Mon Sep 17 00:00:00 2001 From: pants Date: Mon, 21 Sep 2026 14:17:03 -0700 Subject: init --- python/atms-310/notebooks/Week 02 M.ipynb | 380 ++++++++++++++++++++++++++++++ 1 file changed, 380 insertions(+) create mode 100755 python/atms-310/notebooks/Week 02 M.ipynb (limited to 'python/atms-310/notebooks/Week 02 M.ipynb') 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": [ + "

2. Lists and Dictionaries

\n", + "

10/02/23

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

2.0 Last Time...

\n", + "\n", + "" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

2.1 Lists and Tuples

\n", + "\n", + "A list 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 0. 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 -1 is actually the last element in a list, -2 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 inclusive and the upper range is exclusive, 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 mutable. 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 methods 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 insert, remove and append" + ] + }, + { + "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 tuple is simply a list that cannot be changed (i.e., immutable), 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": [ + "

2.2 Dictionaries

\n", + "\n", + "A dictionary, as opposed to a list/tuple, is an unordered list: elements are referenced by keys, 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 methods - 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 keys and values." + ] + }, + { + "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 keys and values 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 sorted, which we'll explore soon." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "

TAKE-HOME POINTS

\n", + "\n", + "" + ] + }, + { + "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 +} -- cgit v1.2.3