Skip to content

Commit

Permalink
python_programming and nns
Browse files Browse the repository at this point in the history
  • Loading branch information
B7M committed Jun 25, 2024
1 parent 5c07086 commit b9d0f7d
Show file tree
Hide file tree
Showing 32 changed files with 33,601 additions and 62 deletions.
189 changes: 163 additions & 26 deletions slides/summer_institute/04_python_progamming.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,8 @@
"tags": []
},
"source": [
"## Control flow"
]
},
{
"cell_type": "markdown",
"id": "ebbd20d1-9176-4d2b-9873-ce6cf51131f9",
"metadata": {
"slideshow": {
"slide_type": "subslide"
},
"tags": []
},
"source": [
"## Control flow\n",
"\n",
"### If statements\n",
"\n",
"+ Python has if statements like most programming languages\n",
Expand All @@ -46,7 +35,11 @@
"cell_type": "code",
"execution_count": 2,
"id": "f33a408a-84cd-4b5a-8511-0cfea531383a",
"metadata": {},
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
Expand All @@ -67,6 +60,19 @@
" print(\"Gryffindor\")"
]
},
{
"cell_type": "markdown",
"id": "ebbd20d1-9176-4d2b-9873-ce6cf51131f9",
"metadata": {
"slideshow": {
"slide_type": "notes"
},
"tags": []
},
"source": [
"Control flow in Python is essential for making decisions in your code. Python’s if statements work similarly to those in other programming languages but pay special attention to indentation and the use of the colon. The indentation defines the block of code to be executed if the condition is true. Proper indentation is crucial as it ensures the code runs as expected. Let's look at a simple example to see how if statements control the flow based on conditions let's assume a user input stored in x. The if statement checks if x is equal to 'y'. If true, it prints 'Slytherine!', otherwise, it prints 'Gryffindor'. "
]
},
{
"cell_type": "markdown",
"id": "c6e719b7-882c-4441-86fa-42d3e2418dcc",
Expand All @@ -90,6 +96,18 @@
"```"
]
},
{
"cell_type": "markdown",
"id": "3157dc1f",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Whitespace is more meaningful in Python compared to other programming languages. This feature enforces good indentation practices, improving code readability. For example, in the following pseudo-code if statement A is true, statement B is executed due to its indentation. However, statement C is executed regardless of statement A because it’s not indented under the if block. Proper use of whitespace ensures clear, maintainable code structure."
]
},
{
"cell_type": "markdown",
"id": "19bd82bb-bc6f-4b5a-8436-a6077302b547",
Expand All @@ -100,7 +118,7 @@
"tags": []
},
"source": [
"### General if statment structure\n",
"### General if statement structure\n",
"\n",
"\n",
"```\n",
Expand All @@ -110,17 +128,33 @@
" ...\n",
"else \n",
" ...\n",
"```\n",
"Here's an example (note this is just equal to the statement `(a < 0) - (a > 0)`"
"```\n"
]
},
{
"cell_type": "markdown",
"id": "3914fa70",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"The general structure of if statements in Python allows for multiple conditions to be checked sequentially using if, elif, and else. The basic format looks like this. Next slide will show an example of this structure in action."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4afd61f3-3fb6-471a-b14a-c188a7de854c",
"metadata": {},
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"source": [
"# note this is just equal to the statement `(a < 0) - (a > 0)`\n",
"a = 5\n",
"\n",
"if a < 0 :\n",
Expand All @@ -133,6 +167,18 @@
"print(a)"
]
},
{
"cell_type": "markdown",
"id": "e04c965d",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"In this example, Python checks each condition in order. If a is greater than 0, it prints Positive. If a is less than 0, it prints Negative. If neither condition is met, it defaults to printing Zero. This structure provides a clear and organized way to handle multiple conditional scenarios."
]
},
{
"cell_type": "markdown",
"id": "bd30d824-8081-4369-be21-b2b12d16f126",
Expand Down Expand Up @@ -163,11 +209,27 @@
"+ Don't forget the indentation or the `:`\n"
]
},
{
"cell_type": "markdown",
"id": "ed7a9d20",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"Python supports both for and while loops for iterative operations. For loops can iterate over various objects such as lists, tuples, strings, and more. Remember to use proper indentation and a colon (:) to define the loop body. Following slide shows two examples of for loops in Python."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "fa4cbf09-07ff-469e-82ee-48ad2b4c2c64",
"metadata": {},
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
Expand All @@ -189,7 +251,11 @@
"cell_type": "code",
"execution_count": 4,
"id": "e28a1bb2-141d-4944-aebe-d521e4c165a3",
"metadata": {},
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
Expand All @@ -205,6 +271,18 @@
" print(i)"
]
},
{
"cell_type": "markdown",
"id": "a0127a84",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"In the first example, the for loop iterates over each character in the string \"test\", printing each character individually. In second example, the for loop iterates over a list of lists. Each iteration prints one of the inner lists. This demonstrates the flexibility of for loops in Python, allowing them to work with strings, lists, and other iterable objects. As always, proper indentation and the use of a colon are crucial."
]
},
{
"cell_type": "markdown",
"id": "a0fec0d4-a6dd-4bca-b059-2ee2eee329aa",
Expand All @@ -224,7 +302,11 @@
"cell_type": "code",
"execution_count": 14,
"id": "aa749189-53a3-4698-9d3a-0cb0d2c2a8f4",
"metadata": {},
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
Expand All @@ -240,12 +322,39 @@
"print(type(list(range(4))))"
]
},
{
"cell_type": "markdown",
"id": "27a1ce6f",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"The range function in Python is used to generate a sequence of integers. It creates an iterable, but it’s not a list itself. For example, range(4) generates an iterable that represents the sequence 0, 1, 2, 3, but it’s not a list. To convert it to a list, you can use list(range(4)). This function is particularly useful in for loops when you need to iterate a specific number of times. The following example demonstrates how to use range in a for loop."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "0f3c2311-672b-4728-89c3-8ac96ebb72d7",
"metadata": {},
"outputs": [],
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"for i in range(4) :\n",
" print(i)"
Expand All @@ -264,11 +373,27 @@
"### While loop example"
]
},
{
"cell_type": "markdown",
"id": "b651e825",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"A while loop in Python repeatedly executes a block of code as long as a specified condition is true. It's useful for scenarios where the number of iterations isn't predetermined. The loop begins by evaluating the condition; if it's true, the code within the loop executes. After executing the code block, the condition is re-evaluated, and if it remains true, the loop continues. This cycle repeats until the condition evaluates to false, at which point the loop terminates and control passes to the next statement following the loop. To avoid infinite loops, ensure that the loop's condition will eventually become false, typically by updating a variable within the loop. "
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "1d14a8bf-42dd-4018-bdf1-239262248ed2",
"metadata": {},
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
Expand All @@ -287,6 +412,18 @@
" x = x - 1\n",
" print(x)"
]
},
{
"cell_type": "markdown",
"id": "1fbaa9ef",
"metadata": {
"slideshow": {
"slide_type": "notes"
}
},
"source": [
"This example demonstrates the basic structure and functionality of a while loop. In this loop, we start with x equal to 4. The while loop continues as long as x is greater than 0. Inside the loop, x is decremented by 1 each iteration, and the new value of x is printed. This loop will print the values 3, 2, 1, and 0 before exiting, as the condition x > 0 will no longer be true once x reaches 0. "
]
}
],
"metadata": {
Expand All @@ -305,7 +442,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
"version": "3.11.2"
}
},
"nbformat": 4,
Expand Down
9 changes: 3 additions & 6 deletions slides/summer_institute/06_data_cleaning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1876,13 +1876,10 @@
"toc_visible": true,
"version": "0.3.2"
},
"interpreter": {
"hash": "625a8f875bfb3f569e4f618df17e1f8389970b6b26ee2c84acb92c5fbebf95c3"
},
"kernelspec": {
"display_name": "Python [conda env:.conda-ds4bio]",
"display_name": "RL",
"language": "python",
"name": "conda-env-.conda-ds4bio-py"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -1894,7 +1891,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
"version": "3.11.2"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit b9d0f7d

Please sign in to comment.