Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
bcaffo authored Jun 25, 2024
1 parent d7d6134 commit 5c07086
Showing 1 changed file with 313 additions and 0 deletions.
313 changes: 313 additions & 0 deletions slides/summer_institute/04_python_progamming.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "04e5766e-1b6f-45b8-98a4-ff15044c3120",
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"# Python programming 1"
]
},
{
"cell_type": "markdown",
"id": "5cde6156-39c3-4898-b016-6ce89ab8cb5c",
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"## Control flow"
]
},
{
"cell_type": "markdown",
"id": "ebbd20d1-9176-4d2b-9873-ce6cf51131f9",
"metadata": {
"slideshow": {
"slide_type": "subslide"
},
"tags": []
},
"source": [
"### If statements\n",
"\n",
"+ Python has if statements like most programming languages\n",
"+ Notice the indentation and the `:`"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f33a408a-84cd-4b5a-8511-0cfea531383a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Gryffindor\n"
]
}
],
"source": [
"# do this if you'd like to prompt for an input\n",
"# x = input(\"are you mean (y/n)? > \")\n",
"# Let's just assume the user input 'n' and just assign x='n'\n",
"x = 'n'\n",
"if x == 'y': \n",
" print(\"Slytherine!\")\n",
"else:\n",
" print(\"Gryffindor\")"
]
},
{
"cell_type": "markdown",
"id": "c6e719b7-882c-4441-86fa-42d3e2418dcc",
"metadata": {
"slideshow": {
"slide_type": "subslide"
},
"tags": []
},
"source": [
"### White space\n",
"\n",
"+ White space is more meaningful in python than other languages\n",
"+ This is actually useful, as it forces good indentation practices\n",
"\n",
"```\n",
"## Some more about white space\n",
"if statementA:\n",
" statementB # Executed if statementA is True\n",
"statementC # Executed regardless since it's not indented\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "19bd82bb-bc6f-4b5a-8436-a6077302b547",
"metadata": {
"slideshow": {
"slide_type": "subslide"
},
"tags": []
},
"source": [
"### General if statment structure\n",
"\n",
"\n",
"```\n",
"if statement1 :\n",
" ...\n",
"elif statement2 :\n",
" ...\n",
"else \n",
" ...\n",
"```\n",
"Here's an example (note this is just equal to the statement `(a < 0) - (a > 0)`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4afd61f3-3fb6-471a-b14a-c188a7de854c",
"metadata": {},
"outputs": [],
"source": [
"a = 5\n",
"\n",
"if a < 0 :\n",
" a = -1\n",
"elif a > 0 :\n",
" a = 1\n",
"else :\n",
" a = 0\n",
"\n",
"print(a)"
]
},
{
"cell_type": "markdown",
"id": "bd30d824-8081-4369-be21-b2b12d16f126",
"metadata": {
"slideshow": {
"slide_type": "slide"
},
"tags": []
},
"source": [
"## for and while loops"
]
},
{
"cell_type": "markdown",
"id": "cacc1a7f-1ee0-4aed-a49f-5f93069c1b09",
"metadata": {
"slideshow": {
"slide_type": "subslide"
},
"tags": []
},
"source": [
"### looping in python\n",
"\n",
"+ Python has for and while loops \n",
"+ for loops will iterate over most objects for which it makes sense\n",
"+ Don't forget the indentation or the `:`\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "fa4cbf09-07ff-469e-82ee-48ad2b4c2c64",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t\n",
"e\n",
"s\n",
"t\n"
]
}
],
"source": [
"for i in \"test\":\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e28a1bb2-141d-4944-aebe-d521e4c165a3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2]\n",
"[3, 4]\n"
]
}
],
"source": [
"for i in [[1, 2], [3, 4]]:\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"id": "a0fec0d4-a6dd-4bca-b059-2ee2eee329aa",
"metadata": {
"slideshow": {
"slide_type": "subslide"
},
"tags": []
},
"source": [
"### range\n",
"+ range is a function that creates an iterable list of integers\n",
"+ `range(4)` is not itself a list, but `list(range(4))` is"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "aa749189-53a3-4698-9d3a-0cb0d2c2a8f4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'range'>\n",
"<class 'list'>\n"
]
}
],
"source": [
"print(type(range(4)))\n",
"print(type(list(range(4))))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0f3c2311-672b-4728-89c3-8ac96ebb72d7",
"metadata": {},
"outputs": [],
"source": [
"for i in range(4) :\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"id": "315beabe-a999-4b5d-84ea-9f7259f7d7b8",
"metadata": {
"slideshow": {
"slide_type": "subslide"
},
"tags": []
},
"source": [
"### While loop example"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "1d14a8bf-42dd-4018-bdf1-239262248ed2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"2\n",
"1\n",
"0\n"
]
}
],
"source": [
"x = 4\n",
"while x > 0 :\n",
" x = x - 1\n",
" print(x)"
]
}
],
"metadata": {
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 5c07086

Please sign in to comment.