{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### This is a Jupyter notebook\n", "\n", "Throughout this course we will be running Python in Jupyter notebooks.\n", "\n", "Jupyter notebooks consist of a combination of markdown cells and code cells. This cell is a markdown cell.\n", "\n", "The cell below is a code cell. To execute the code in the code cell, click the run triangle next to the cell." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = [4,7,2,8,9,2]\n", "len(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python is a dynamic language. To create a variable we simply assign a value to the variable. The variable will take on the type of the data stored in the variable. In this example x contains a Python list object.\n", "\n", "When you evaluate a cell in a Jypyter notebook the output that gets printed is the result of the last statement in the cell." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basics of Python\n", "\n", "Python uses indentation in place of curly braces to indicate structure in loops, if-else statements, and other types of statements. To add a level of indentation we use a tab." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 is not in the list\n" ] } ], "source": [ "search = 5\n", "present = False\n", "for n in x:\n", " if n == search:\n", " present = True\n", "if present:\n", " print(\"5 is in the list\")\n", "else:\n", " print(\"5 is not in the list\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is an example of a function definition in Python." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7 is present\n" ] } ], "source": [ "def isPresent(x,xs):\n", " for n in xs:\n", " if n == x:\n", " return True\n", " return False\n", "\n", "if isPresent(7,x):\n", " print(\"7 is present\")\n", "else:\n", " print(\"7 is not present\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python offers a large number of packages. To import a Python package we use the import command.\n", "\n", "The numpy package is one of the more popular packages in Python. This package offers many facilities for manipulating lists and arrays of numbers." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([5, 7, 2, 1, 1, 4, 4, 4, 8, 2])" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "y = np.random.randint(low = 1,high=9,size=10)\n", "y" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The average of the numbers in the list is 4.8.\n", "The largest number in list is 7.\n" ] } ], "source": [ "print(f\"The average of the numbers in the list is {y.mean()}.\")\n", "print(f\"The largest number in list is {y.max()}.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Vector processing\n", "\n", "In this course we will frequently be using Kera and Tensorflow to process large blocks of data. Numpy is also built from the ground up to do a special style of data processing, called vector processing.\n", "\n", "The next couple of code samples demonstrate how this works.\n", "\n", "Here is numpy array containing a large set of sample points." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0.05, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 , 0.45, 0.5 ,\n", " 0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85, 0.9 , 0.95, 1. , 1.05,\n", " 1.1 , 1.15, 1.2 , 1.25, 1.3 , 1.35, 1.4 , 1.45])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "xs = np.arange(0.,1.5,0.05)\n", "xs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can apply a function to each element of this list by using special Numpy vector functions." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0.00249896, 0.00998334, 0.02241572, 0.03973387,\n", " 0.06185099, 0.08865606, 0.12001423, 0.15576734, 0.19573449,\n", " 0.23971277, 0.28747798, 0.33878548, 0.39337116, 0.45095238,\n", " 0.51122907, 0.57388487, 0.63858834, 0.70499422, 0.77274473,\n", " 0.84147098, 0.91079439, 0.9803281 , 1.04967853, 1.1184469 ,\n", " 1.18623077, 1.25262564, 1.31722653, 1.37962962, 1.43943384])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ys = xs * np.sin(xs)\n", "ys" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Numpy both the multiplication function (*) and the np.sin() function are vector functions. When you apply them to a numpy array the function automatically get applied to every element in the array." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Tuples and list comprehensions\n", "\n", "As an alternative to the list structure Python also offers the tuple structure. Tuples and lists share many common features: you can access elements in tuple or a list via the usual list index notation using square braces. The main difference between lists and tuples is that lists are mutable while tuples are not:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The second item in t is 6\n", "The second item in lt is 6\n", "[5, 6, 7]\n" ] }, { "ename": "AttributeError", "evalue": "'tuple' object has no attribute 'append'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[11], line 8\u001b[0m\n\u001b[1;32m 6\u001b[0m lt\u001b[39m.\u001b[39mappend(\u001b[39m7\u001b[39m)\n\u001b[1;32m 7\u001b[0m \u001b[39mprint\u001b[39m(lt)\n\u001b[0;32m----> 8\u001b[0m t\u001b[39m.\u001b[39;49mappend(\u001b[39m7\u001b[39m)\n", "\u001b[0;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'append'" ] } ], "source": [ "t = (5,6)\n", "lt = [5,6]\n", "print(f\"The second item in t is {t[1]}\")\n", "print(f\"The second item in lt is {lt[1]}\")\n", "\n", "lt.append(7)\n", "print(lt)\n", "t.append(7)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A frequently used programming pattern in Python is to return a tuple from a function." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 7)\n" ] } ], "source": [ "def betterSearch(x,xs):\n", " for i,n in enumerate(xs):\n", " if n > x:\n", " return (i,n)\n", " return (-1,-1)\n", "\n", "location, value = betterSearch(5,x)\n", "print((location,value))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Python `enumerate()` function returns a sequence of tuples: the first item in each tuple is the index of the current item in the list, and the second item is the current item." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another less commonly used programming pattern in Python loops is to iterate over a range of integers that covers all of the index positions in a list. Here is the last example rewritten to use this approach." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 7)\n" ] } ], "source": [ "def betterSearch(x,xs):\n", " for i in range(0,len(xs)):\n", " if xs[i] > x:\n", " return (i,xs[i])\n", " return (-1,-1)\n", "\n", "location, value = betterSearch(5,x)\n", "print((location,value))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A Python list comprehension is an alterative method for constructing a list. This method is usually used to construct a new list from an existing list." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2.0,\n", " 2.6457513110645907,\n", " 1.4142135623730951,\n", " 2.8284271247461903,\n", " 3.0,\n", " 1.4142135623730951]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z = [np.sqrt(n) for n in x]\n", "z" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can convert any Python list to a Numpy array by using the np.array() constructor function." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2. , 2.64575131, 1.41421356, 2.82842712, 3. ,\n", " 1.41421356])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nz = np.array(z)\n", "nz" ] } ], "metadata": { "kernelspec": { "display_name": "env_tensorflow", "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.9.16" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }