{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Jupyter notebooks and python\n", "\n", "## Jupyter notebook basics\n", "\n", "This document is a Jupyter notebook that I wrote to be a kind of scratchpad to get started.\n", "\n", "First, a few things about how to use Jupyter notebooks. This notebook is comprised of a number of cells. There are two types of cells: markdown and code. The markdown cells are documentation that may include formatted text, hyperlinks, and pictures---a lot like a webpage, but with better mathematics formatting. This cell, for example is a markdown cell, and you can see that we can put well formatted mathematics here: \n", "\n", "$$\\int_{-\\infty}^{+\\infty} e^\\frac{-x^2}{2}dx=\\sqrt{2\\pi}.$$\n", "\n", "If you want to edit a markdown cell, select it by double clicking. This makes the commands used to create the cell visible. You can edit the cell in this mode, and press SHIFT-Return to present the markdown cell again.\n", "\n", "I think the Markdown Guide provided by Google's Colab is a good reference.\n", "\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Code cells contain bits of executable python programs and can be executed by selecting that cell and pressing SHIFT-Return, or by using the play button on the menubar. Code cells are indicated in the left margin by \"In [ ]:\" For example, the next cell is a code cell. When you exectute it, it should produce the output on the line below the cell, indicated in the left margin by a matching \"Out [ ]:\"" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2+3" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "It's not difficult to write Jupyter notebooks and to format mathematics---you'll learn how. But for now, see if you can use this notebook to get familiar with the basics of python.\n", "\n", "Try selecting the code cells above, then change the code from 2+3 to something else, like 3+3, and execute it with SHIFT-Return." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Here are the first examples from section 1.2 of our textbook *Explorations in Numerical Analysis, Python Edition*" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "a=3+4" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7\n" ] } ], "source": [ "print(a)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import math" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "b = math.sqrt(a)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.6457513110645907" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1096.6331584284585" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c=math.exp(a)\n", "c" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'%.4f'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%precision 4" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.6458" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.6457513110645907161710965738166123628616333007812500000000000000000000" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%precision 70\n", "b" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "36\n", "37\n", "72\n" ] } ], "source": [ "# a single line of code can be put on multiple lines if you use pararentheses \n", "x=(1+2+3+4+\n", " 5+6+7+8)\n", "\n", "# and multiple lines can be put on the same line if seperated by a semicolon\n", "print(x); print(x+1); print(2*x)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n", "8\n", "9\n" ] } ], "source": [ "# indexing starts with 0, not 1\n", "for n in range(10):\n", " print(n)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "x=1" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "x='hello' #redefines x" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "print(x)" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n" ] } ], "source": [ "x = [1,2,3]\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "x.append(4) # add 4 as a new entry at the end of the list x" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4]\n" ] } ], "source": [ "print(x)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x)" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "something else\n" ] } ], "source": [ "x='something else'\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x)" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.666666666666667" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "17/3" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "17//3" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "17%3" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "There's a difference between equality and identity." ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x=[1,5,10]\n", "y=[1,5,10]\n", "x==y" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x is y" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "z=x" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x is z" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "1. Write a program to add the numbers from 1 to 100.\n", "1. Write a program that makes a list of even numbers from 1 to 100.\n", "1. Remove the number 8 from the list you created in exercise 2.\n", "1. Write a function that takes an integer n as a parameter and returns the sum of the numbers from 1 to n." ] } ], "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.9.16" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }