{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "*This notebook contains course material from [CBE30338](https://jckantor.github.io/CBE30338)\n", "by Jeffrey Kantor (jeff at nd.edu); the content is available [on Github](https://github.com/jckantor/CBE30338.git).\n", "The text is released under the [CC-BY-NC-ND-4.0 license](https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode),\n", "and code is released under the [MIT license](https://opensource.org/licenses/MIT).*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "< [Getting Started with Python and Jupyter Notebooks](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/01.01-Getting-Started-with-Python-and-Jupyter-Notebooks.ipynb) | [Contents](toc.ipynb) | [Python Conditionals and Libraries](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/01.03-Python-Conditionals-and-Libraries.ipynb) >
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python Basics\n",
"\n",
"**A Tutorial by Jacob Gerace**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 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 in lecture, or when you need to write python for an assignment.\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": {},
"source": [
"### 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": {},
"source": [
"### 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": {},
"source": [
"### 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": {},
"source": [
"## Python Basics"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Variables"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"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": {},
"source": [
"### More Complicated Data Types"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Green', 'Blue', 'Red']\n",
"[10, 20, 30, 40, 50, 'Sixty']\n",
"Green\n",
"Red\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": {},
"source": [
"### Using Variables"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"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": {},
"source": [
"### Importing in Python: Math and plotting"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"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\n",
"print(math.log(float1))\n",
"print(math.exp(float2))\n",
"print(math.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": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.749199854809259\n",
"9.487735836358526\n",
"32.0\n",
"32.0\n"
]
},
{
"data": {
"text/plain": [
"[ "
]
}
],
"metadata": {
"anaconda-cloud": {},
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}