{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "LSw75KkTdnet", "nbpages": { "level": 0, "link": "[](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html)", "section": "" } }, "source": [ "\n", "*This notebook contains material from [CBE30338](https://jckantor.github.io/CBE30338);\n", "content is available [on Github](https://github.com/jckantor/CBE30338.git).*\n" ] }, { "cell_type": "markdown", "metadata": { "id": "UbTZxT9zdnez", "nbpages": { "level": 0, "link": "[](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html)", "section": "" } }, "source": [ "\n", "< [1.1 Getting Started with Python and Jupyter Notebooks](https://jckantor.github.io/CBE30338/01.01-Getting-Started-with-Python-and-Jupyter-Notebooks.html) | [Contents](toc.html) | [Tag Index](tag_index.html) | [1.3 Python Conditionals and Libraries](https://jckantor.github.io/CBE30338/01.03-Python-Conditionals-and-Libraries.html)
"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "UbTZxT9zdnez",
"nbpages": {
"level": 0,
"link": "[](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html)",
"section": ""
}
},
"source": [
"# Introduction to Python in Jupyter Notebook/Google Colab"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "UbTZxT9zdnez",
"nbpages": {
"level": 0,
"link": "[](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html)",
"section": ""
}
},
"outputs": [],
"source": [
"!pip install -q -r requirements.txt"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Bnh2iPUbst5v"
},
"source": [
"# 1.1 What is Google Colab\n",
"\n",
"* Free to use for non-commercial use\n",
"* Easy access to high computational power (CPUs, GPUs)\n",
"* Easy start (no need to install or tune)\n",
"* Connected to Google Drive\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wNr0hLX3dne0",
"nbpages": {
"level": 1,
"link": "[1.2 Python Basics](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html#1.2-Python-Basics)",
"section": "1.2 Python Basics"
}
},
"source": [
"# 1.2 Python Basics\n",
"\n",
"**An edit of Tutorial by Jacob Gerace**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5SL1fUv0dne0",
"nbpages": {
"level": 2,
"link": "[1.2.1 What I hope you'll get out of this tutorial](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html#1.2.1-What-I-hope-you'll-get-out-of-this-tutorial)",
"section": "1.2.1 What I hope you'll get out of this tutorial"
}
},
"source": [
"## 1.2.1 What I hope you'll get out of this tutorial\n",
"* The feeling that you'll \"know where to start\" when you see python code.\n",
"* (You won't be a python expert after one hour)\n",
"* Basics to variables, lists, conditionals, functions, loops, and the numpy package.\n",
"* Resources to look further"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IOaAVswwdne0",
"nbpages": {
"level": 3,
"link": "[1.2.1.1 Why Python?](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html#1.2.1.1-Why-Python?)",
"section": "1.2.1.1 Why Python?"
}
},
"source": [
"### 1.2.1.1 Why Python?\n",
"\n",
"1. Clean syntax\n",
"2. The same code can run on all Operating Systems\n",
"3. **Extensive first and third party libraries (of particular note for our purposes is NumPy)**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rRfPHXPKdne1",
"nbpages": {
"level": 3,
"link": "[1.2.1.2 Markdown Sidenote](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html#1.2.1.2-Markdown-Sidenote)",
"section": "1.2.1.2 Markdown Sidenote"
}
},
"source": [
"### 1.2.1.2 Markdown Sidenote\n",
" * This text is written in a Markdown block. Markdown is straightforward way to format writeups in Jupyter, but I won't cover it here for the sake of brevity. \n",
" * See if you can use Markdown in your next homework, here's a link that explains the formatting: https://daringfireball.net/projects/markdown/syntax . \n",
" * You can also look at existing Markdown examples (i.e. this worksheet) and emulate the style. Double click a Markdown box in Jupyter to show the code.\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "C1gGyXPwdne1",
"nbpages": {
"level": 3,
"link": "[1.2.1.3 LaTeX Sidenote](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html#1.2.1.3-LaTeX-Sidenote)",
"section": "1.2.1.3 LaTeX Sidenote"
}
},
"source": [
"### 1.2.1.3 LaTeX Sidenote\n",
"* LaTeX (pronounced \"La-tech\") is a language itself used widely to write documents with symbolic math\n",
"* When you add a mathematical formula to these markdown blocks, the math is in LaTeX.\n",
"* Ex from class: $$V \\frac{dC}{dt} = u(t) - Q C(t)$$ \n",
"* A good resource: https://en.wikibooks.org/wiki/LaTeX/Mathematics\n",
" \n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3Y_C4LEDdne1",
"nbpages": {
"level": 2,
"link": "[1.2.2 Python Basics](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html#1.2.2-Python-Basics)",
"section": "1.2.2 Python Basics"
}
},
"source": [
"## 1.2.2 Python Basics"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "DJVJUKvFdne2",
"nbpages": {
"level": 3,
"link": "[1.2.2.1 Variables](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html#1.2.2.1-Variables)",
"section": "1.2.2.1 Variables"
}
},
"source": [
"### 1.2.2.1 Variables"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 323,
"status": "ok",
"timestamp": 1622104823853,
"user": {
"displayName": "Martin Schätz",
"photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gjb7hTzMGZhh4Pah6B3Hcmw9FADWAQgRfEq6m95Cg=s64",
"userId": "10828352848441153145"
},
"user_tz": -120
},
"id": "J-mTXR3Z-sEo",
"outputId": "41c54501-f78d-460d-807f-9fce231d3301"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"a='1'\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 220,
"status": "ok",
"timestamp": 1622104860373,
"user": {
"displayName": "Martin Schätz",
"photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gjb7hTzMGZhh4Pah6B3Hcmw9FADWAQgRfEq6m95Cg=s64",
"userId": "10828352848441153145"
},
"user_tz": -120
},
"id": "PXczimGNdne2",
"nbpages": {
"level": 3,
"link": "[1.2.2.1 Variables](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html#1.2.2.1-Variables)",
"section": "1.2.2.1 Variables"
},
"outputId": "5bf7c036-c509-4b42-ca82-0d0ba6722716"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"42\n",
"3.1415\n"
]
}
],
"source": [
"#A variable stores a piece of data and gives it a name\n",
"answer = 42\n",
"\n",
"#answer contained an integer because we gave it an integer!\n",
"\n",
"is_it_thursday = True\n",
"is_it_wednesday = False\n",
"\n",
"#these both are 'booleans' or true/false values\n",
"\n",
"pi_approx = 3.1415\n",
"\n",
"#This will be a floating point number, or a number containing digits after the decimal point\n",
"\n",
"my_name = \"Jacob\"\n",
"#This is a string datatype, the name coming from a string of characters\n",
"\n",
"#Data doesn't have to be a singular unit\n",
"\n",
"#p.s., we can print all of these with a print command. For Example:\n",
"print(answer)\n",
"print(pi_approx)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "KCDYCTgmdne3",
"nbpages": {
"level": 3,
"link": "[1.2.2.2 More Complicated Data Types](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html#1.2.2.2-More-Complicated-Data-Types)",
"section": "1.2.2.2 More Complicated Data Types"
}
},
"source": [
"### 1.2.2.2 More Complicated Data Types"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 193,
"status": "ok",
"timestamp": 1622104896239,
"user": {
"displayName": "Martin Schätz",
"photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gjb7hTzMGZhh4Pah6B3Hcmw9FADWAQgRfEq6m95Cg=s64",
"userId": "10828352848441153145"
},
"user_tz": -120
},
"id": "s76leyH1dne3",
"nbpages": {
"level": 3,
"link": "[1.2.2.2 More Complicated Data Types](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html#1.2.2.2-More-Complicated-Data-Types)",
"section": "1.2.2.2 More Complicated Data Types"
},
"outputId": "80d1ab1c-ed82-4f68-99c9-261ffc27a9eb",
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Green', 'Blue', 'Red']\n",
"[10, 20, 30, 40, 50, 'Sixty']\n",
"Green\n",
"Red\n",
"Rosa Parks\n"
]
}
],
"source": [
"\n",
"#What if we want to store many integers? We need a list!\n",
"prices = [10, 20, 30, 40, 50]\n",
"\n",
"#This is a way to define a list in place. We can also make an empty list and add to it.\n",
"colors = []\n",
"\n",
"colors.append(\"Green\")\n",
"colors.append(\"Blue\")\n",
"colors.append(\"Red\")\n",
"\n",
"print(colors)\n",
"\n",
"#We can also add unlike data to a list\n",
"prices.append(\"Sixty\")\n",
"\n",
"#As an exercise, look up lists in python and find out how to add in the middle of a list!\n",
"\n",
"print(prices)\n",
"#We can access a specific element of a list too:\n",
"\n",
"print(colors[0])\n",
"print(colors[2])\n",
"\n",
"#Notice here how the first element of the list is index 0, not 1! \n",
"#Languages like MATLAB are 1 indexed, be careful!\n",
"\n",
"#In addition to lists, there are tuples\n",
"#Tuples behave very similarly to lists except that you can't change them \n",
"# after you make them\n",
"\n",
"#An empty Tuple isn't very useful:\n",
"empty_tuple = ()\n",
"\n",
"#Nor is a tuple with just one value:\n",
"one_tuple = (\"first\",)\n",
"\n",
"#But tuples with many values are useful:\n",
"rosa_parks_info = (\"Rosa\", \"Parks\", 1913, \"February\", 4)\n",
"\n",
"#You can access tuples just like lists\n",
"print(rosa_parks_info[0] + \" \" + rosa_parks_info[1])\n",
"\n",
"# You cannot modify existing tuples, but you can make new tuples that extend \n",
"# the information.\n",
"# I expect Tuples to come up less than lists. So we'll just leave it at that. "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "jsX7mYBIdne4",
"nbpages": {
"level": 3,
"link": "[1.2.2.3 Using Variables](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html#1.2.2.3-Using-Variables)",
"section": "1.2.2.3 Using Variables"
}
},
"source": [
"### 1.2.2.3 Using Variables"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 461,
"status": "ok",
"timestamp": 1622105024526,
"user": {
"displayName": "Martin Schätz",
"photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gjb7hTzMGZhh4Pah6B3Hcmw9FADWAQgRfEq6m95Cg=s64",
"userId": "10828352848441153145"
},
"user_tz": -120
},
"id": "TGZPm8i1dne4",
"nbpages": {
"level": 3,
"link": "[1.2.2.3 Using Variables](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html#1.2.2.3-Using-Variables)",
"section": "1.2.2.3 Using Variables"
},
"outputId": "ce4fc694-d66f-4dd8-ce24-e9d33b649aff",
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8.0\n",
"3.5\n",
"12.9375\n",
"2.5555555555555554\n",
"1\n"
]
}
],
"source": [
"float1 = 5.75\n",
"float2 = 2.25\n",
"#Addition, subtraction, multiplication, division are as you expect\n",
"\n",
"print(float1 + float2)\n",
"print(float1 - float2)\n",
"print(float1 * float2)\n",
"print(float1 / float2)\n",
"\n",
"#Here's an interesting one that showed up in the first homework in 2017. Modulus: \n",
"print(5 % 2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "HLww7Rp2dne5",
"nbpages": {
"level": 3,
"link": "[1.2.2.4 Importing in Python: Math and plotting](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html#1.2.2.4-Importing-in-Python:-Math-and-plotting)",
"section": "1.2.2.4 Importing in Python: Math and plotting"
}
},
"source": [
"### 1.2.2.4 Importing in Python: Math and plotting"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"executionInfo": {
"elapsed": 181,
"status": "ok",
"timestamp": 1622105063936,
"user": {
"displayName": "Martin Schätz",
"photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gjb7hTzMGZhh4Pah6B3Hcmw9FADWAQgRfEq6m95Cg=s64",
"userId": "10828352848441153145"
},
"user_tz": -120
},
"id": "BR-E5Vgfdne5",
"nbpages": {
"level": 3,
"link": "[1.2.2.4 Importing in Python: Math and plotting](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html#1.2.2.4-Importing-in-Python:-Math-and-plotting)",
"section": "1.2.2.4 Importing in Python: Math and plotting"
},
"outputId": "90e4e39e-6aa1-48cd-e11c-45c1d9d795f8"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.749199854809259\n",
"9.487735836358526\n",
"32.0\n",
"32.0\n",
"[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]\n"
]
}
],
"source": [
"#Just about every standard math function on a calculator has a python equivalent pre made.\n",
"#however, they are from the 'math' package in python. Let's add that package!\n",
"import math as m\n",
"print(m.log(float1))\n",
"print(m.exp(float2))\n",
"print(m.pow(2,5))\n",
"# There is a quicker way to write exponents if you want:\n",
"print(2.0**5.0)\n",
"\n",
"#Like in MATLAB, you can expand the math to entire lists\n",
"list3 = [1, 2, 3, 4, 5]\n",
"print(2 * list3)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 282
},
"executionInfo": {
"elapsed": 532,
"status": "ok",
"timestamp": 1622105300659,
"user": {
"displayName": "Martin Schätz",
"photoUrl": "https://lh3.googleusercontent.com/a-/AOh14Gjb7hTzMGZhh4Pah6B3Hcmw9FADWAQgRfEq6m95Cg=s64",
"userId": "10828352848441153145"
},
"user_tz": -120
},
"id": "AK32eNtFdne5",
"nbpages": {
"level": 3,
"link": "[1.2.2.4 Importing in Python: Math and plotting](https://jckantor.github.io/CBE30338/01.02-Python-Basics.html#1.2.2.4-Importing-in-Python:-Math-and-plotting)",
"section": "1.2.2.4 Importing in Python: Math and plotting"
},
"outputId": "ae548221-c5c8-480b-a47a-681b7a23f076"
},
"outputs": [
{
"data": {
"text/plain": [
"[
"
]
}
],
"metadata": {
"anaconda-cloud": {},
"colab": {
"provenance": []
},
"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.9.15"
}
},
"nbformat": 4,
"nbformat_minor": 4
}