{ "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", "< [Python Basics](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/01.02-Python-Basics.ipynb) | [Contents](toc.ipynb) | [Python Numeric Integration Revisited](http://nbviewer.jupyter.org/github/jckantor/CBE30338/blob/master/notebooks/01.04-Python-Numeric-Integration-Revisited.ipynb) >
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python Conditionals and Libraries"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A Tutorial by Jacob Gerace"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Conditionals in Python"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is the answer to the ultimate question\n",
"This print statement is run no matter what because it is not indented!\n",
"It takes me 11 minutes\n"
]
}
],
"source": [
"#Sometimes you want to execute code only in certain circumstances. \n",
"\n",
"#Change answer and see what code is executed:\n",
"answer = 42\n",
"\n",
"if answer == 42:\n",
" print('This is the answer to the ultimate question')\n",
"elif answer < 42:\n",
" print('This is less than the answer to the ultimate question')\n",
"else:\n",
" print('This is more than the answer to the ultimate question')\n",
"print('This print statement is run no matter what because it is not indented!')\n",
"\n",
"#An if statement is an example of a structure that creates a new block. The block includes all of the code that is \n",
"#indented. The indentation (tab character) is imperative. Don't forget it!\n",
"\n",
"#This is normally just good coding style in other languages, but in python it isn't optional\n",
"\n",
"#We can check multiple things at once using boolean operations\n",
"snowy = True\n",
"day = \"Monday\"\n",
"\n",
"#How long does it take me to get to class in the morning?\n",
"\n",
"if (snowy == False) and (day != \"Monday\"):\n",
" #and is boolean and. True only if both are true. False otherwise\n",
" time = 7\n",
"elif (snowy == True) and (day == \"Monday\"):\n",
" time = 11\n",
"elif (rainy == True) or (day == \"Monday\"):\n",
" time = 9\n",
" \n",
"print(\"It takes me %d minutes\" %(time))\n",
" \n",
"#You can structure these statements more neatly if you \"nest\" if statements (put an if statement inside an if statement)\n",
"#But this is just for edification."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Functions in Python"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-1\n",
"1\n",
"None\n",
"1\n"
]
}
],
"source": [
"#We can separate off code into functions, that can take input and can give output. They serve as black boxes from the \n",
"#perspective of the rest of our code\n",
"\n",
"#use the def keyword, and indent because this creates a new block\n",
"def print_me( string ):\n",
" print(string)\n",
" \n",
" #End with the \"return\" keyword\n",
" return\n",
"\n",
"#Your functions can return data if you so choose\n",
"def step(x):\n",
" if (x < 0):\n",
" return -1\n",
" elif (x > 0):\n",
" return 1\n",
"\n",
"#call functions by repeating their name, and putting your variable in the parenthesis. \n",
"#Your variable need not be named the same thing, but it should be the right type!\n",
"\n",
"print(step(-1))\n",
"print(step(1))\n",
"\n",
"#what happens for x = 0?\n",
"print(step(0))\n",
"\n",
"#Python automatically adds in a \"return none\" statement if you are missing one. \n",
"#If you see \"none\" make sure your program can work with that!\n",
"\n",
"\n",
"#Fix the return none issue\n",
"def step_v2(x):\n",
" if (x < 0):\n",
" return -1\n",
" elif (x >= 0):\n",
" return 1\n",
" \n",
"print(step_v2(0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Numpy - \"The Fundamental Package for Scientific Computing with Python\""
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1 2 3]\n",
"1\n",
"[10 10 10]\n",
"[-8 -6 -4]\n",
"[ 9 16 21]\n",
"[ 0.11111111 0.25 0.42857143]\n",
"[[14 14 14]\n",
" [32 32 32]\n",
" [50 50 50]]\n",
"-9.51619735393e-16\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.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}