From 82b04ec535c40323c61113af3740da8bdd590234 Mon Sep 17 00:00:00 2001 From: GURDITKING4 Date: Sat, 13 Sep 2025 14:02:27 +0100 Subject: [PATCH 1/6] my lab --- your-code/main.ipynb | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index de27676..0c7ae8a 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -9,7 +9,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -25,11 +25,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "GS\n" + ] + } + ], "source": [ - "# your code here" + "# your code here\n", + "print(\"GS\")" ] }, { @@ -41,7 +50,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -57,7 +66,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -228,7 +237,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -242,7 +251,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" }, "toc": { "base_numbering": 1, @@ -285,11 +294,6 @@ "_Feature" ], "window_display": false - }, - "vscode": { - "interpreter": { - "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" - } } }, "nbformat": 4, From 7565bd51f63e17b53bdcb72d6e15c19003def1a7 Mon Sep 17 00:00:00 2001 From: GURDITKING4 Date: Sat, 13 Sep 2025 14:06:08 +0100 Subject: [PATCH 2/6] lab submission --- your-code/main.ipynb | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index 0c7ae8a..fe4ad74 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -52,9 +52,26 @@ "cell_type": "code", "execution_count": 3, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "play\n", + "filling\n", + "bar\n", + "theatre\n", + "easygoing\n", + "date\n", + "lead\n", + "island\n" + ] + } + ], "source": [ - "# your code here" + "my_list = ['play', 'filling', 'bar', 'theatre', 'easygoing', 'date', 'lead', 'island']\n", + "for item in my_list:\n", + " print(item)" ] }, { @@ -66,11 +83,24 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 8, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "island\n" + ] + } + ], "source": [ - "# your code here" + "words = ['play', 'filling', 'bar', 'theatre', 'easygoing', 'date', 'lead', 'that', 'story', 'island']\n", + "for word in words:\n", + " if word.startswith(\"i\"):\n", + " print(word)\n", + " break\n", + " " ] }, { From 188c9992771b87c4ab2252bca53ec84657f6e450 Mon Sep 17 00:00:00 2001 From: GURDITKING4 Date: Thu, 18 Sep 2025 07:40:40 +0100 Subject: [PATCH 3/6] Add files via upload --- 2_Flow_Control_I.ipynb | 1680 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1680 insertions(+) create mode 100644 2_Flow_Control_I.ipynb diff --git a/2_Flow_Control_I.ipynb b/2_Flow_Control_I.ipynb new file mode 100644 index 0000000..a5547fa --- /dev/null +++ b/2_Flow_Control_I.ipynb @@ -0,0 +1,1680 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "b0f6ad4a", + "metadata": {}, + "source": [ + "# Flow Control 1" + ] + }, + { + "cell_type": "markdown", + "id": "subjective-quarter", + "metadata": { + "toc": true + }, + "source": [ + "

Table of Contents

\n", + "" + ] + }, + { + "cell_type": "markdown", + "id": "63c618c2", + "metadata": {}, + "source": [ + "![](https://media.makeameme.org/created/im-just-going-ca4ce2c899.jpg)" + ] + }, + { + "cell_type": "markdown", + "id": "143f947d", + "metadata": { + "lang": "en" + }, + "source": [ + "## Arithmetic operators" + ] + }, + { + "cell_type": "markdown", + "id": "9aecbca0", + "metadata": { + "lang": "en" + }, + "source": [ + "Arithmetic operators are used to perform arithmetic operations, that is, to manipulate numerical data through mathematical operations such as addition, subtraction or multiplication..." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "8b428e9f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = 10\n", + "b = 2\n", + "\n", + "a + b\n", + "a * b" + ] + }, + { + "cell_type": "markdown", + "id": "7dae48c3", + "metadata": {}, + "source": [ + "`+`, `*`, and all others" + ] + }, + { + "cell_type": "markdown", + "id": "e6aaec42", + "metadata": { + "lang": "en" + }, + "source": [ + "## Assignment operators (Python tricks)" + ] + }, + { + "cell_type": "markdown", + "id": "e3982cf4", + "metadata": {}, + "source": [ + "![](https://i.pinimg.com/236x/2b/dd/c9/2bddc91accccb9b70178c49cf5684bed.jpg)" + ] + }, + { + "cell_type": "markdown", + "id": "3e1e56bd", + "metadata": { + "lang": "en" + }, + "source": [ + "Assignment operators are those that allow you to give a value to a variable or modify it. Python has eight different assignment operators: a simple assignment operator and seven compound assignment operators." + ] + }, + { + "cell_type": "markdown", + "id": "fc5d346e", + "metadata": { + "lang": "en" + }, + "source": [ + "* `=` Simple assignment `a=b`\n", + "* `+=` Addition assignment `a+=b` Simple equivalent `a=a+b`\n", + "* `-=` Subtraction assignment `a-=b` Simple equivalent `a=a-b`\n", + "* `*=` Multiplication Assignment `a*=b` Simple Equivalent `a=a*b`\n", + "* `/=` Division assignment `a/=b` Simple equivalent `a=a/b`\n", + "* `%=` Module mapping `a/=b` Simple equivalent `a=a%b`\n", + "* `//=` Integer Division Assignment `a//=b` Simple Equivalent `a=a//b`\n", + "\n", + "(It is important that you know this, but remember python principles....)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "7dba4669", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#a = a + 1\n", + "a =+ 1\n", + "a" + ] + }, + { + "cell_type": "markdown", + "id": "89b42f24", + "metadata": { + "lang": "en" + }, + "source": [ + "The simple assignment operator is the equals symbol (=) and operations performed on it always have the syntax: `variable = expression`. In this type of operation, first, the expression on the right is resolved and the resulting value is assigned to the variable on the left." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "709c2301", + "metadata": {}, + "outputs": [], + "source": [ + "# Take some time, and practice" + ] + }, + { + "cell_type": "markdown", + "id": "71a92117", + "metadata": { + "lang": "en" + }, + "source": [ + "## Relational operators" + ] + }, + { + "cell_type": "markdown", + "id": "38d5146c", + "metadata": { + "lang": "en" + }, + "source": [ + "\n", + "Relational operators are symbols used to compare two values ​​or expressions. The result of the evaluation with these two operators can be True, if the comparison is true, or False, if the comparison is false." + ] + }, + { + "cell_type": "markdown", + "id": "b7d7c2b5", + "metadata": { + "lang": "en" + }, + "source": [ + "* `==` Equal to `a==b`\n", + "* `!=` Not equal to `a!=b`\n", + "* `>` Greater than `a>b`\n", + "* `<` Lesser than `a=` Greater than or equal to `a>=b`\n", + "* `<=` Less than or equal to `a<=b`" + ] + }, + { + "cell_type": "markdown", + "id": "6bb2bc2a", + "metadata": { + "lang": "en" + }, + "source": [ + "Note the difference between a single equals sign (=), which is an assignment, and a double equals sign (==), which is a relational operator\n", + "\n", + "**Hint: Use parentheses to maintain the order of operations." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ac9ccfb3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# What is the difference between these two?\n", + "a = 10\n", + "a == 10" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "c00bf648", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a != 10" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "3dde583b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a > b" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "4efdcce8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a < b" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "16a5b605", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = (a <= b)\n", + "x" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "de053fec", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y = (b <= 20)\n", + "y" + ] + }, + { + "cell_type": "markdown", + "id": "5753abc4", + "metadata": {}, + "source": [ + "### Boolean Algebra" + ] + }, + { + "cell_type": "markdown", + "id": "0bf8a056", + "metadata": { + "lang": "en" + }, + "source": [ + "With just these two `True`|`False` values ​​we can create a whole branch of mathematics called [Boolean Algebra](https://en.wikipedia.org/wiki/Boolean_algebra#Laws). While in regular Algebra the basic operations are addition and multiplication, the main operations in Boolean Algebra are conjunction (and), disjunction (or), and negation (not). **It is the formalism used to describe the logical operations**.\n", + "\n", + "In [Python we write these operations](https://www.geeksforgeeks.org/python-3-logical-operators/?ref=rp) as:\n", + "\n", + "- `==`\n", + "* `x` and `y`\n", + "* `x` or `y`\n", + "* not `x`\n", + "* Extra: `^` [caret operator](https://stackoverflow.com/questions/2451386/what-does-the-caret-operator-do)\n", + "\n", + "Although the meaning of these operations is clear, we can fully define them with the so-called \"truth table\":" + ] + }, + { + "cell_type": "markdown", + "id": "coral-armenia", + "metadata": {}, + "source": [ + "![tablas](http://elclubdelautodidacta.es/wp/wp-content/uploads/2012/11/python-logicos.png)" + ] + }, + { + "cell_type": "markdown", + "id": "c39be317", + "metadata": { + "lang": "en" + }, + "source": [ + "We can also include the EXCLUSIVE OR, which is true when one and only one of the operands is true, but strictly you must know that it is derived from the three basic ones. Its representation is ^, the hat or caret." + ] + }, + { + "cell_type": "markdown", + "id": "244fb7c7", + "metadata": {}, + "source": [ + "### **equals** vs **is**" + ] + }, + { + "cell_type": "markdown", + "id": "c2fc6ff2", + "metadata": {}, + "source": [ + "[Article on Equal vs Is](https://towardsdatascience.com/whats-the-difference-between-is-and-in-python-dc26406c85ad)" + ] + }, + { + "cell_type": "markdown", + "id": "e8287044", + "metadata": {}, + "source": [ + "`==` -> points to the value
\n", + "`is` -> looks for identity" + ] + }, + { + "cell_type": "markdown", + "id": "1c4f6e09", + "metadata": {}, + "source": [ + "| Operator | Compares | Returns `True` if |\n", + "|----------|------------------|-----------------------------|\n", + "| `==` | Values | Values are equal |\n", + "| `is` | Object identity | Both refer to same object |\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "873ada8d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "# Example 1: Comparing Numbers\n", + "a = 5\n", + "b = 5\n", + "\n", + "print(a == b) # True, because a and b are the same number" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "63b2eb26", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "print(a is b)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "5a1aa6df", + "metadata": {}, + "outputs": [], + "source": [ + "# Example 2: comparing lists\n", + "list1 = [1, 2, 3]\n", + "list2 = [1, 2, 3]" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "f714c908", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "print(list1 == list2) # True, because the content of the list is the same" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "5a5f1e05", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "print(list1 is list2) # False, because list1 and list2 are two different objects" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "44e6eefc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "a = 5\n", + "b = 5\n", + "\n", + "c = 5\n", + "\n", + "a = c\n", + "b = c\n", + "\n", + "\n", + "print(a is b) # True? yes, but wierd: a and b point to the same \"thing\" due to an optimization in python" + ] + }, + { + "cell_type": "markdown", + "id": "29d1ae12", + "metadata": {}, + "source": [ + "## NOT\n", + "\n", + "- The **not** operator. The **not** operator is an operator that returns the opposite value of the evaluated expression. If the expression has the value True, it returns False. Conversely, if the expression has the value False, it returns True." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "70ecf121", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "not(list1 == list2)" + ] + }, + { + "cell_type": "markdown", + "id": "d5122028", + "metadata": { + "lang": "en" + }, + "source": [ + "## AND\n", + "- The **and** operator evaluates whether both expressions are true. If both expressions are true, it returns True. If either expression is false, it returns False. These types of tables are formally known as \"truth tables\"." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "6096259b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## TRUE FALSE\n", + "(list1 == list2) and (a == list2) " + ] + }, + { + "cell_type": "markdown", + "id": "ddb3ca4d", + "metadata": { + "lang": "en" + }, + "source": [ + "## OR\n", + "The **or** operator evaluates whether either expression is true, that is, it returns True if either expression is true and False when both expressions are false." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "3c409f34", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Same to (lista1 == lista2) | (a == lista2) \n", + "(list1 == list2) or (a == list2) " + ] + }, + { + "cell_type": "markdown", + "id": "ef5ced3e", + "metadata": {}, + "source": [ + "## EXERCISE" + ] + }, + { + "cell_type": "markdown", + "id": "e373cd72", + "metadata": {}, + "source": [ + "#### Exercise: Exploring Relational Operators\n", + "\n", + "In this exercise, we will explore the various relational operators available in Python. These operators are primarily used to establish relationships between different values and are essential in decision-making structures, although we won't be using them in that capacity in this exercise. Using these operators, please perform the following tasks:\n", + "\n", + "#### Task 1: Equal to Operator (==)\n", + "1. Create two strings and check if they are equal using `==`. Print the result.\n", + "2. Create two lists with equal elements and check if they are equal using `==`. Print the result.\n", + "\n", + "#### Task 2: Not Equal to Operator (!=)\n", + "1. Create two integers and check if they are not equal using `!=`. Print the result.\n", + "2. Use the `input()` function to get two inputs from the user and check if they are not equal using `!=`. Print the result.\n", + "\n", + "#### Task 3: Greater than and Lesser than Operators (>, <)\n", + "1. Create two floating-point numbers and check if one is greater than the other using `>`. Print the result.\n", + "2. Create two floating-point numbers and check if one is lesser than the other using `<`. Print the result.\n", + "\n", + "#### Task 4: Greater than or Equal to and Less than or Equal to Operators (>=, <=)\n", + "1. Create two integers and check if one is greater than or equal to the other using `>=`. Print the result.\n", + "2. Create two integers and check if one is less than or equal to the other using `<=`. Print the result." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "5de219ad", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n", + "False\n", + "True\n", + "False\n", + "True\n" + ] + } + ], + "source": [ + "# your code here\n", + "str1 = \"Hello\"\n", + "str2 = \"Bye\"\n", + "print(str1 == str2)\n", + "\n", + "int1 = 16\n", + "int2 = 25\n", + "print(int1 != int2)\n", + "\n", + "float1 = 24.55\n", + "float2 = 33.98\n", + "print(float1 > float2)\n", + "print(float1 < float2)\n", + "\n", + "int3 = 16\n", + "int4 = 25\n", + "print(int3 >= int4)\n", + "print(int3 <= int4)" + ] + }, + { + "cell_type": "markdown", + "id": "attended-assurance", + "metadata": {}, + "source": [ + "## Loops an stuff\n" + ] + }, + { + "cell_type": "markdown", + "id": "b0a9f8bc", + "metadata": {}, + "source": [ + "![](https://i1.sndcdn.com/artworks-SvzDuFfyt3AJ9fO1-iQrV9Q-t500x500.jpg)" + ] + }, + { + "cell_type": "markdown", + "id": "3e0ac95f", + "metadata": { + "lang": "en" + }, + "source": [ + "In programming, a **loop** is a fundamental concept that allows a block of code to be repeated multiple times. It facilitates the execution of repetitive tasks without needing to write the same lines of code over and over again, promoting both efficiency and readability. The basic idea is to loop through a block of code as long as a specified condition remains true.\n", + "\n", + "There are generally two types of loops you'll encounter:\n", + "\n", + "1. **For Loop**: Generally used to iterate over a collection of items, such as elements in a list, tuple, or string. It will execute a block of code for each item in the collection, hence making it simpler and cleaner to perform the same action on multiple items.\n", + "\n", + "2. **While Loop**: This type of loop continues to execute a block of code as long as a particular condition is met. It's crucial to update the variables influencing the condition inside the loop, to avoid creating an infinite loop.\n", + "\n", + "Loops are incredibly powerful tools in programming, helping to save time and reduce the potential for errors in your code. As you progress in your learning, you will find loops are essential components in many programming solutions, facilitating the creation of complex, efficient, and innovative solutions to a wide range of problems.\n", + "\n", + "In the following sections, we will explore both types of loops in more detail, with examples to illustrate their usage and versatility in programming tasks.\n", + "\n", + "Happy looping!" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "1d6224df", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'BANANA'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista= [\"melon\", \"apple\", \"banana\"]\n", + "lista[0].upper()\n", + "lista[1].upper()\n", + "lista[2].upper()" + ] + }, + { + "cell_type": "markdown", + "id": "aa20ffc4", + "metadata": {}, + "source": [ + "### Pseudocode" + ] + }, + { + "cell_type": "markdown", + "id": "c2a2b91b", + "metadata": { + "lang": "en" + }, + "source": [ + "Before we dive into the world of loops, let's take a moment to understand what \"Pseudocode\" is. Pseudocode is a high-level description of the functioning of a computer program or algorithm. It utilizes the structural conventions of programming languages but is designed to be read by humans rather than machines. \n", + "\n", + "In pseudocode, we are not concerned with precise syntax that a programming language might require. Instead, we focus on expressing the logic and the steps needed to solve a problem or accomplish a task, often in natural language mixed with some common programming structures. This means that pseudocode does not have a strict syntax and can vary widely.\n", + "\n", + "The main objective of writing pseudocode is to outline the structure of the program and delineate its logical flow without getting caught up in the specific details of a particular programming language. This makes it a useful tool for planning and designing programs, where the emphasis is on what to do rather than how to do it.\n", + "\n", + "Here are some general characteristics of pseudocode:\n", + "1. **Simplicity**: It should be simple and easy to understand.\n", + "2. **Clarity**: Each step is clearly defined without ambiguity.\n", + "3. **Language-independent**: It does not adhere to the syntax of any specific programming language.\n", + "4. **Focus on Logic**: The primary focus is on the logic and flow of the algorithm, rather than the specific syntax of a programming language.\n", + "\n", + "As you begin to work with loops and other programming structures, you'll find that creating pseudocode can be a valuable first step in the coding process, helping you to organize your thoughts and create a roadmap for your program before you start writing actual code." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "a1b077ff", + "metadata": {}, + "outputs": [], + "source": [ + "# a list of elements\n", + "# loop through the list \n", + "# and check if the type is a number\n", + "\n", + "# if the type is a number\n", + "# i do stuff\n", + "\n", + "# WHAT IS IT I HAVE?: LIST OF LISTS WITH NUMBERS\n", + " # original = [[1, 2], [3], [5, 6, 9]]\n", + " # 1. Create a new_list\n", + " # 2. GOAL: Take the element out\n", + " # 3. Access the element\n", + " # 4. loop original list: [1, 2]\n", + " # 5. loop the i/individual_list again\n", + " # 6: add to new_list\n", + " # 7: print\n", + " \n", + "# GOAL: WHAT RESULT I NEED TO RETURN\n", + " # [1, 2, 3, 4, 5, 6]\n", + " \n", + "# always print" + ] + }, + { + "cell_type": "markdown", + "id": "4690d4e0", + "metadata": {}, + "source": [ + "Quick infor card:\n", + "\n", + "In Python, the `range()` function is used to generate a sequence of numbers over time. It is widely used in loops to control the number of iterations. The function has three parameters: start, stop, and step.\n", + "\n", + "- **Start**: This parameter specifies the starting point of the sequence. It is optional, and the default value is 0.\n", + "- **Stop**: This parameter specifies the endpoint of the sequence. This value is not included in the sequence.\n", + "- **Step**: This parameter specifies the increment between each number in the sequence. It is optional, and the default value is 1.\n", + "\n", + "Here is the syntax for the `range()` function:\n", + "\n", + "- range(start, stop, step)\n", + "\n", + "Some example:" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "d024b13d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "range(1, 6)" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "range(1, 6)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "89f2329b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "Hi\n" + ] + } + ], + "source": [ + "for i in range(6):\n", + " print(i)\n", + "print(\"Hi\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "9fdcf991", + "metadata": {}, + "outputs": [], + "source": [ + "# play here with range" + ] + }, + { + "cell_type": "markdown", + "id": "fee3afff", + "metadata": {}, + "source": [ + "### Loop example\n", + "Let's do more complex stuff:" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "23dd3aa0", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2]\n", + "1\n", + "2\n", + "[3]\n", + "3\n", + "[5, 6, 9]\n", + "5\n", + "6\n", + "9\n", + "[1, 2, 3, 5, 6, 9]\n" + ] + } + ], + "source": [ + "# This is the original list of lists where each element is a list containing integers.\n", + "original = [[1, 2], [3], [5, 6, 9]]\n", + "# A new empty list is initialized to store the individual elements after flattening the original list.\n", + "new_list = []\n", + "\n", + "# The outer loop iterates through each individual list inside the original list.\n", + "for individual_list in original:\n", + " print(individual_list)\n", + " # The inner loop iterates through each element in the currently selected individual list (from the outer loop).\n", + " for each_element in individual_list:\n", + " # The current element from the individual list is appended to the new list.\n", + " print(each_element)\n", + " new_list.append(each_element)\n", + "\n", + "# Printing the new_list to see the final output.\n", + "print(new_list)" + ] + }, + { + "cell_type": "markdown", + "id": "8a77f835", + "metadata": {}, + "source": [ + "Debug what is happenin:" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "a36780d7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2]\n", + "[3]\n", + "[5, 6, 9, [1, 2]]\n" + ] + } + ], + "source": [ + "# We are defining a list named 'bigger_list' which contains several other lists, one of which is nested\n", + "bigger_list = [[1, 2], [3], [5, 6, 9, [1, 2]]]\n", + "\n", + "# We are initiating a for loop to iterate over each 'small_list' present within the 'bigger_list'\n", + "for small_list in bigger_list:\n", + " # Inside the loop, we are printing the current 'small_list' that the loop is iterating over\n", + " print(small_list)\n", + " # Once the end of the loop is reached, it goes back to the start to process the next 'small_list' in the 'bigger_list'" + ] + }, + { + "cell_type": "markdown", + "id": "202e988c", + "metadata": {}, + "source": [ + "### Exercise:" + ] + }, + { + "cell_type": "markdown", + "id": "66a5e23a", + "metadata": {}, + "source": [ + "You are working in the sales department of a company. At the end of each week, you need to generate a report of total sales made each day. You have a list where each element represents the sales made on each day of the week (from Monday to Sunday). Your task is to calculate the total sales of the week.\n", + "\n", + "To complete this exercise, you need to follow the given steps:\n", + "\n", + "1. **Initialize Variables**:\n", + " Before starting the loop, initialize a variable `total_sales` to 0. This will be used to accumulate the sales of each day. Additionally, initialize `max_sales` to 0.\n", + " \n", + " \n", + "2. **Creating a Loop**:\n", + " Create a for loop where you iterate over each element in the `sales_data` list. You can use a loop structure like this: for day in sales_data:\n", + " \n", + " \n", + "3. **Calculating Total Sales**:\n", + " Inside the loop, add the sales of the current day to `total_sales`. You can do this using an assignment statement like this: total_sales += day\n", + "\n", + "\n", + "\n", + "5. **Printing the Results**:\n", + " After the loop, print the `total_sales` along with appropriate messages to show the total sales of the week and the day with maximum sales." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "3bd7d85f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4500\n", + "1000\n" + ] + } + ], + "source": [ + "# List representing sales data for seven days (in units)\n", + "sales_data = [200, 300, 400, 500, 600, 700, 800,1000]\n", + "\n", + "# Initialize total_sales to 0 before starting the loop\n", + "total_sales = 0\n", + "# Initialize max_sales before starting the loop\n", + "max_sales = 0\n", + "\n", + "# {YOUR CODE HERE}\n", + "for day in sales_data:\n", + " #print(day)\n", + "\n", + " total_sales +=day\n", + " if day > max_sales:\n", + " max_sales = day\n", + "\n", + "print(total_sales)\n", + "print(max_sales)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "654bbe75", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1000" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max(sales_data )" + ] + }, + { + "cell_type": "markdown", + "id": "0eaad548", + "metadata": { + "lang": "en" + }, + "source": [ + "## The \"If\" statement\n", + "This is the most popular option to control the flow of a program. Conditions allow you to choose between different paths depending on the value of an expression. When you want to execute an action only when some condition is `True`, an \"if\" statement is used:" + ] + }, + { + "cell_type": "markdown", + "id": "580479b3", + "metadata": {}, + "source": [ + "#### if syntax:\n", + "- indentation: `tab / 5 spaces`\n", + "- colon: `:`\n" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "ef3ac3be", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Adult\n", + "True condition\n" + ] + } + ], + "source": [ + "age = 18\n", + "\n", + "if age >= 18:\n", + " print(\"Adult\")\n", + " print(\"True condition\")\n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "305e83a6", + "metadata": { + "lang": "en" + }, + "source": [ + "As we can see, the code block associated with the condition begins after the colon \":\", with an indentation that determines the code block. All statements belonging to the same block must have the same indentation. The block ends when the indent returns to the starting position of the if statement. Remember that Python uses indentation to identify blocks of code." + ] + }, + { + "cell_type": "markdown", + "id": "770fbd1a", + "metadata": { + "lang": "en" + }, + "source": [ + "### else\n", + "Sometimes you want if the condition is not met (and only then) another action is executed. They are mutually exclusive actions. This is achieved with the `else` statement." + ] + }, + { + "cell_type": "markdown", + "id": "median-injection", + "metadata": {}, + "source": [ + "![if](https://www.tutorialspoint.com/scala/images/scala_decision_making.jpg)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "5c7a1210", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not an adult\n" + ] + } + ], + "source": [ + "age = 17\n", + "\n", + "if age >= 18:\n", + " print(\"Adult\")\n", + "else:\n", + " print(\"Not an adult\")" + ] + }, + { + "cell_type": "markdown", + "id": "baa11a80", + "metadata": { + "lang": "en" + }, + "source": [ + "### elif\n", + "Sometimes there are more than two possibilities. Think of something like:\n", + "\"If Bob can get into the disco, let him in. If not, if he's over 16, recommend the nearby light disco. If not, send him home.\"\n", + "\n", + "We can achieve this with the `elif` clause and chained conditionals:" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "82badc29", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not Human\n" + ] + } + ], + "source": [ + "age = 130\n", + "\n", + "if age >= 130:\n", + " print(\"Not Human\")\n", + "\n", + "elif age >= 21:\n", + " print(\"adult\")\n", + " \n", + "elif age < 21:\n", + " print(\"Not an adult\")\n", + "\n", + "else:\n", + " print(\"It is good practice to have an else\")" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "8dfc81eb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not Human\n" + ] + } + ], + "source": [ + "age = 200\n", + "\n", + "if age >= 130:\n", + " print(\"Not Human\")\n", + "elif age >21:\n", + " print(\"Adult\")" + ] + }, + { + "cell_type": "markdown", + "id": "b3d55cd1", + "metadata": {}, + "source": [ + "### IF; ELIF; ELSE" + ] + }, + { + "cell_type": "markdown", + "id": "50d43411", + "metadata": {}, + "source": [ + "In Python programming, a nested if statement is an if statement that is the target of another if statement. Nested if statements mean an if statement inside another if statement. Yes, Python allows us to nest if statements within if statements. Here is the general form of a nested if statement." + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "36b1c6ee", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: ''", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[33], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# 1. Takes the input of a user (controlling it's an int)\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m age \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease input your age: \u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# 2. Based on conditions: return something or something else\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m age \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n", + "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: ''" + ] + } + ], + "source": [ + "# 1. Takes the input of a user (controlling it's an int)\n", + "age = int(input(\"Please input your age: \"))\n", + "\n", + "# 2. Based on conditions: return something or something else\n", + "if age > 0:\n", + " if age < 16:\n", + " print(\"You CANNOT get in\")\n", + "\n", + " elif age >= 16 and age < 18:\n", + " print(\"Alcohol free club\")\n", + "\n", + " elif age >= 18:\n", + " print(\"You can get in\")\n", + " \n", + "else:\n", + " print(\"Sorry, wrong info: negative value?\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f2fd273c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ok\n", + "startswith L\n", + "has letters\n", + "Name is Lola\n" + ] + } + ], + "source": [ + "# Assign a string which is intended to represent a password to the variable 'password'\n", + "password = \"12345678909876dcvbnhjytfgvb+\"\n", + "\n", + "# Check if the length of the password is greater than 8, if true, print \"ok\"\n", + "if len(password) > 8:\n", + " print(\"ok\")\n", + "\n", + "# Check if the password contains any letters, if true, print \"startswith L\"\n", + "# (Note: The current condition will always evaluate to True as it's checking if password is non-empty.\n", + "# To check for letters, you might need to modify the condition using any(x.isalpha() for x in password))\n", + "if password: # has letters\n", + " print(\"startswith L\")\n", + " \n", + "if any(x.isalpha() for x in password):\n", + " print(\"has letters\")\n", + " \n", + "# Check if the password contains any numbers, if true, print \"Name is Lola\"\n", + "# (Note: Similar to the previous note, the condition here checks if password is non-empty, \n", + "# to check for numbers you might want to modify the condition using any(x.isdigit() for x in password))\n", + "if password: # has numbers:\n", + " print(\"Name is Lola\")\n", + " \n", + "# (... modify the code for expected output\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3169c74a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "0\n", + "9\n", + "8\n", + "7\n", + "6\n", + "d\n", + "c\n", + "v\n", + "b\n", + "n\n", + "h\n", + "j\n", + "y\n", + "t\n", + "f\n", + "g\n", + "v\n", + "b\n", + "+\n" + ] + } + ], + "source": [ + "for x in password:\n", + " print(x)" + ] + }, + { + "cell_type": "markdown", + "id": "a62738d2", + "metadata": {}, + "source": [ + "### Exercise extension:\n", + "Now use if statements, to calculate on top of total sales, which day had the maximum sales." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "65c946ed", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The day with the max sales is 7 \n" + ] + } + ], + "source": [ + "# List representing sales data for seven days (in units)\n", + "sales_data = [200, 300, 400, 500, 600, 700, 800]\n", + "\n", + "# Initialize total_sales to 0 before starting the loop\n", + "total_sales = 0\n", + "\n", + "# Initialize max_sales before starting the loop\n", + "max_sales = 0\n", + "max_day=1\n", + "\n", + "# {YOUR CODE HERE}\n", + "for i in range (0,len(sales_data)):\n", + " if sales_data[i]>max_sales:\n", + " max_sales=sales_data[i]\n", + " max_day=i+1\n", + "print(f\"The day with the max sales is {max_day} \")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "4202238e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200\n", + "300\n", + "400\n", + "500\n", + "600\n", + "700\n", + "800\n", + "1000\n" + ] + } + ], + "source": [ + "max_sales = 0\n", + "for day in sales_data:\n", + " if day > max_sales:\n", + " max_sales = day\n", + " \n", + " print(max_sales)" + ] + }, + { + "cell_type": "markdown", + "id": "4119b21e", + "metadata": { + "lang": "en" + }, + "source": [ + "# Challenge 🤔\n", + "\n", + "Write a simple program that prints if bob can get into the club\n", + "for any two values ​​of the following variables:\n", + "`print(age_bob,age_minimum)`" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "5d8caf12", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "club\n" + ] + } + ], + "source": [ + "age_bob = 18\n", + "age_minimum = 16\n", + "\n", + "if age_bob >= age_minimum:\n", + " print(\"club\")\n", + "elif age_bob == 18:\n", + " print(\"cannot club\")\n", + "else:\n", + " print(\"cannot club\")" + ] + }, + { + "cell_type": "markdown", + "id": "a9b00fd8", + "metadata": { + "lang": "en" + }, + "source": [ + "### Your examples 🚀" + ] + }, + { + "cell_type": "markdown", + "id": "0833207b", + "metadata": {}, + "source": [ + "Modify your schedule so that when Bob can't get into the club but is over the legal age (age=21)\n", + "send it to disco light" + ] + }, + { + "cell_type": "markdown", + "id": "5f01cb54", + "metadata": { + "lang": "en" + }, + "source": [ + "# Challenge 🤔\n" + ] + }, + { + "cell_type": "markdown", + "id": "b217ff64", + "metadata": { + "lang": "en" + }, + "source": [ + "Let's imagine that we are building the program for a robot that sorts eggs by size. Our robot arm receives information from a scale, which indicates, in grams, the weight of the egg to be classified. The arm must, from the weight, place the egg in one box or another as follows:\n", + "\n", + "- **Box S** (small): weight less than 53 grams.\n", + "- **Box M** (medium) : weight greater than or equal to 53 grams and less than 63 grams.\n", + "- **Box L** (large): weight greater than or equal to 63 grams and less than 73 grams.\n", + "- **XL box** (super large): weight greater than or equal to 73 grams." + ] + }, + { + "cell_type": "markdown", + "id": "4ed9f405", + "metadata": {}, + "source": [] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "d61dba2f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Box L (large)\n" + ] + } + ], + "source": [ + "weight = 70 # in kilograms\n", + "if weight < 53:\n", + " print(\"Box S (small)\")\n", + "elif weight < 63:\n", + " print(\"Box M (medium)\")\n", + "elif weight < 73:\n", + " print(\"Box L (large)\") \n", + "else:\n", + " print(\"Box XL (extra large)\")" + ] + }, + { + "cell_type": "markdown", + "id": "61cfbb7c", + "metadata": {}, + "source": [ + "## Summary\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4524a456", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "7deb669c", + "metadata": {}, + "source": [ + "[Rubber duck debugging](https://www.freecodecamp.org/news/rubber-duck-debugging/#:~:text=The%20idea%20behind%20the%20rubber,loud%20before%20you%20post%20it)\n", + "\n", + "The \"Rubber Duck Theory\" or \"Rubber Duck Debugging\" is a method used to find errors or bugs in a code. In this technique, the programmer explains their code, line by line, to an inanimate object such as a rubber duck. The act of explaining the code in detail often helps the programmer to see errors or improvements that they didn't notice before. This is similar to writing a message on social media and then reading it aloud before posting it, which can sometimes help in spotting errors or reconsidering the message's content." + ] + }, + { + "cell_type": "markdown", + "id": "toxic-romance", + "metadata": {}, + "source": [ + "## Further materials\n", + "\n", + "* [Python docs](https://docs.python.org/3/tutorial/controlflow.html)\n", + "* Short tutorial on [Python Booleans](https://realpython.com/python-boolean/)\n", + "* Just a nice [talk by Feynman](https://www.youtube.com/watch?v=EKWGGDXe5MA) on the principles of computation" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.13.5" + }, + "nbTranslate": { + "displayLangs": [ + "en", + "es" + ], + "hotkey": "alt-a", + "langInMainMenu": true, + "sourceLang": "es", + "targetLang": "en", + "useGoogleTranslate": true + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": true, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": true, + "toc_position": { + "height": "calc(100% - 180px)", + "left": "10px", + "top": "150px", + "width": "165px" + }, + "toc_section_display": true, + "toc_window_display": true + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 0aeec0c98b126193ba4571cd47e0f7f4a3f348d9 Mon Sep 17 00:00:00 2001 From: GURDITKING4 Date: Fri, 19 Sep 2025 16:00:13 +0100 Subject: [PATCH 4/6] lab submission --- your-code/main.ipynb | 117 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 97 insertions(+), 20 deletions(-) diff --git a/your-code/main.ipynb b/your-code/main.ipynb index fe4ad74..eca95fd 100644 --- a/your-code/main.ipynb +++ b/your-code/main.ipynb @@ -123,7 +123,25 @@ "metadata": {}, "outputs": [], "source": [ - "# your code here" + "list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n" + ] + } + ], + "source": [ + "squares_1_to_10 = [x**2 for x in range(1, 11)]\n", + "print(squares_1_to_10)" ] }, { @@ -135,11 +153,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 9, 25, 49, 81]\n" + ] + } + ], "source": [ - "# your code here" + "odd_squares_1_to_10 = [x**2 for x in range(1, 10) if x % 2 != 0]\n", + "print(odd_squares_1_to_10)" ] }, { @@ -151,11 +178,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[64, 256, 576, 1024, 1600, 2304, 3136, 4096, 5184, 6400, 7744, 9216, 10816, 12544, 14400, 16384, 18496, 20736, 23104, 25600, 28224, 30976, 33856, 36864, 40000, 43264, 46656, 50176, 53824, 57600, 61504, 65536, 69696, 73984, 78400, 82944, 87616, 92416, 97344, 102400, 107584, 112896, 118336, 123904, 129600, 135424, 141376, 147456, 153664, 160000, 166464, 173056, 179776, 186624, 193600, 200704, 207936, 215296, 222784, 230400, 238144, 246016, 254016, 262144, 270400, 278784, 287296, 295936, 304704, 313600, 322624, 331776, 341056, 350464, 360000, 369664, 379456, 389376, 399424, 409600, 419904, 430336, 440896, 451584, 462400, 473344, 484416, 495616, 506944, 518400, 529984, 541696, 553536, 565504, 577600, 589824, 602176, 614656, 627264, 640000, 652864, 665856, 678976, 692224, 705600, 719104, 732736, 746496, 760384, 774400, 788544, 802816, 817216, 831744, 846400, 861184, 876096, 891136, 906304, 921600, 937024, 952576, 968256, 984064]\n" + ] + } + ], "source": [ - "# your code here" + "squares_mult_8 = [x**2 for x in range(8, 1000, 8)]\n", + "print(squares_mult_8)" ] }, { @@ -167,7 +203,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -209,11 +245,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "How many people are there? 5\n" + ] + } + ], "source": [ - "# your code here" + "num_people = len(people)\n", + "print(\"How many people are there?\", num_people)" ] }, { @@ -225,11 +270,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "How many people have kids? 4\n" + ] + } + ], "source": [ - "# your code here" + "num_with_kids = sum(1 for person in people if person[\"n_kids\"] > 0)\n", + "print(\"How many people have kids?\", num_with_kids)" ] }, { @@ -241,11 +295,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "How many kids do they have in total? 10\n" + ] + } + ], "source": [ - "# your code here" + "total_kids = sum(person[\"n_kids\"] for person in people)\n", + "print(\"How many kids do they have in total?\", total_kids)" ] }, { @@ -257,11 +320,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "in a year's time, people info:\n", + "[{'name': 'Juan', 'age': 34, 'n_kids': 2}, {'name': 'Pepe', 'age': 27, 'n_kids': 0}, {'name': 'Sonia', 'age': 41, 'n_kids': 2}, {'name': 'Lucía', 'age': 22, 'n_kids': 3}, {'name': 'Leo', 'age': 55, 'n_kids': 5}]\n" + ] + } + ], "source": [ - "# your code here" + "updated_people = [\n", + " {**person, \"n_kids\": person[\"n_kids\"] + 1 if person[\"name\"].endswith(\"a\") else person[\"n_kids\"]}\n", + " for person in people\n", + "]\n", + "print(\"in a year's time, people info:\")\n", + "print(updated_people)" ] } ], From f49886b1467559151ecf15f5f35be0b563645656 Mon Sep 17 00:00:00 2001 From: gurdit singh mlds4 Date: Fri, 19 Sep 2025 17:29:58 +0100 Subject: [PATCH 5/6] Add files via upload --- 2_Flow_Control_I.ipynb | 3360 ++++++++++++++++++++-------------------- 1 file changed, 1680 insertions(+), 1680 deletions(-) diff --git a/2_Flow_Control_I.ipynb b/2_Flow_Control_I.ipynb index a5547fa..dbf61a6 100644 --- a/2_Flow_Control_I.ipynb +++ b/2_Flow_Control_I.ipynb @@ -1,1680 +1,1680 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "b0f6ad4a", - "metadata": {}, - "source": [ - "# Flow Control 1" - ] - }, - { - "cell_type": "markdown", - "id": "subjective-quarter", - "metadata": { - "toc": true - }, - "source": [ - "

Table of Contents

\n", - "" - ] - }, - { - "cell_type": "markdown", - "id": "63c618c2", - "metadata": {}, - "source": [ - "![](https://media.makeameme.org/created/im-just-going-ca4ce2c899.jpg)" - ] - }, - { - "cell_type": "markdown", - "id": "143f947d", - "metadata": { - "lang": "en" - }, - "source": [ - "## Arithmetic operators" - ] - }, - { - "cell_type": "markdown", - "id": "9aecbca0", - "metadata": { - "lang": "en" - }, - "source": [ - "Arithmetic operators are used to perform arithmetic operations, that is, to manipulate numerical data through mathematical operations such as addition, subtraction or multiplication..." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "8b428e9f", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "20" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = 10\n", - "b = 2\n", - "\n", - "a + b\n", - "a * b" - ] - }, - { - "cell_type": "markdown", - "id": "7dae48c3", - "metadata": {}, - "source": [ - "`+`, `*`, and all others" - ] - }, - { - "cell_type": "markdown", - "id": "e6aaec42", - "metadata": { - "lang": "en" - }, - "source": [ - "## Assignment operators (Python tricks)" - ] - }, - { - "cell_type": "markdown", - "id": "e3982cf4", - "metadata": {}, - "source": [ - "![](https://i.pinimg.com/236x/2b/dd/c9/2bddc91accccb9b70178c49cf5684bed.jpg)" - ] - }, - { - "cell_type": "markdown", - "id": "3e1e56bd", - "metadata": { - "lang": "en" - }, - "source": [ - "Assignment operators are those that allow you to give a value to a variable or modify it. Python has eight different assignment operators: a simple assignment operator and seven compound assignment operators." - ] - }, - { - "cell_type": "markdown", - "id": "fc5d346e", - "metadata": { - "lang": "en" - }, - "source": [ - "* `=` Simple assignment `a=b`\n", - "* `+=` Addition assignment `a+=b` Simple equivalent `a=a+b`\n", - "* `-=` Subtraction assignment `a-=b` Simple equivalent `a=a-b`\n", - "* `*=` Multiplication Assignment `a*=b` Simple Equivalent `a=a*b`\n", - "* `/=` Division assignment `a/=b` Simple equivalent `a=a/b`\n", - "* `%=` Module mapping `a/=b` Simple equivalent `a=a%b`\n", - "* `//=` Integer Division Assignment `a//=b` Simple Equivalent `a=a//b`\n", - "\n", - "(It is important that you know this, but remember python principles....)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "7dba4669", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "#a = a + 1\n", - "a =+ 1\n", - "a" - ] - }, - { - "cell_type": "markdown", - "id": "89b42f24", - "metadata": { - "lang": "en" - }, - "source": [ - "The simple assignment operator is the equals symbol (=) and operations performed on it always have the syntax: `variable = expression`. In this type of operation, first, the expression on the right is resolved and the resulting value is assigned to the variable on the left." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "709c2301", - "metadata": {}, - "outputs": [], - "source": [ - "# Take some time, and practice" - ] - }, - { - "cell_type": "markdown", - "id": "71a92117", - "metadata": { - "lang": "en" - }, - "source": [ - "## Relational operators" - ] - }, - { - "cell_type": "markdown", - "id": "38d5146c", - "metadata": { - "lang": "en" - }, - "source": [ - "\n", - "Relational operators are symbols used to compare two values ​​or expressions. The result of the evaluation with these two operators can be True, if the comparison is true, or False, if the comparison is false." - ] - }, - { - "cell_type": "markdown", - "id": "b7d7c2b5", - "metadata": { - "lang": "en" - }, - "source": [ - "* `==` Equal to `a==b`\n", - "* `!=` Not equal to `a!=b`\n", - "* `>` Greater than `a>b`\n", - "* `<` Lesser than `a=` Greater than or equal to `a>=b`\n", - "* `<=` Less than or equal to `a<=b`" - ] - }, - { - "cell_type": "markdown", - "id": "6bb2bc2a", - "metadata": { - "lang": "en" - }, - "source": [ - "Note the difference between a single equals sign (=), which is an assignment, and a double equals sign (==), which is a relational operator\n", - "\n", - "**Hint: Use parentheses to maintain the order of operations." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "ac9ccfb3", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# What is the difference between these two?\n", - "a = 10\n", - "a == 10" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "c00bf648", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a != 10" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "3dde583b", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a > b" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "4efdcce8", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a < b" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "16a5b605", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x = (a <= b)\n", - "x" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "de053fec", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "y = (b <= 20)\n", - "y" - ] - }, - { - "cell_type": "markdown", - "id": "5753abc4", - "metadata": {}, - "source": [ - "### Boolean Algebra" - ] - }, - { - "cell_type": "markdown", - "id": "0bf8a056", - "metadata": { - "lang": "en" - }, - "source": [ - "With just these two `True`|`False` values ​​we can create a whole branch of mathematics called [Boolean Algebra](https://en.wikipedia.org/wiki/Boolean_algebra#Laws). While in regular Algebra the basic operations are addition and multiplication, the main operations in Boolean Algebra are conjunction (and), disjunction (or), and negation (not). **It is the formalism used to describe the logical operations**.\n", - "\n", - "In [Python we write these operations](https://www.geeksforgeeks.org/python-3-logical-operators/?ref=rp) as:\n", - "\n", - "- `==`\n", - "* `x` and `y`\n", - "* `x` or `y`\n", - "* not `x`\n", - "* Extra: `^` [caret operator](https://stackoverflow.com/questions/2451386/what-does-the-caret-operator-do)\n", - "\n", - "Although the meaning of these operations is clear, we can fully define them with the so-called \"truth table\":" - ] - }, - { - "cell_type": "markdown", - "id": "coral-armenia", - "metadata": {}, - "source": [ - "![tablas](http://elclubdelautodidacta.es/wp/wp-content/uploads/2012/11/python-logicos.png)" - ] - }, - { - "cell_type": "markdown", - "id": "c39be317", - "metadata": { - "lang": "en" - }, - "source": [ - "We can also include the EXCLUSIVE OR, which is true when one and only one of the operands is true, but strictly you must know that it is derived from the three basic ones. Its representation is ^, the hat or caret." - ] - }, - { - "cell_type": "markdown", - "id": "244fb7c7", - "metadata": {}, - "source": [ - "### **equals** vs **is**" - ] - }, - { - "cell_type": "markdown", - "id": "c2fc6ff2", - "metadata": {}, - "source": [ - "[Article on Equal vs Is](https://towardsdatascience.com/whats-the-difference-between-is-and-in-python-dc26406c85ad)" - ] - }, - { - "cell_type": "markdown", - "id": "e8287044", - "metadata": {}, - "source": [ - "`==` -> points to the value
\n", - "`is` -> looks for identity" - ] - }, - { - "cell_type": "markdown", - "id": "1c4f6e09", - "metadata": {}, - "source": [ - "| Operator | Compares | Returns `True` if |\n", - "|----------|------------------|-----------------------------|\n", - "| `==` | Values | Values are equal |\n", - "| `is` | Object identity | Both refer to same object |\n" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "873ada8d", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] - } - ], - "source": [ - "# Example 1: Comparing Numbers\n", - "a = 5\n", - "b = 5\n", - "\n", - "print(a == b) # True, because a and b are the same number" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "63b2eb26", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] - } - ], - "source": [ - "print(a is b)" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "5a1aa6df", - "metadata": {}, - "outputs": [], - "source": [ - "# Example 2: comparing lists\n", - "list1 = [1, 2, 3]\n", - "list2 = [1, 2, 3]" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "id": "f714c908", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] - } - ], - "source": [ - "print(list1 == list2) # True, because the content of the list is the same" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "id": "5a5f1e05", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "False\n" - ] - } - ], - "source": [ - "print(list1 is list2) # False, because list1 and list2 are two different objects" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "id": "44e6eefc", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] - } - ], - "source": [ - "a = 5\n", - "b = 5\n", - "\n", - "c = 5\n", - "\n", - "a = c\n", - "b = c\n", - "\n", - "\n", - "print(a is b) # True? yes, but wierd: a and b point to the same \"thing\" due to an optimization in python" - ] - }, - { - "cell_type": "markdown", - "id": "29d1ae12", - "metadata": {}, - "source": [ - "## NOT\n", - "\n", - "- The **not** operator. The **not** operator is an operator that returns the opposite value of the evaluated expression. If the expression has the value True, it returns False. Conversely, if the expression has the value False, it returns True." - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "id": "70ecf121", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "not(list1 == list2)" - ] - }, - { - "cell_type": "markdown", - "id": "d5122028", - "metadata": { - "lang": "en" - }, - "source": [ - "## AND\n", - "- The **and** operator evaluates whether both expressions are true. If both expressions are true, it returns True. If either expression is false, it returns False. These types of tables are formally known as \"truth tables\"." - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "id": "6096259b", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "## TRUE FALSE\n", - "(list1 == list2) and (a == list2) " - ] - }, - { - "cell_type": "markdown", - "id": "ddb3ca4d", - "metadata": { - "lang": "en" - }, - "source": [ - "## OR\n", - "The **or** operator evaluates whether either expression is true, that is, it returns True if either expression is true and False when both expressions are false." - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "id": "3c409f34", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Same to (lista1 == lista2) | (a == lista2) \n", - "(list1 == list2) or (a == list2) " - ] - }, - { - "cell_type": "markdown", - "id": "ef5ced3e", - "metadata": {}, - "source": [ - "## EXERCISE" - ] - }, - { - "cell_type": "markdown", - "id": "e373cd72", - "metadata": {}, - "source": [ - "#### Exercise: Exploring Relational Operators\n", - "\n", - "In this exercise, we will explore the various relational operators available in Python. These operators are primarily used to establish relationships between different values and are essential in decision-making structures, although we won't be using them in that capacity in this exercise. Using these operators, please perform the following tasks:\n", - "\n", - "#### Task 1: Equal to Operator (==)\n", - "1. Create two strings and check if they are equal using `==`. Print the result.\n", - "2. Create two lists with equal elements and check if they are equal using `==`. Print the result.\n", - "\n", - "#### Task 2: Not Equal to Operator (!=)\n", - "1. Create two integers and check if they are not equal using `!=`. Print the result.\n", - "2. Use the `input()` function to get two inputs from the user and check if they are not equal using `!=`. Print the result.\n", - "\n", - "#### Task 3: Greater than and Lesser than Operators (>, <)\n", - "1. Create two floating-point numbers and check if one is greater than the other using `>`. Print the result.\n", - "2. Create two floating-point numbers and check if one is lesser than the other using `<`. Print the result.\n", - "\n", - "#### Task 4: Greater than or Equal to and Less than or Equal to Operators (>=, <=)\n", - "1. Create two integers and check if one is greater than or equal to the other using `>=`. Print the result.\n", - "2. Create two integers and check if one is less than or equal to the other using `<=`. Print the result." - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "id": "5de219ad", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "False\n", - "True\n", - "False\n", - "True\n", - "False\n", - "True\n" - ] - } - ], - "source": [ - "# your code here\n", - "str1 = \"Hello\"\n", - "str2 = \"Bye\"\n", - "print(str1 == str2)\n", - "\n", - "int1 = 16\n", - "int2 = 25\n", - "print(int1 != int2)\n", - "\n", - "float1 = 24.55\n", - "float2 = 33.98\n", - "print(float1 > float2)\n", - "print(float1 < float2)\n", - "\n", - "int3 = 16\n", - "int4 = 25\n", - "print(int3 >= int4)\n", - "print(int3 <= int4)" - ] - }, - { - "cell_type": "markdown", - "id": "attended-assurance", - "metadata": {}, - "source": [ - "## Loops an stuff\n" - ] - }, - { - "cell_type": "markdown", - "id": "b0a9f8bc", - "metadata": {}, - "source": [ - "![](https://i1.sndcdn.com/artworks-SvzDuFfyt3AJ9fO1-iQrV9Q-t500x500.jpg)" - ] - }, - { - "cell_type": "markdown", - "id": "3e0ac95f", - "metadata": { - "lang": "en" - }, - "source": [ - "In programming, a **loop** is a fundamental concept that allows a block of code to be repeated multiple times. It facilitates the execution of repetitive tasks without needing to write the same lines of code over and over again, promoting both efficiency and readability. The basic idea is to loop through a block of code as long as a specified condition remains true.\n", - "\n", - "There are generally two types of loops you'll encounter:\n", - "\n", - "1. **For Loop**: Generally used to iterate over a collection of items, such as elements in a list, tuple, or string. It will execute a block of code for each item in the collection, hence making it simpler and cleaner to perform the same action on multiple items.\n", - "\n", - "2. **While Loop**: This type of loop continues to execute a block of code as long as a particular condition is met. It's crucial to update the variables influencing the condition inside the loop, to avoid creating an infinite loop.\n", - "\n", - "Loops are incredibly powerful tools in programming, helping to save time and reduce the potential for errors in your code. As you progress in your learning, you will find loops are essential components in many programming solutions, facilitating the creation of complex, efficient, and innovative solutions to a wide range of problems.\n", - "\n", - "In the following sections, we will explore both types of loops in more detail, with examples to illustrate their usage and versatility in programming tasks.\n", - "\n", - "Happy looping!" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "id": "1d6224df", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'BANANA'" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "lista= [\"melon\", \"apple\", \"banana\"]\n", - "lista[0].upper()\n", - "lista[1].upper()\n", - "lista[2].upper()" - ] - }, - { - "cell_type": "markdown", - "id": "aa20ffc4", - "metadata": {}, - "source": [ - "### Pseudocode" - ] - }, - { - "cell_type": "markdown", - "id": "c2a2b91b", - "metadata": { - "lang": "en" - }, - "source": [ - "Before we dive into the world of loops, let's take a moment to understand what \"Pseudocode\" is. Pseudocode is a high-level description of the functioning of a computer program or algorithm. It utilizes the structural conventions of programming languages but is designed to be read by humans rather than machines. \n", - "\n", - "In pseudocode, we are not concerned with precise syntax that a programming language might require. Instead, we focus on expressing the logic and the steps needed to solve a problem or accomplish a task, often in natural language mixed with some common programming structures. This means that pseudocode does not have a strict syntax and can vary widely.\n", - "\n", - "The main objective of writing pseudocode is to outline the structure of the program and delineate its logical flow without getting caught up in the specific details of a particular programming language. This makes it a useful tool for planning and designing programs, where the emphasis is on what to do rather than how to do it.\n", - "\n", - "Here are some general characteristics of pseudocode:\n", - "1. **Simplicity**: It should be simple and easy to understand.\n", - "2. **Clarity**: Each step is clearly defined without ambiguity.\n", - "3. **Language-independent**: It does not adhere to the syntax of any specific programming language.\n", - "4. **Focus on Logic**: The primary focus is on the logic and flow of the algorithm, rather than the specific syntax of a programming language.\n", - "\n", - "As you begin to work with loops and other programming structures, you'll find that creating pseudocode can be a valuable first step in the coding process, helping you to organize your thoughts and create a roadmap for your program before you start writing actual code." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "id": "a1b077ff", - "metadata": {}, - "outputs": [], - "source": [ - "# a list of elements\n", - "# loop through the list \n", - "# and check if the type is a number\n", - "\n", - "# if the type is a number\n", - "# i do stuff\n", - "\n", - "# WHAT IS IT I HAVE?: LIST OF LISTS WITH NUMBERS\n", - " # original = [[1, 2], [3], [5, 6, 9]]\n", - " # 1. Create a new_list\n", - " # 2. GOAL: Take the element out\n", - " # 3. Access the element\n", - " # 4. loop original list: [1, 2]\n", - " # 5. loop the i/individual_list again\n", - " # 6: add to new_list\n", - " # 7: print\n", - " \n", - "# GOAL: WHAT RESULT I NEED TO RETURN\n", - " # [1, 2, 3, 4, 5, 6]\n", - " \n", - "# always print" - ] - }, - { - "cell_type": "markdown", - "id": "4690d4e0", - "metadata": {}, - "source": [ - "Quick infor card:\n", - "\n", - "In Python, the `range()` function is used to generate a sequence of numbers over time. It is widely used in loops to control the number of iterations. The function has three parameters: start, stop, and step.\n", - "\n", - "- **Start**: This parameter specifies the starting point of the sequence. It is optional, and the default value is 0.\n", - "- **Stop**: This parameter specifies the endpoint of the sequence. This value is not included in the sequence.\n", - "- **Step**: This parameter specifies the increment between each number in the sequence. It is optional, and the default value is 1.\n", - "\n", - "Here is the syntax for the `range()` function:\n", - "\n", - "- range(start, stop, step)\n", - "\n", - "Some example:" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "id": "d024b13d", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "range(1, 6)" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "range(1, 6)" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "id": "89f2329b", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0\n", - "1\n", - "2\n", - "3\n", - "4\n", - "5\n", - "Hi\n" - ] - } - ], - "source": [ - "for i in range(6):\n", - " print(i)\n", - "print(\"Hi\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "id": "9fdcf991", - "metadata": {}, - "outputs": [], - "source": [ - "# play here with range" - ] - }, - { - "cell_type": "markdown", - "id": "fee3afff", - "metadata": {}, - "source": [ - "### Loop example\n", - "Let's do more complex stuff:" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "id": "23dd3aa0", - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2]\n", - "1\n", - "2\n", - "[3]\n", - "3\n", - "[5, 6, 9]\n", - "5\n", - "6\n", - "9\n", - "[1, 2, 3, 5, 6, 9]\n" - ] - } - ], - "source": [ - "# This is the original list of lists where each element is a list containing integers.\n", - "original = [[1, 2], [3], [5, 6, 9]]\n", - "# A new empty list is initialized to store the individual elements after flattening the original list.\n", - "new_list = []\n", - "\n", - "# The outer loop iterates through each individual list inside the original list.\n", - "for individual_list in original:\n", - " print(individual_list)\n", - " # The inner loop iterates through each element in the currently selected individual list (from the outer loop).\n", - " for each_element in individual_list:\n", - " # The current element from the individual list is appended to the new list.\n", - " print(each_element)\n", - " new_list.append(each_element)\n", - "\n", - "# Printing the new_list to see the final output.\n", - "print(new_list)" - ] - }, - { - "cell_type": "markdown", - "id": "8a77f835", - "metadata": {}, - "source": [ - "Debug what is happenin:" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "id": "a36780d7", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2]\n", - "[3]\n", - "[5, 6, 9, [1, 2]]\n" - ] - } - ], - "source": [ - "# We are defining a list named 'bigger_list' which contains several other lists, one of which is nested\n", - "bigger_list = [[1, 2], [3], [5, 6, 9, [1, 2]]]\n", - "\n", - "# We are initiating a for loop to iterate over each 'small_list' present within the 'bigger_list'\n", - "for small_list in bigger_list:\n", - " # Inside the loop, we are printing the current 'small_list' that the loop is iterating over\n", - " print(small_list)\n", - " # Once the end of the loop is reached, it goes back to the start to process the next 'small_list' in the 'bigger_list'" - ] - }, - { - "cell_type": "markdown", - "id": "202e988c", - "metadata": {}, - "source": [ - "### Exercise:" - ] - }, - { - "cell_type": "markdown", - "id": "66a5e23a", - "metadata": {}, - "source": [ - "You are working in the sales department of a company. At the end of each week, you need to generate a report of total sales made each day. You have a list where each element represents the sales made on each day of the week (from Monday to Sunday). Your task is to calculate the total sales of the week.\n", - "\n", - "To complete this exercise, you need to follow the given steps:\n", - "\n", - "1. **Initialize Variables**:\n", - " Before starting the loop, initialize a variable `total_sales` to 0. This will be used to accumulate the sales of each day. Additionally, initialize `max_sales` to 0.\n", - " \n", - " \n", - "2. **Creating a Loop**:\n", - " Create a for loop where you iterate over each element in the `sales_data` list. You can use a loop structure like this: for day in sales_data:\n", - " \n", - " \n", - "3. **Calculating Total Sales**:\n", - " Inside the loop, add the sales of the current day to `total_sales`. You can do this using an assignment statement like this: total_sales += day\n", - "\n", - "\n", - "\n", - "5. **Printing the Results**:\n", - " After the loop, print the `total_sales` along with appropriate messages to show the total sales of the week and the day with maximum sales." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "id": "3bd7d85f", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "4500\n", - "1000\n" - ] - } - ], - "source": [ - "# List representing sales data for seven days (in units)\n", - "sales_data = [200, 300, 400, 500, 600, 700, 800,1000]\n", - "\n", - "# Initialize total_sales to 0 before starting the loop\n", - "total_sales = 0\n", - "# Initialize max_sales before starting the loop\n", - "max_sales = 0\n", - "\n", - "# {YOUR CODE HERE}\n", - "for day in sales_data:\n", - " #print(day)\n", - "\n", - " total_sales +=day\n", - " if day > max_sales:\n", - " max_sales = day\n", - "\n", - "print(total_sales)\n", - "print(max_sales)" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "id": "654bbe75", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1000" - ] - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "max(sales_data )" - ] - }, - { - "cell_type": "markdown", - "id": "0eaad548", - "metadata": { - "lang": "en" - }, - "source": [ - "## The \"If\" statement\n", - "This is the most popular option to control the flow of a program. Conditions allow you to choose between different paths depending on the value of an expression. When you want to execute an action only when some condition is `True`, an \"if\" statement is used:" - ] - }, - { - "cell_type": "markdown", - "id": "580479b3", - "metadata": {}, - "source": [ - "#### if syntax:\n", - "- indentation: `tab / 5 spaces`\n", - "- colon: `:`\n" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "id": "ef3ac3be", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Adult\n", - "True condition\n" - ] - } - ], - "source": [ - "age = 18\n", - "\n", - "if age >= 18:\n", - " print(\"Adult\")\n", - " print(\"True condition\")\n", - " " - ] - }, - { - "cell_type": "markdown", - "id": "305e83a6", - "metadata": { - "lang": "en" - }, - "source": [ - "As we can see, the code block associated with the condition begins after the colon \":\", with an indentation that determines the code block. All statements belonging to the same block must have the same indentation. The block ends when the indent returns to the starting position of the if statement. Remember that Python uses indentation to identify blocks of code." - ] - }, - { - "cell_type": "markdown", - "id": "770fbd1a", - "metadata": { - "lang": "en" - }, - "source": [ - "### else\n", - "Sometimes you want if the condition is not met (and only then) another action is executed. They are mutually exclusive actions. This is achieved with the `else` statement." - ] - }, - { - "cell_type": "markdown", - "id": "median-injection", - "metadata": {}, - "source": [ - "![if](https://www.tutorialspoint.com/scala/images/scala_decision_making.jpg)" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "id": "5c7a1210", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Not an adult\n" - ] - } - ], - "source": [ - "age = 17\n", - "\n", - "if age >= 18:\n", - " print(\"Adult\")\n", - "else:\n", - " print(\"Not an adult\")" - ] - }, - { - "cell_type": "markdown", - "id": "baa11a80", - "metadata": { - "lang": "en" - }, - "source": [ - "### elif\n", - "Sometimes there are more than two possibilities. Think of something like:\n", - "\"If Bob can get into the disco, let him in. If not, if he's over 16, recommend the nearby light disco. If not, send him home.\"\n", - "\n", - "We can achieve this with the `elif` clause and chained conditionals:" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "id": "82badc29", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Not Human\n" - ] - } - ], - "source": [ - "age = 130\n", - "\n", - "if age >= 130:\n", - " print(\"Not Human\")\n", - "\n", - "elif age >= 21:\n", - " print(\"adult\")\n", - " \n", - "elif age < 21:\n", - " print(\"Not an adult\")\n", - "\n", - "else:\n", - " print(\"It is good practice to have an else\")" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "id": "8dfc81eb", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Not Human\n" - ] - } - ], - "source": [ - "age = 200\n", - "\n", - "if age >= 130:\n", - " print(\"Not Human\")\n", - "elif age >21:\n", - " print(\"Adult\")" - ] - }, - { - "cell_type": "markdown", - "id": "b3d55cd1", - "metadata": {}, - "source": [ - "### IF; ELIF; ELSE" - ] - }, - { - "cell_type": "markdown", - "id": "50d43411", - "metadata": {}, - "source": [ - "In Python programming, a nested if statement is an if statement that is the target of another if statement. Nested if statements mean an if statement inside another if statement. Yes, Python allows us to nest if statements within if statements. Here is the general form of a nested if statement." - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "id": "36b1c6ee", - "metadata": {}, - "outputs": [ - { - "ename": "ValueError", - "evalue": "invalid literal for int() with base 10: ''", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[33], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# 1. Takes the input of a user (controlling it's an int)\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m age \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease input your age: \u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# 2. Based on conditions: return something or something else\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m age \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n", - "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: ''" - ] - } - ], - "source": [ - "# 1. Takes the input of a user (controlling it's an int)\n", - "age = int(input(\"Please input your age: \"))\n", - "\n", - "# 2. Based on conditions: return something or something else\n", - "if age > 0:\n", - " if age < 16:\n", - " print(\"You CANNOT get in\")\n", - "\n", - " elif age >= 16 and age < 18:\n", - " print(\"Alcohol free club\")\n", - "\n", - " elif age >= 18:\n", - " print(\"You can get in\")\n", - " \n", - "else:\n", - " print(\"Sorry, wrong info: negative value?\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f2fd273c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "ok\n", - "startswith L\n", - "has letters\n", - "Name is Lola\n" - ] - } - ], - "source": [ - "# Assign a string which is intended to represent a password to the variable 'password'\n", - "password = \"12345678909876dcvbnhjytfgvb+\"\n", - "\n", - "# Check if the length of the password is greater than 8, if true, print \"ok\"\n", - "if len(password) > 8:\n", - " print(\"ok\")\n", - "\n", - "# Check if the password contains any letters, if true, print \"startswith L\"\n", - "# (Note: The current condition will always evaluate to True as it's checking if password is non-empty.\n", - "# To check for letters, you might need to modify the condition using any(x.isalpha() for x in password))\n", - "if password: # has letters\n", - " print(\"startswith L\")\n", - " \n", - "if any(x.isalpha() for x in password):\n", - " print(\"has letters\")\n", - " \n", - "# Check if the password contains any numbers, if true, print \"Name is Lola\"\n", - "# (Note: Similar to the previous note, the condition here checks if password is non-empty, \n", - "# to check for numbers you might want to modify the condition using any(x.isdigit() for x in password))\n", - "if password: # has numbers:\n", - " print(\"Name is Lola\")\n", - " \n", - "# (... modify the code for expected output\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3169c74a", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "2\n", - "3\n", - "4\n", - "5\n", - "6\n", - "7\n", - "8\n", - "9\n", - "0\n", - "9\n", - "8\n", - "7\n", - "6\n", - "d\n", - "c\n", - "v\n", - "b\n", - "n\n", - "h\n", - "j\n", - "y\n", - "t\n", - "f\n", - "g\n", - "v\n", - "b\n", - "+\n" - ] - } - ], - "source": [ - "for x in password:\n", - " print(x)" - ] - }, - { - "cell_type": "markdown", - "id": "a62738d2", - "metadata": {}, - "source": [ - "### Exercise extension:\n", - "Now use if statements, to calculate on top of total sales, which day had the maximum sales." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "65c946ed", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The day with the max sales is 7 \n" - ] - } - ], - "source": [ - "# List representing sales data for seven days (in units)\n", - "sales_data = [200, 300, 400, 500, 600, 700, 800]\n", - "\n", - "# Initialize total_sales to 0 before starting the loop\n", - "total_sales = 0\n", - "\n", - "# Initialize max_sales before starting the loop\n", - "max_sales = 0\n", - "max_day=1\n", - "\n", - "# {YOUR CODE HERE}\n", - "for i in range (0,len(sales_data)):\n", - " if sales_data[i]>max_sales:\n", - " max_sales=sales_data[i]\n", - " max_day=i+1\n", - "print(f\"The day with the max sales is {max_day} \")\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "id": "4202238e", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "200\n", - "300\n", - "400\n", - "500\n", - "600\n", - "700\n", - "800\n", - "1000\n" - ] - } - ], - "source": [ - "max_sales = 0\n", - "for day in sales_data:\n", - " if day > max_sales:\n", - " max_sales = day\n", - " \n", - " print(max_sales)" - ] - }, - { - "cell_type": "markdown", - "id": "4119b21e", - "metadata": { - "lang": "en" - }, - "source": [ - "# Challenge 🤔\n", - "\n", - "Write a simple program that prints if bob can get into the club\n", - "for any two values ​​of the following variables:\n", - "`print(age_bob,age_minimum)`" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "5d8caf12", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "club\n" - ] - } - ], - "source": [ - "age_bob = 18\n", - "age_minimum = 16\n", - "\n", - "if age_bob >= age_minimum:\n", - " print(\"club\")\n", - "elif age_bob == 18:\n", - " print(\"cannot club\")\n", - "else:\n", - " print(\"cannot club\")" - ] - }, - { - "cell_type": "markdown", - "id": "a9b00fd8", - "metadata": { - "lang": "en" - }, - "source": [ - "### Your examples 🚀" - ] - }, - { - "cell_type": "markdown", - "id": "0833207b", - "metadata": {}, - "source": [ - "Modify your schedule so that when Bob can't get into the club but is over the legal age (age=21)\n", - "send it to disco light" - ] - }, - { - "cell_type": "markdown", - "id": "5f01cb54", - "metadata": { - "lang": "en" - }, - "source": [ - "# Challenge 🤔\n" - ] - }, - { - "cell_type": "markdown", - "id": "b217ff64", - "metadata": { - "lang": "en" - }, - "source": [ - "Let's imagine that we are building the program for a robot that sorts eggs by size. Our robot arm receives information from a scale, which indicates, in grams, the weight of the egg to be classified. The arm must, from the weight, place the egg in one box or another as follows:\n", - "\n", - "- **Box S** (small): weight less than 53 grams.\n", - "- **Box M** (medium) : weight greater than or equal to 53 grams and less than 63 grams.\n", - "- **Box L** (large): weight greater than or equal to 63 grams and less than 73 grams.\n", - "- **XL box** (super large): weight greater than or equal to 73 grams." - ] - }, - { - "cell_type": "markdown", - "id": "4ed9f405", - "metadata": {}, - "source": [] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "d61dba2f", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Box L (large)\n" - ] - } - ], - "source": [ - "weight = 70 # in kilograms\n", - "if weight < 53:\n", - " print(\"Box S (small)\")\n", - "elif weight < 63:\n", - " print(\"Box M (medium)\")\n", - "elif weight < 73:\n", - " print(\"Box L (large)\") \n", - "else:\n", - " print(\"Box XL (extra large)\")" - ] - }, - { - "cell_type": "markdown", - "id": "61cfbb7c", - "metadata": {}, - "source": [ - "## Summary\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4524a456", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "7deb669c", - "metadata": {}, - "source": [ - "[Rubber duck debugging](https://www.freecodecamp.org/news/rubber-duck-debugging/#:~:text=The%20idea%20behind%20the%20rubber,loud%20before%20you%20post%20it)\n", - "\n", - "The \"Rubber Duck Theory\" or \"Rubber Duck Debugging\" is a method used to find errors or bugs in a code. In this technique, the programmer explains their code, line by line, to an inanimate object such as a rubber duck. The act of explaining the code in detail often helps the programmer to see errors or improvements that they didn't notice before. This is similar to writing a message on social media and then reading it aloud before posting it, which can sometimes help in spotting errors or reconsidering the message's content." - ] - }, - { - "cell_type": "markdown", - "id": "toxic-romance", - "metadata": {}, - "source": [ - "## Further materials\n", - "\n", - "* [Python docs](https://docs.python.org/3/tutorial/controlflow.html)\n", - "* Short tutorial on [Python Booleans](https://realpython.com/python-boolean/)\n", - "* Just a nice [talk by Feynman](https://www.youtube.com/watch?v=EKWGGDXe5MA) on the principles of computation" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.13.5" - }, - "nbTranslate": { - "displayLangs": [ - "en", - "es" - ], - "hotkey": "alt-a", - "langInMainMenu": true, - "sourceLang": "es", - "targetLang": "en", - "useGoogleTranslate": true - }, - "toc": { - "base_numbering": 1, - "nav_menu": {}, - "number_sections": true, - "sideBar": true, - "skip_h1_title": true, - "title_cell": "Table of Contents", - "title_sidebar": "Contents", - "toc_cell": true, - "toc_position": { - "height": "calc(100% - 180px)", - "left": "10px", - "top": "150px", - "width": "165px" - }, - "toc_section_display": true, - "toc_window_display": true - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} +{ + "cells": [ + { + "cell_type": "markdown", + "id": "b0f6ad4a", + "metadata": {}, + "source": [ + "# Flow Control 1" + ] + }, + { + "cell_type": "markdown", + "id": "subjective-quarter", + "metadata": { + "toc": true + }, + "source": [ + "

Table of Contents

\n", + "" + ] + }, + { + "cell_type": "markdown", + "id": "63c618c2", + "metadata": {}, + "source": [ + "![](https://media.makeameme.org/created/im-just-going-ca4ce2c899.jpg)" + ] + }, + { + "cell_type": "markdown", + "id": "143f947d", + "metadata": { + "lang": "en" + }, + "source": [ + "## Arithmetic operators" + ] + }, + { + "cell_type": "markdown", + "id": "9aecbca0", + "metadata": { + "lang": "en" + }, + "source": [ + "Arithmetic operators are used to perform arithmetic operations, that is, to manipulate numerical data through mathematical operations such as addition, subtraction or multiplication..." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "8b428e9f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = 10\n", + "b = 2\n", + "\n", + "a + b\n", + "a * b" + ] + }, + { + "cell_type": "markdown", + "id": "7dae48c3", + "metadata": {}, + "source": [ + "`+`, `*`, and all others" + ] + }, + { + "cell_type": "markdown", + "id": "e6aaec42", + "metadata": { + "lang": "en" + }, + "source": [ + "## Assignment operators (Python tricks)" + ] + }, + { + "cell_type": "markdown", + "id": "e3982cf4", + "metadata": {}, + "source": [ + "![](https://i.pinimg.com/236x/2b/dd/c9/2bddc91accccb9b70178c49cf5684bed.jpg)" + ] + }, + { + "cell_type": "markdown", + "id": "3e1e56bd", + "metadata": { + "lang": "en" + }, + "source": [ + "Assignment operators are those that allow you to give a value to a variable or modify it. Python has eight different assignment operators: a simple assignment operator and seven compound assignment operators." + ] + }, + { + "cell_type": "markdown", + "id": "fc5d346e", + "metadata": { + "lang": "en" + }, + "source": [ + "* `=` Simple assignment `a=b`\n", + "* `+=` Addition assignment `a+=b` Simple equivalent `a=a+b`\n", + "* `-=` Subtraction assignment `a-=b` Simple equivalent `a=a-b`\n", + "* `*=` Multiplication Assignment `a*=b` Simple Equivalent `a=a*b`\n", + "* `/=` Division assignment `a/=b` Simple equivalent `a=a/b`\n", + "* `%=` Module mapping `a/=b` Simple equivalent `a=a%b`\n", + "* `//=` Integer Division Assignment `a//=b` Simple Equivalent `a=a//b`\n", + "\n", + "(It is important that you know this, but remember python principles....)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "7dba4669", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#a = a + 1\n", + "a =+ 1\n", + "a" + ] + }, + { + "cell_type": "markdown", + "id": "89b42f24", + "metadata": { + "lang": "en" + }, + "source": [ + "The simple assignment operator is the equals symbol (=) and operations performed on it always have the syntax: `variable = expression`. In this type of operation, first, the expression on the right is resolved and the resulting value is assigned to the variable on the left." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "709c2301", + "metadata": {}, + "outputs": [], + "source": [ + "# Take some time, and practice" + ] + }, + { + "cell_type": "markdown", + "id": "71a92117", + "metadata": { + "lang": "en" + }, + "source": [ + "## Relational operators" + ] + }, + { + "cell_type": "markdown", + "id": "38d5146c", + "metadata": { + "lang": "en" + }, + "source": [ + "\n", + "Relational operators are symbols used to compare two values ​​or expressions. The result of the evaluation with these two operators can be True, if the comparison is true, or False, if the comparison is false." + ] + }, + { + "cell_type": "markdown", + "id": "b7d7c2b5", + "metadata": { + "lang": "en" + }, + "source": [ + "* `==` Equal to `a==b`\n", + "* `!=` Not equal to `a!=b`\n", + "* `>` Greater than `a>b`\n", + "* `<` Lesser than `a=` Greater than or equal to `a>=b`\n", + "* `<=` Less than or equal to `a<=b`" + ] + }, + { + "cell_type": "markdown", + "id": "6bb2bc2a", + "metadata": { + "lang": "en" + }, + "source": [ + "Note the difference between a single equals sign (=), which is an assignment, and a double equals sign (==), which is a relational operator\n", + "\n", + "**Hint: Use parentheses to maintain the order of operations." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ac9ccfb3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# What is the difference between these two?\n", + "a = 10\n", + "a == 10" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "c00bf648", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a != 10" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "3dde583b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a > b" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "4efdcce8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a < b" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "16a5b605", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "x = (a <= b)\n", + "x" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "de053fec", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "y = (b <= 20)\n", + "y" + ] + }, + { + "cell_type": "markdown", + "id": "5753abc4", + "metadata": {}, + "source": [ + "### Boolean Algebra" + ] + }, + { + "cell_type": "markdown", + "id": "0bf8a056", + "metadata": { + "lang": "en" + }, + "source": [ + "With just these two `True`|`False` values ​​we can create a whole branch of mathematics called [Boolean Algebra](https://en.wikipedia.org/wiki/Boolean_algebra#Laws). While in regular Algebra the basic operations are addition and multiplication, the main operations in Boolean Algebra are conjunction (and), disjunction (or), and negation (not). **It is the formalism used to describe the logical operations**.\n", + "\n", + "In [Python we write these operations](https://www.geeksforgeeks.org/python-3-logical-operators/?ref=rp) as:\n", + "\n", + "- `==`\n", + "* `x` and `y`\n", + "* `x` or `y`\n", + "* not `x`\n", + "* Extra: `^` [caret operator](https://stackoverflow.com/questions/2451386/what-does-the-caret-operator-do)\n", + "\n", + "Although the meaning of these operations is clear, we can fully define them with the so-called \"truth table\":" + ] + }, + { + "cell_type": "markdown", + "id": "coral-armenia", + "metadata": {}, + "source": [ + "![tablas](http://elclubdelautodidacta.es/wp/wp-content/uploads/2012/11/python-logicos.png)" + ] + }, + { + "cell_type": "markdown", + "id": "c39be317", + "metadata": { + "lang": "en" + }, + "source": [ + "We can also include the EXCLUSIVE OR, which is true when one and only one of the operands is true, but strictly you must know that it is derived from the three basic ones. Its representation is ^, the hat or caret." + ] + }, + { + "cell_type": "markdown", + "id": "244fb7c7", + "metadata": {}, + "source": [ + "### **equals** vs **is**" + ] + }, + { + "cell_type": "markdown", + "id": "c2fc6ff2", + "metadata": {}, + "source": [ + "[Article on Equal vs Is](https://towardsdatascience.com/whats-the-difference-between-is-and-in-python-dc26406c85ad)" + ] + }, + { + "cell_type": "markdown", + "id": "e8287044", + "metadata": {}, + "source": [ + "`==` -> points to the value
\n", + "`is` -> looks for identity" + ] + }, + { + "cell_type": "markdown", + "id": "1c4f6e09", + "metadata": {}, + "source": [ + "| Operator | Compares | Returns `True` if |\n", + "|----------|------------------|-----------------------------|\n", + "| `==` | Values | Values are equal |\n", + "| `is` | Object identity | Both refer to same object |\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "873ada8d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "# Example 1: Comparing Numbers\n", + "a = 5\n", + "b = 5\n", + "\n", + "print(a == b) # True, because a and b are the same number" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "63b2eb26", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "print(a is b)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "5a1aa6df", + "metadata": {}, + "outputs": [], + "source": [ + "# Example 2: comparing lists\n", + "list1 = [1, 2, 3]\n", + "list2 = [1, 2, 3]" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "f714c908", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "print(list1 == list2) # True, because the content of the list is the same" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "5a5f1e05", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], + "source": [ + "print(list1 is list2) # False, because list1 and list2 are two different objects" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "44e6eefc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "a = 5\n", + "b = 5\n", + "\n", + "c = 5\n", + "\n", + "a = c\n", + "b = c\n", + "\n", + "\n", + "print(a is b) # True? yes, but wierd: a and b point to the same \"thing\" due to an optimization in python" + ] + }, + { + "cell_type": "markdown", + "id": "29d1ae12", + "metadata": {}, + "source": [ + "## NOT\n", + "\n", + "- The **not** operator. The **not** operator is an operator that returns the opposite value of the evaluated expression. If the expression has the value True, it returns False. Conversely, if the expression has the value False, it returns True." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "70ecf121", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "not(list1 == list2)" + ] + }, + { + "cell_type": "markdown", + "id": "d5122028", + "metadata": { + "lang": "en" + }, + "source": [ + "## AND\n", + "- The **and** operator evaluates whether both expressions are true. If both expressions are true, it returns True. If either expression is false, it returns False. These types of tables are formally known as \"truth tables\"." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "6096259b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "## TRUE FALSE\n", + "(list1 == list2) and (a == list2) " + ] + }, + { + "cell_type": "markdown", + "id": "ddb3ca4d", + "metadata": { + "lang": "en" + }, + "source": [ + "## OR\n", + "The **or** operator evaluates whether either expression is true, that is, it returns True if either expression is true and False when both expressions are false." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "3c409f34", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Same to (lista1 == lista2) | (a == lista2) \n", + "(list1 == list2) or (a == list2) " + ] + }, + { + "cell_type": "markdown", + "id": "ef5ced3e", + "metadata": {}, + "source": [ + "## EXERCISE" + ] + }, + { + "cell_type": "markdown", + "id": "e373cd72", + "metadata": {}, + "source": [ + "#### Exercise: Exploring Relational Operators\n", + "\n", + "In this exercise, we will explore the various relational operators available in Python. These operators are primarily used to establish relationships between different values and are essential in decision-making structures, although we won't be using them in that capacity in this exercise. Using these operators, please perform the following tasks:\n", + "\n", + "#### Task 1: Equal to Operator (==)\n", + "1. Create two strings and check if they are equal using `==`. Print the result.\n", + "2. Create two lists with equal elements and check if they are equal using `==`. Print the result.\n", + "\n", + "#### Task 2: Not Equal to Operator (!=)\n", + "1. Create two integers and check if they are not equal using `!=`. Print the result.\n", + "2. Use the `input()` function to get two inputs from the user and check if they are not equal using `!=`. Print the result.\n", + "\n", + "#### Task 3: Greater than and Lesser than Operators (>, <)\n", + "1. Create two floating-point numbers and check if one is greater than the other using `>`. Print the result.\n", + "2. Create two floating-point numbers and check if one is lesser than the other using `<`. Print the result.\n", + "\n", + "#### Task 4: Greater than or Equal to and Less than or Equal to Operators (>=, <=)\n", + "1. Create two integers and check if one is greater than or equal to the other using `>=`. Print the result.\n", + "2. Create two integers and check if one is less than or equal to the other using `<=`. Print the result." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "5de219ad", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n", + "False\n", + "True\n", + "False\n", + "True\n" + ] + } + ], + "source": [ + "# your code here\n", + "str1 = \"Hello\"\n", + "str2 = \"Bye\"\n", + "print(str1 == str2)\n", + "\n", + "int1 = 16\n", + "int2 = 25\n", + "print(int1 != int2)\n", + "\n", + "float1 = 24.55\n", + "float2 = 33.98\n", + "print(float1 > float2)\n", + "print(float1 < float2)\n", + "\n", + "int3 = 16\n", + "int4 = 25\n", + "print(int3 >= int4)\n", + "print(int3 <= int4)" + ] + }, + { + "cell_type": "markdown", + "id": "attended-assurance", + "metadata": {}, + "source": [ + "## Loops an stuff\n" + ] + }, + { + "cell_type": "markdown", + "id": "b0a9f8bc", + "metadata": {}, + "source": [ + "![](https://i1.sndcdn.com/artworks-SvzDuFfyt3AJ9fO1-iQrV9Q-t500x500.jpg)" + ] + }, + { + "cell_type": "markdown", + "id": "3e0ac95f", + "metadata": { + "lang": "en" + }, + "source": [ + "In programming, a **loop** is a fundamental concept that allows a block of code to be repeated multiple times. It facilitates the execution of repetitive tasks without needing to write the same lines of code over and over again, promoting both efficiency and readability. The basic idea is to loop through a block of code as long as a specified condition remains true.\n", + "\n", + "There are generally two types of loops you'll encounter:\n", + "\n", + "1. **For Loop**: Generally used to iterate over a collection of items, such as elements in a list, tuple, or string. It will execute a block of code for each item in the collection, hence making it simpler and cleaner to perform the same action on multiple items.\n", + "\n", + "2. **While Loop**: This type of loop continues to execute a block of code as long as a particular condition is met. It's crucial to update the variables influencing the condition inside the loop, to avoid creating an infinite loop.\n", + "\n", + "Loops are incredibly powerful tools in programming, helping to save time and reduce the potential for errors in your code. As you progress in your learning, you will find loops are essential components in many programming solutions, facilitating the creation of complex, efficient, and innovative solutions to a wide range of problems.\n", + "\n", + "In the following sections, we will explore both types of loops in more detail, with examples to illustrate their usage and versatility in programming tasks.\n", + "\n", + "Happy looping!" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "1d6224df", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'BANANA'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lista= [\"melon\", \"apple\", \"banana\"]\n", + "lista[0].upper()\n", + "lista[1].upper()\n", + "lista[2].upper()" + ] + }, + { + "cell_type": "markdown", + "id": "aa20ffc4", + "metadata": {}, + "source": [ + "### Pseudocode" + ] + }, + { + "cell_type": "markdown", + "id": "c2a2b91b", + "metadata": { + "lang": "en" + }, + "source": [ + "Before we dive into the world of loops, let's take a moment to understand what \"Pseudocode\" is. Pseudocode is a high-level description of the functioning of a computer program or algorithm. It utilizes the structural conventions of programming languages but is designed to be read by humans rather than machines. \n", + "\n", + "In pseudocode, we are not concerned with precise syntax that a programming language might require. Instead, we focus on expressing the logic and the steps needed to solve a problem or accomplish a task, often in natural language mixed with some common programming structures. This means that pseudocode does not have a strict syntax and can vary widely.\n", + "\n", + "The main objective of writing pseudocode is to outline the structure of the program and delineate its logical flow without getting caught up in the specific details of a particular programming language. This makes it a useful tool for planning and designing programs, where the emphasis is on what to do rather than how to do it.\n", + "\n", + "Here are some general characteristics of pseudocode:\n", + "1. **Simplicity**: It should be simple and easy to understand.\n", + "2. **Clarity**: Each step is clearly defined without ambiguity.\n", + "3. **Language-independent**: It does not adhere to the syntax of any specific programming language.\n", + "4. **Focus on Logic**: The primary focus is on the logic and flow of the algorithm, rather than the specific syntax of a programming language.\n", + "\n", + "As you begin to work with loops and other programming structures, you'll find that creating pseudocode can be a valuable first step in the coding process, helping you to organize your thoughts and create a roadmap for your program before you start writing actual code." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "a1b077ff", + "metadata": {}, + "outputs": [], + "source": [ + "# a list of elements\n", + "# loop through the list \n", + "# and check if the type is a number\n", + "\n", + "# if the type is a number\n", + "# i do stuff\n", + "\n", + "# WHAT IS IT I HAVE?: LIST OF LISTS WITH NUMBERS\n", + " # original = [[1, 2], [3], [5, 6, 9]]\n", + " # 1. Create a new_list\n", + " # 2. GOAL: Take the element out\n", + " # 3. Access the element\n", + " # 4. loop original list: [1, 2]\n", + " # 5. loop the i/individual_list again\n", + " # 6: add to new_list\n", + " # 7: print\n", + " \n", + "# GOAL: WHAT RESULT I NEED TO RETURN\n", + " # [1, 2, 3, 4, 5, 6]\n", + " \n", + "# always print" + ] + }, + { + "cell_type": "markdown", + "id": "4690d4e0", + "metadata": {}, + "source": [ + "Quick infor card:\n", + "\n", + "In Python, the `range()` function is used to generate a sequence of numbers over time. It is widely used in loops to control the number of iterations. The function has three parameters: start, stop, and step.\n", + "\n", + "- **Start**: This parameter specifies the starting point of the sequence. It is optional, and the default value is 0.\n", + "- **Stop**: This parameter specifies the endpoint of the sequence. This value is not included in the sequence.\n", + "- **Step**: This parameter specifies the increment between each number in the sequence. It is optional, and the default value is 1.\n", + "\n", + "Here is the syntax for the `range()` function:\n", + "\n", + "- range(start, stop, step)\n", + "\n", + "Some example:" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "d024b13d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "range(1, 6)" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "range(1, 6)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "89f2329b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n", + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "Hi\n" + ] + } + ], + "source": [ + "for i in range(6):\n", + " print(i)\n", + "print(\"Hi\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "9fdcf991", + "metadata": {}, + "outputs": [], + "source": [ + "# play here with range" + ] + }, + { + "cell_type": "markdown", + "id": "fee3afff", + "metadata": {}, + "source": [ + "### Loop example\n", + "Let's do more complex stuff:" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "23dd3aa0", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2]\n", + "1\n", + "2\n", + "[3]\n", + "3\n", + "[5, 6, 9]\n", + "5\n", + "6\n", + "9\n", + "[1, 2, 3, 5, 6, 9]\n" + ] + } + ], + "source": [ + "# This is the original list of lists where each element is a list containing integers.\n", + "original = [[1, 2], [3], [5, 6, 9]]\n", + "# A new empty list is initialized to store the individual elements after flattening the original list.\n", + "new_list = []\n", + "\n", + "# The outer loop iterates through each individual list inside the original list.\n", + "for individual_list in original:\n", + " print(individual_list)\n", + " # The inner loop iterates through each element in the currently selected individual list (from the outer loop).\n", + " for each_element in individual_list:\n", + " # The current element from the individual list is appended to the new list.\n", + " print(each_element)\n", + " new_list.append(each_element)\n", + "\n", + "# Printing the new_list to see the final output.\n", + "print(new_list)" + ] + }, + { + "cell_type": "markdown", + "id": "8a77f835", + "metadata": {}, + "source": [ + "Debug what is happenin:" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "a36780d7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2]\n", + "[3]\n", + "[5, 6, 9, [1, 2]]\n" + ] + } + ], + "source": [ + "# We are defining a list named 'bigger_list' which contains several other lists, one of which is nested\n", + "bigger_list = [[1, 2], [3], [5, 6, 9, [1, 2]]]\n", + "\n", + "# We are initiating a for loop to iterate over each 'small_list' present within the 'bigger_list'\n", + "for small_list in bigger_list:\n", + " # Inside the loop, we are printing the current 'small_list' that the loop is iterating over\n", + " print(small_list)\n", + " # Once the end of the loop is reached, it goes back to the start to process the next 'small_list' in the 'bigger_list'" + ] + }, + { + "cell_type": "markdown", + "id": "202e988c", + "metadata": {}, + "source": [ + "### Exercise:" + ] + }, + { + "cell_type": "markdown", + "id": "66a5e23a", + "metadata": {}, + "source": [ + "You are working in the sales department of a company. At the end of each week, you need to generate a report of total sales made each day. You have a list where each element represents the sales made on each day of the week (from Monday to Sunday). Your task is to calculate the total sales of the week.\n", + "\n", + "To complete this exercise, you need to follow the given steps:\n", + "\n", + "1. **Initialize Variables**:\n", + " Before starting the loop, initialize a variable `total_sales` to 0. This will be used to accumulate the sales of each day. Additionally, initialize `max_sales` to 0.\n", + " \n", + " \n", + "2. **Creating a Loop**:\n", + " Create a for loop where you iterate over each element in the `sales_data` list. You can use a loop structure like this: for day in sales_data:\n", + " \n", + " \n", + "3. **Calculating Total Sales**:\n", + " Inside the loop, add the sales of the current day to `total_sales`. You can do this using an assignment statement like this: total_sales += day\n", + "\n", + "\n", + "\n", + "5. **Printing the Results**:\n", + " After the loop, print the `total_sales` along with appropriate messages to show the total sales of the week and the day with maximum sales." + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "3bd7d85f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4500\n", + "1000\n" + ] + } + ], + "source": [ + "# List representing sales data for seven days (in units)\n", + "sales_data = [200, 300, 400, 500, 600, 700, 800,1000]\n", + "\n", + "# Initialize total_sales to 0 before starting the loop\n", + "total_sales = 0\n", + "# Initialize max_sales before starting the loop\n", + "max_sales = 0\n", + "\n", + "# {YOUR CODE HERE}\n", + "for day in sales_data:\n", + " #print(day)\n", + "\n", + " total_sales +=day\n", + " if day > max_sales:\n", + " max_sales = day\n", + "\n", + "print(total_sales)\n", + "print(max_sales)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "654bbe75", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1000" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max(sales_data )" + ] + }, + { + "cell_type": "markdown", + "id": "0eaad548", + "metadata": { + "lang": "en" + }, + "source": [ + "## The \"If\" statement\n", + "This is the most popular option to control the flow of a program. Conditions allow you to choose between different paths depending on the value of an expression. When you want to execute an action only when some condition is `True`, an \"if\" statement is used:" + ] + }, + { + "cell_type": "markdown", + "id": "580479b3", + "metadata": {}, + "source": [ + "#### if syntax:\n", + "- indentation: `tab / 5 spaces`\n", + "- colon: `:`\n" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "ef3ac3be", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Adult\n", + "True condition\n" + ] + } + ], + "source": [ + "age = 18\n", + "\n", + "if age >= 18:\n", + " print(\"Adult\")\n", + " print(\"True condition\")\n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "305e83a6", + "metadata": { + "lang": "en" + }, + "source": [ + "As we can see, the code block associated with the condition begins after the colon \":\", with an indentation that determines the code block. All statements belonging to the same block must have the same indentation. The block ends when the indent returns to the starting position of the if statement. Remember that Python uses indentation to identify blocks of code." + ] + }, + { + "cell_type": "markdown", + "id": "770fbd1a", + "metadata": { + "lang": "en" + }, + "source": [ + "### else\n", + "Sometimes you want if the condition is not met (and only then) another action is executed. They are mutually exclusive actions. This is achieved with the `else` statement." + ] + }, + { + "cell_type": "markdown", + "id": "median-injection", + "metadata": {}, + "source": [ + "![if](https://www.tutorialspoint.com/scala/images/scala_decision_making.jpg)" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "5c7a1210", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not an adult\n" + ] + } + ], + "source": [ + "age = 17\n", + "\n", + "if age >= 18:\n", + " print(\"Adult\")\n", + "else:\n", + " print(\"Not an adult\")" + ] + }, + { + "cell_type": "markdown", + "id": "baa11a80", + "metadata": { + "lang": "en" + }, + "source": [ + "### elif\n", + "Sometimes there are more than two possibilities. Think of something like:\n", + "\"If Bob can get into the disco, let him in. If not, if he's over 16, recommend the nearby light disco. If not, send him home.\"\n", + "\n", + "We can achieve this with the `elif` clause and chained conditionals:" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "82badc29", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not Human\n" + ] + } + ], + "source": [ + "age = 130\n", + "\n", + "if age >= 130:\n", + " print(\"Not Human\")\n", + "\n", + "elif age >= 21:\n", + " print(\"adult\")\n", + " \n", + "elif age < 21:\n", + " print(\"Not an adult\")\n", + "\n", + "else:\n", + " print(\"It is good practice to have an else\")" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "8dfc81eb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not Human\n" + ] + } + ], + "source": [ + "age = 200\n", + "\n", + "if age >= 130:\n", + " print(\"Not Human\")\n", + "elif age >21:\n", + " print(\"Adult\")" + ] + }, + { + "cell_type": "markdown", + "id": "b3d55cd1", + "metadata": {}, + "source": [ + "### IF; ELIF; ELSE" + ] + }, + { + "cell_type": "markdown", + "id": "50d43411", + "metadata": {}, + "source": [ + "In Python programming, a nested if statement is an if statement that is the target of another if statement. Nested if statements mean an if statement inside another if statement. Yes, Python allows us to nest if statements within if statements. Here is the general form of a nested if statement." + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "36b1c6ee", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "invalid literal for int() with base 10: ''", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[33], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# 1. Takes the input of a user (controlling it's an int)\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m age \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease input your age: \u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# 2. Based on conditions: return something or something else\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m age \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n", + "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: ''" + ] + } + ], + "source": [ + "# 1. Takes the input of a user (controlling it's an int)\n", + "age = int(input(\"Please input your age: \"))\n", + "\n", + "# 2. Based on conditions: return something or something else\n", + "if age > 0:\n", + " if age < 16:\n", + " print(\"You CANNOT get in\")\n", + "\n", + " elif age >= 16 and age < 18:\n", + " print(\"Alcohol free club\")\n", + "\n", + " elif age >= 18:\n", + " print(\"You can get in\")\n", + " \n", + "else:\n", + " print(\"Sorry, wrong info: negative value?\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f2fd273c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ok\n", + "startswith L\n", + "has letters\n", + "Name is Lola\n" + ] + } + ], + "source": [ + "# Assign a string which is intended to represent a password to the variable 'password'\n", + "password = \"12345678909876dcvbnhjytfgvb+\"\n", + "\n", + "# Check if the length of the password is greater than 8, if true, print \"ok\"\n", + "if len(password) > 8:\n", + " print(\"ok\")\n", + "\n", + "# Check if the password contains any letters, if true, print \"startswith L\"\n", + "# (Note: The current condition will always evaluate to True as it's checking if password is non-empty.\n", + "# To check for letters, you might need to modify the condition using any(x.isalpha() for x in password))\n", + "if password: # has letters\n", + " print(\"startswith L\")\n", + " \n", + "if any(x.isalpha() for x in password):\n", + " print(\"has letters\")\n", + " \n", + "# Check if the password contains any numbers, if true, print \"Name is Lola\"\n", + "# (Note: Similar to the previous note, the condition here checks if password is non-empty, \n", + "# to check for numbers you might want to modify the condition using any(x.isdigit() for x in password))\n", + "if password: # has numbers:\n", + " print(\"Name is Lola\")\n", + " \n", + "# (... modify the code for expected output\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3169c74a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "0\n", + "9\n", + "8\n", + "7\n", + "6\n", + "d\n", + "c\n", + "v\n", + "b\n", + "n\n", + "h\n", + "j\n", + "y\n", + "t\n", + "f\n", + "g\n", + "v\n", + "b\n", + "+\n" + ] + } + ], + "source": [ + "for x in password:\n", + " print(x)" + ] + }, + { + "cell_type": "markdown", + "id": "a62738d2", + "metadata": {}, + "source": [ + "### Exercise extension:\n", + "Now use if statements, to calculate on top of total sales, which day had the maximum sales." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "65c946ed", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The day with the max sales is 7 \n" + ] + } + ], + "source": [ + "# List representing sales data for seven days (in units)\n", + "sales_data = [200, 300, 400, 500, 600, 700, 800]\n", + "\n", + "# Initialize total_sales to 0 before starting the loop\n", + "total_sales = 0\n", + "\n", + "# Initialize max_sales before starting the loop\n", + "max_sales = 0\n", + "max_day=1\n", + "\n", + "# {YOUR CODE HERE}\n", + "for i in range (0,len(sales_data)):\n", + " if sales_data[i]>max_sales:\n", + " max_sales=sales_data[i]\n", + " max_day=i+1\n", + "print(f\"The day with the max sales is {max_day} \")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "4202238e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200\n", + "300\n", + "400\n", + "500\n", + "600\n", + "700\n", + "800\n", + "1000\n" + ] + } + ], + "source": [ + "max_sales = 0\n", + "for day in sales_data:\n", + " if day > max_sales:\n", + " max_sales = day\n", + " \n", + " print(max_sales)" + ] + }, + { + "cell_type": "markdown", + "id": "4119b21e", + "metadata": { + "lang": "en" + }, + "source": [ + "# Challenge 🤔\n", + "\n", + "Write a simple program that prints if bob can get into the club\n", + "for any two values ​​of the following variables:\n", + "`print(age_bob,age_minimum)`" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "5d8caf12", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "club\n" + ] + } + ], + "source": [ + "age_bob = 18\n", + "age_minimum = 16\n", + "\n", + "if age_bob >= age_minimum:\n", + " print(\"club\")\n", + "elif age_bob == 18:\n", + " print(\"cannot club\")\n", + "else:\n", + " print(\"cannot club\")" + ] + }, + { + "cell_type": "markdown", + "id": "a9b00fd8", + "metadata": { + "lang": "en" + }, + "source": [ + "### Your examples 🚀" + ] + }, + { + "cell_type": "markdown", + "id": "0833207b", + "metadata": {}, + "source": [ + "Modify your schedule so that when Bob can't get into the club but is over the legal age (age=21)\n", + "send it to disco light" + ] + }, + { + "cell_type": "markdown", + "id": "5f01cb54", + "metadata": { + "lang": "en" + }, + "source": [ + "# Challenge 🤔\n" + ] + }, + { + "cell_type": "markdown", + "id": "b217ff64", + "metadata": { + "lang": "en" + }, + "source": [ + "Let's imagine that we are building the program for a robot that sorts eggs by size. Our robot arm receives information from a scale, which indicates, in grams, the weight of the egg to be classified. The arm must, from the weight, place the egg in one box or another as follows:\n", + "\n", + "- **Box S** (small): weight less than 53 grams.\n", + "- **Box M** (medium) : weight greater than or equal to 53 grams and less than 63 grams.\n", + "- **Box L** (large): weight greater than or equal to 63 grams and less than 73 grams.\n", + "- **XL box** (super large): weight greater than or equal to 73 grams." + ] + }, + { + "cell_type": "markdown", + "id": "4ed9f405", + "metadata": {}, + "source": [] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "d61dba2f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Box L (large)\n" + ] + } + ], + "source": [ + "weight = 70 # in kilograms\n", + "if weight < 53:\n", + " print(\"Box S (small)\")\n", + "elif weight < 63:\n", + " print(\"Box M (medium)\")\n", + "elif weight < 73:\n", + " print(\"Box L (large)\") \n", + "else:\n", + " print(\"Box XL (extra large)\")" + ] + }, + { + "cell_type": "markdown", + "id": "61cfbb7c", + "metadata": {}, + "source": [ + "## Summary\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4524a456", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "7deb669c", + "metadata": {}, + "source": [ + "[Rubber duck debugging](https://www.freecodecamp.org/news/rubber-duck-debugging/#:~:text=The%20idea%20behind%20the%20rubber,loud%20before%20you%20post%20it)\n", + "\n", + "The \"Rubber Duck Theory\" or \"Rubber Duck Debugging\" is a method used to find errors or bugs in a code. In this technique, the programmer explains their code, line by line, to an inanimate object such as a rubber duck. The act of explaining the code in detail often helps the programmer to see errors or improvements that they didn't notice before. This is similar to writing a message on social media and then reading it aloud before posting it, which can sometimes help in spotting errors or reconsidering the message's content." + ] + }, + { + "cell_type": "markdown", + "id": "toxic-romance", + "metadata": {}, + "source": [ + "## Further materials\n", + "\n", + "* [Python docs](https://docs.python.org/3/tutorial/controlflow.html)\n", + "* Short tutorial on [Python Booleans](https://realpython.com/python-boolean/)\n", + "* Just a nice [talk by Feynman](https://www.youtube.com/watch?v=EKWGGDXe5MA) on the principles of computation" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "base", + "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.13.5" + }, + "nbTranslate": { + "displayLangs": [ + "en", + "es" + ], + "hotkey": "alt-a", + "langInMainMenu": true, + "sourceLang": "es", + "targetLang": "en", + "useGoogleTranslate": true + }, + "toc": { + "base_numbering": 1, + "nav_menu": {}, + "number_sections": true, + "sideBar": true, + "skip_h1_title": true, + "title_cell": "Table of Contents", + "title_sidebar": "Contents", + "toc_cell": true, + "toc_position": { + "height": "calc(100% - 180px)", + "left": "10px", + "top": "150px", + "width": "165px" + }, + "toc_section_display": true, + "toc_window_display": true + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 0f72441b8b68f1784add009becf7975c6638ee08 Mon Sep 17 00:00:00 2001 From: GURDITKING4 Date: Sat, 20 Sep 2025 09:32:22 +0100 Subject: [PATCH 6/6] delete unnecessary file --- 2_Flow_Control_I.ipynb | 1680 ---------------------------------------- 1 file changed, 1680 deletions(-) delete mode 100644 2_Flow_Control_I.ipynb diff --git a/2_Flow_Control_I.ipynb b/2_Flow_Control_I.ipynb deleted file mode 100644 index a5547fa..0000000 --- a/2_Flow_Control_I.ipynb +++ /dev/null @@ -1,1680 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "b0f6ad4a", - "metadata": {}, - "source": [ - "# Flow Control 1" - ] - }, - { - "cell_type": "markdown", - "id": "subjective-quarter", - "metadata": { - "toc": true - }, - "source": [ - "

Table of Contents

\n", - "" - ] - }, - { - "cell_type": "markdown", - "id": "63c618c2", - "metadata": {}, - "source": [ - "![](https://media.makeameme.org/created/im-just-going-ca4ce2c899.jpg)" - ] - }, - { - "cell_type": "markdown", - "id": "143f947d", - "metadata": { - "lang": "en" - }, - "source": [ - "## Arithmetic operators" - ] - }, - { - "cell_type": "markdown", - "id": "9aecbca0", - "metadata": { - "lang": "en" - }, - "source": [ - "Arithmetic operators are used to perform arithmetic operations, that is, to manipulate numerical data through mathematical operations such as addition, subtraction or multiplication..." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "8b428e9f", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "20" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a = 10\n", - "b = 2\n", - "\n", - "a + b\n", - "a * b" - ] - }, - { - "cell_type": "markdown", - "id": "7dae48c3", - "metadata": {}, - "source": [ - "`+`, `*`, and all others" - ] - }, - { - "cell_type": "markdown", - "id": "e6aaec42", - "metadata": { - "lang": "en" - }, - "source": [ - "## Assignment operators (Python tricks)" - ] - }, - { - "cell_type": "markdown", - "id": "e3982cf4", - "metadata": {}, - "source": [ - "![](https://i.pinimg.com/236x/2b/dd/c9/2bddc91accccb9b70178c49cf5684bed.jpg)" - ] - }, - { - "cell_type": "markdown", - "id": "3e1e56bd", - "metadata": { - "lang": "en" - }, - "source": [ - "Assignment operators are those that allow you to give a value to a variable or modify it. Python has eight different assignment operators: a simple assignment operator and seven compound assignment operators." - ] - }, - { - "cell_type": "markdown", - "id": "fc5d346e", - "metadata": { - "lang": "en" - }, - "source": [ - "* `=` Simple assignment `a=b`\n", - "* `+=` Addition assignment `a+=b` Simple equivalent `a=a+b`\n", - "* `-=` Subtraction assignment `a-=b` Simple equivalent `a=a-b`\n", - "* `*=` Multiplication Assignment `a*=b` Simple Equivalent `a=a*b`\n", - "* `/=` Division assignment `a/=b` Simple equivalent `a=a/b`\n", - "* `%=` Module mapping `a/=b` Simple equivalent `a=a%b`\n", - "* `//=` Integer Division Assignment `a//=b` Simple Equivalent `a=a//b`\n", - "\n", - "(It is important that you know this, but remember python principles....)" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "7dba4669", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "#a = a + 1\n", - "a =+ 1\n", - "a" - ] - }, - { - "cell_type": "markdown", - "id": "89b42f24", - "metadata": { - "lang": "en" - }, - "source": [ - "The simple assignment operator is the equals symbol (=) and operations performed on it always have the syntax: `variable = expression`. In this type of operation, first, the expression on the right is resolved and the resulting value is assigned to the variable on the left." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "709c2301", - "metadata": {}, - "outputs": [], - "source": [ - "# Take some time, and practice" - ] - }, - { - "cell_type": "markdown", - "id": "71a92117", - "metadata": { - "lang": "en" - }, - "source": [ - "## Relational operators" - ] - }, - { - "cell_type": "markdown", - "id": "38d5146c", - "metadata": { - "lang": "en" - }, - "source": [ - "\n", - "Relational operators are symbols used to compare two values ​​or expressions. The result of the evaluation with these two operators can be True, if the comparison is true, or False, if the comparison is false." - ] - }, - { - "cell_type": "markdown", - "id": "b7d7c2b5", - "metadata": { - "lang": "en" - }, - "source": [ - "* `==` Equal to `a==b`\n", - "* `!=` Not equal to `a!=b`\n", - "* `>` Greater than `a>b`\n", - "* `<` Lesser than `a=` Greater than or equal to `a>=b`\n", - "* `<=` Less than or equal to `a<=b`" - ] - }, - { - "cell_type": "markdown", - "id": "6bb2bc2a", - "metadata": { - "lang": "en" - }, - "source": [ - "Note the difference between a single equals sign (=), which is an assignment, and a double equals sign (==), which is a relational operator\n", - "\n", - "**Hint: Use parentheses to maintain the order of operations." - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "ac9ccfb3", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# What is the difference between these two?\n", - "a = 10\n", - "a == 10" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "c00bf648", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 5, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a != 10" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "3dde583b", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a > b" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "4efdcce8", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "a < b" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "16a5b605", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "x = (a <= b)\n", - "x" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "de053fec", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "y = (b <= 20)\n", - "y" - ] - }, - { - "cell_type": "markdown", - "id": "5753abc4", - "metadata": {}, - "source": [ - "### Boolean Algebra" - ] - }, - { - "cell_type": "markdown", - "id": "0bf8a056", - "metadata": { - "lang": "en" - }, - "source": [ - "With just these two `True`|`False` values ​​we can create a whole branch of mathematics called [Boolean Algebra](https://en.wikipedia.org/wiki/Boolean_algebra#Laws). While in regular Algebra the basic operations are addition and multiplication, the main operations in Boolean Algebra are conjunction (and), disjunction (or), and negation (not). **It is the formalism used to describe the logical operations**.\n", - "\n", - "In [Python we write these operations](https://www.geeksforgeeks.org/python-3-logical-operators/?ref=rp) as:\n", - "\n", - "- `==`\n", - "* `x` and `y`\n", - "* `x` or `y`\n", - "* not `x`\n", - "* Extra: `^` [caret operator](https://stackoverflow.com/questions/2451386/what-does-the-caret-operator-do)\n", - "\n", - "Although the meaning of these operations is clear, we can fully define them with the so-called \"truth table\":" - ] - }, - { - "cell_type": "markdown", - "id": "coral-armenia", - "metadata": {}, - "source": [ - "![tablas](http://elclubdelautodidacta.es/wp/wp-content/uploads/2012/11/python-logicos.png)" - ] - }, - { - "cell_type": "markdown", - "id": "c39be317", - "metadata": { - "lang": "en" - }, - "source": [ - "We can also include the EXCLUSIVE OR, which is true when one and only one of the operands is true, but strictly you must know that it is derived from the three basic ones. Its representation is ^, the hat or caret." - ] - }, - { - "cell_type": "markdown", - "id": "244fb7c7", - "metadata": {}, - "source": [ - "### **equals** vs **is**" - ] - }, - { - "cell_type": "markdown", - "id": "c2fc6ff2", - "metadata": {}, - "source": [ - "[Article on Equal vs Is](https://towardsdatascience.com/whats-the-difference-between-is-and-in-python-dc26406c85ad)" - ] - }, - { - "cell_type": "markdown", - "id": "e8287044", - "metadata": {}, - "source": [ - "`==` -> points to the value
\n", - "`is` -> looks for identity" - ] - }, - { - "cell_type": "markdown", - "id": "1c4f6e09", - "metadata": {}, - "source": [ - "| Operator | Compares | Returns `True` if |\n", - "|----------|------------------|-----------------------------|\n", - "| `==` | Values | Values are equal |\n", - "| `is` | Object identity | Both refer to same object |\n" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "873ada8d", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] - } - ], - "source": [ - "# Example 1: Comparing Numbers\n", - "a = 5\n", - "b = 5\n", - "\n", - "print(a == b) # True, because a and b are the same number" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "63b2eb26", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] - } - ], - "source": [ - "print(a is b)" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "5a1aa6df", - "metadata": {}, - "outputs": [], - "source": [ - "# Example 2: comparing lists\n", - "list1 = [1, 2, 3]\n", - "list2 = [1, 2, 3]" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "id": "f714c908", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] - } - ], - "source": [ - "print(list1 == list2) # True, because the content of the list is the same" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "id": "5a5f1e05", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "False\n" - ] - } - ], - "source": [ - "print(list1 is list2) # False, because list1 and list2 are two different objects" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "id": "44e6eefc", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "True\n" - ] - } - ], - "source": [ - "a = 5\n", - "b = 5\n", - "\n", - "c = 5\n", - "\n", - "a = c\n", - "b = c\n", - "\n", - "\n", - "print(a is b) # True? yes, but wierd: a and b point to the same \"thing\" due to an optimization in python" - ] - }, - { - "cell_type": "markdown", - "id": "29d1ae12", - "metadata": {}, - "source": [ - "## NOT\n", - "\n", - "- The **not** operator. The **not** operator is an operator that returns the opposite value of the evaluated expression. If the expression has the value True, it returns False. Conversely, if the expression has the value False, it returns True." - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "id": "70ecf121", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "not(list1 == list2)" - ] - }, - { - "cell_type": "markdown", - "id": "d5122028", - "metadata": { - "lang": "en" - }, - "source": [ - "## AND\n", - "- The **and** operator evaluates whether both expressions are true. If both expressions are true, it returns True. If either expression is false, it returns False. These types of tables are formally known as \"truth tables\"." - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "id": "6096259b", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 17, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "## TRUE FALSE\n", - "(list1 == list2) and (a == list2) " - ] - }, - { - "cell_type": "markdown", - "id": "ddb3ca4d", - "metadata": { - "lang": "en" - }, - "source": [ - "## OR\n", - "The **or** operator evaluates whether either expression is true, that is, it returns True if either expression is true and False when both expressions are false." - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "id": "3c409f34", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Same to (lista1 == lista2) | (a == lista2) \n", - "(list1 == list2) or (a == list2) " - ] - }, - { - "cell_type": "markdown", - "id": "ef5ced3e", - "metadata": {}, - "source": [ - "## EXERCISE" - ] - }, - { - "cell_type": "markdown", - "id": "e373cd72", - "metadata": {}, - "source": [ - "#### Exercise: Exploring Relational Operators\n", - "\n", - "In this exercise, we will explore the various relational operators available in Python. These operators are primarily used to establish relationships between different values and are essential in decision-making structures, although we won't be using them in that capacity in this exercise. Using these operators, please perform the following tasks:\n", - "\n", - "#### Task 1: Equal to Operator (==)\n", - "1. Create two strings and check if they are equal using `==`. Print the result.\n", - "2. Create two lists with equal elements and check if they are equal using `==`. Print the result.\n", - "\n", - "#### Task 2: Not Equal to Operator (!=)\n", - "1. Create two integers and check if they are not equal using `!=`. Print the result.\n", - "2. Use the `input()` function to get two inputs from the user and check if they are not equal using `!=`. Print the result.\n", - "\n", - "#### Task 3: Greater than and Lesser than Operators (>, <)\n", - "1. Create two floating-point numbers and check if one is greater than the other using `>`. Print the result.\n", - "2. Create two floating-point numbers and check if one is lesser than the other using `<`. Print the result.\n", - "\n", - "#### Task 4: Greater than or Equal to and Less than or Equal to Operators (>=, <=)\n", - "1. Create two integers and check if one is greater than or equal to the other using `>=`. Print the result.\n", - "2. Create two integers and check if one is less than or equal to the other using `<=`. Print the result." - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "id": "5de219ad", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "False\n", - "True\n", - "False\n", - "True\n", - "False\n", - "True\n" - ] - } - ], - "source": [ - "# your code here\n", - "str1 = \"Hello\"\n", - "str2 = \"Bye\"\n", - "print(str1 == str2)\n", - "\n", - "int1 = 16\n", - "int2 = 25\n", - "print(int1 != int2)\n", - "\n", - "float1 = 24.55\n", - "float2 = 33.98\n", - "print(float1 > float2)\n", - "print(float1 < float2)\n", - "\n", - "int3 = 16\n", - "int4 = 25\n", - "print(int3 >= int4)\n", - "print(int3 <= int4)" - ] - }, - { - "cell_type": "markdown", - "id": "attended-assurance", - "metadata": {}, - "source": [ - "## Loops an stuff\n" - ] - }, - { - "cell_type": "markdown", - "id": "b0a9f8bc", - "metadata": {}, - "source": [ - "![](https://i1.sndcdn.com/artworks-SvzDuFfyt3AJ9fO1-iQrV9Q-t500x500.jpg)" - ] - }, - { - "cell_type": "markdown", - "id": "3e0ac95f", - "metadata": { - "lang": "en" - }, - "source": [ - "In programming, a **loop** is a fundamental concept that allows a block of code to be repeated multiple times. It facilitates the execution of repetitive tasks without needing to write the same lines of code over and over again, promoting both efficiency and readability. The basic idea is to loop through a block of code as long as a specified condition remains true.\n", - "\n", - "There are generally two types of loops you'll encounter:\n", - "\n", - "1. **For Loop**: Generally used to iterate over a collection of items, such as elements in a list, tuple, or string. It will execute a block of code for each item in the collection, hence making it simpler and cleaner to perform the same action on multiple items.\n", - "\n", - "2. **While Loop**: This type of loop continues to execute a block of code as long as a particular condition is met. It's crucial to update the variables influencing the condition inside the loop, to avoid creating an infinite loop.\n", - "\n", - "Loops are incredibly powerful tools in programming, helping to save time and reduce the potential for errors in your code. As you progress in your learning, you will find loops are essential components in many programming solutions, facilitating the creation of complex, efficient, and innovative solutions to a wide range of problems.\n", - "\n", - "In the following sections, we will explore both types of loops in more detail, with examples to illustrate their usage and versatility in programming tasks.\n", - "\n", - "Happy looping!" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "id": "1d6224df", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'BANANA'" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "lista= [\"melon\", \"apple\", \"banana\"]\n", - "lista[0].upper()\n", - "lista[1].upper()\n", - "lista[2].upper()" - ] - }, - { - "cell_type": "markdown", - "id": "aa20ffc4", - "metadata": {}, - "source": [ - "### Pseudocode" - ] - }, - { - "cell_type": "markdown", - "id": "c2a2b91b", - "metadata": { - "lang": "en" - }, - "source": [ - "Before we dive into the world of loops, let's take a moment to understand what \"Pseudocode\" is. Pseudocode is a high-level description of the functioning of a computer program or algorithm. It utilizes the structural conventions of programming languages but is designed to be read by humans rather than machines. \n", - "\n", - "In pseudocode, we are not concerned with precise syntax that a programming language might require. Instead, we focus on expressing the logic and the steps needed to solve a problem or accomplish a task, often in natural language mixed with some common programming structures. This means that pseudocode does not have a strict syntax and can vary widely.\n", - "\n", - "The main objective of writing pseudocode is to outline the structure of the program and delineate its logical flow without getting caught up in the specific details of a particular programming language. This makes it a useful tool for planning and designing programs, where the emphasis is on what to do rather than how to do it.\n", - "\n", - "Here are some general characteristics of pseudocode:\n", - "1. **Simplicity**: It should be simple and easy to understand.\n", - "2. **Clarity**: Each step is clearly defined without ambiguity.\n", - "3. **Language-independent**: It does not adhere to the syntax of any specific programming language.\n", - "4. **Focus on Logic**: The primary focus is on the logic and flow of the algorithm, rather than the specific syntax of a programming language.\n", - "\n", - "As you begin to work with loops and other programming structures, you'll find that creating pseudocode can be a valuable first step in the coding process, helping you to organize your thoughts and create a roadmap for your program before you start writing actual code." - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "id": "a1b077ff", - "metadata": {}, - "outputs": [], - "source": [ - "# a list of elements\n", - "# loop through the list \n", - "# and check if the type is a number\n", - "\n", - "# if the type is a number\n", - "# i do stuff\n", - "\n", - "# WHAT IS IT I HAVE?: LIST OF LISTS WITH NUMBERS\n", - " # original = [[1, 2], [3], [5, 6, 9]]\n", - " # 1. Create a new_list\n", - " # 2. GOAL: Take the element out\n", - " # 3. Access the element\n", - " # 4. loop original list: [1, 2]\n", - " # 5. loop the i/individual_list again\n", - " # 6: add to new_list\n", - " # 7: print\n", - " \n", - "# GOAL: WHAT RESULT I NEED TO RETURN\n", - " # [1, 2, 3, 4, 5, 6]\n", - " \n", - "# always print" - ] - }, - { - "cell_type": "markdown", - "id": "4690d4e0", - "metadata": {}, - "source": [ - "Quick infor card:\n", - "\n", - "In Python, the `range()` function is used to generate a sequence of numbers over time. It is widely used in loops to control the number of iterations. The function has three parameters: start, stop, and step.\n", - "\n", - "- **Start**: This parameter specifies the starting point of the sequence. It is optional, and the default value is 0.\n", - "- **Stop**: This parameter specifies the endpoint of the sequence. This value is not included in the sequence.\n", - "- **Step**: This parameter specifies the increment between each number in the sequence. It is optional, and the default value is 1.\n", - "\n", - "Here is the syntax for the `range()` function:\n", - "\n", - "- range(start, stop, step)\n", - "\n", - "Some example:" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "id": "d024b13d", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "range(1, 6)" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "range(1, 6)" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "id": "89f2329b", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0\n", - "1\n", - "2\n", - "3\n", - "4\n", - "5\n", - "Hi\n" - ] - } - ], - "source": [ - "for i in range(6):\n", - " print(i)\n", - "print(\"Hi\")\n" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "id": "9fdcf991", - "metadata": {}, - "outputs": [], - "source": [ - "# play here with range" - ] - }, - { - "cell_type": "markdown", - "id": "fee3afff", - "metadata": {}, - "source": [ - "### Loop example\n", - "Let's do more complex stuff:" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "id": "23dd3aa0", - "metadata": { - "scrolled": true - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2]\n", - "1\n", - "2\n", - "[3]\n", - "3\n", - "[5, 6, 9]\n", - "5\n", - "6\n", - "9\n", - "[1, 2, 3, 5, 6, 9]\n" - ] - } - ], - "source": [ - "# This is the original list of lists where each element is a list containing integers.\n", - "original = [[1, 2], [3], [5, 6, 9]]\n", - "# A new empty list is initialized to store the individual elements after flattening the original list.\n", - "new_list = []\n", - "\n", - "# The outer loop iterates through each individual list inside the original list.\n", - "for individual_list in original:\n", - " print(individual_list)\n", - " # The inner loop iterates through each element in the currently selected individual list (from the outer loop).\n", - " for each_element in individual_list:\n", - " # The current element from the individual list is appended to the new list.\n", - " print(each_element)\n", - " new_list.append(each_element)\n", - "\n", - "# Printing the new_list to see the final output.\n", - "print(new_list)" - ] - }, - { - "cell_type": "markdown", - "id": "8a77f835", - "metadata": {}, - "source": [ - "Debug what is happenin:" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "id": "a36780d7", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[1, 2]\n", - "[3]\n", - "[5, 6, 9, [1, 2]]\n" - ] - } - ], - "source": [ - "# We are defining a list named 'bigger_list' which contains several other lists, one of which is nested\n", - "bigger_list = [[1, 2], [3], [5, 6, 9, [1, 2]]]\n", - "\n", - "# We are initiating a for loop to iterate over each 'small_list' present within the 'bigger_list'\n", - "for small_list in bigger_list:\n", - " # Inside the loop, we are printing the current 'small_list' that the loop is iterating over\n", - " print(small_list)\n", - " # Once the end of the loop is reached, it goes back to the start to process the next 'small_list' in the 'bigger_list'" - ] - }, - { - "cell_type": "markdown", - "id": "202e988c", - "metadata": {}, - "source": [ - "### Exercise:" - ] - }, - { - "cell_type": "markdown", - "id": "66a5e23a", - "metadata": {}, - "source": [ - "You are working in the sales department of a company. At the end of each week, you need to generate a report of total sales made each day. You have a list where each element represents the sales made on each day of the week (from Monday to Sunday). Your task is to calculate the total sales of the week.\n", - "\n", - "To complete this exercise, you need to follow the given steps:\n", - "\n", - "1. **Initialize Variables**:\n", - " Before starting the loop, initialize a variable `total_sales` to 0. This will be used to accumulate the sales of each day. Additionally, initialize `max_sales` to 0.\n", - " \n", - " \n", - "2. **Creating a Loop**:\n", - " Create a for loop where you iterate over each element in the `sales_data` list. You can use a loop structure like this: for day in sales_data:\n", - " \n", - " \n", - "3. **Calculating Total Sales**:\n", - " Inside the loop, add the sales of the current day to `total_sales`. You can do this using an assignment statement like this: total_sales += day\n", - "\n", - "\n", - "\n", - "5. **Printing the Results**:\n", - " After the loop, print the `total_sales` along with appropriate messages to show the total sales of the week and the day with maximum sales." - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "id": "3bd7d85f", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "4500\n", - "1000\n" - ] - } - ], - "source": [ - "# List representing sales data for seven days (in units)\n", - "sales_data = [200, 300, 400, 500, 600, 700, 800,1000]\n", - "\n", - "# Initialize total_sales to 0 before starting the loop\n", - "total_sales = 0\n", - "# Initialize max_sales before starting the loop\n", - "max_sales = 0\n", - "\n", - "# {YOUR CODE HERE}\n", - "for day in sales_data:\n", - " #print(day)\n", - "\n", - " total_sales +=day\n", - " if day > max_sales:\n", - " max_sales = day\n", - "\n", - "print(total_sales)\n", - "print(max_sales)" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "id": "654bbe75", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1000" - ] - }, - "execution_count": 28, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "max(sales_data )" - ] - }, - { - "cell_type": "markdown", - "id": "0eaad548", - "metadata": { - "lang": "en" - }, - "source": [ - "## The \"If\" statement\n", - "This is the most popular option to control the flow of a program. Conditions allow you to choose between different paths depending on the value of an expression. When you want to execute an action only when some condition is `True`, an \"if\" statement is used:" - ] - }, - { - "cell_type": "markdown", - "id": "580479b3", - "metadata": {}, - "source": [ - "#### if syntax:\n", - "- indentation: `tab / 5 spaces`\n", - "- colon: `:`\n" - ] - }, - { - "cell_type": "code", - "execution_count": 29, - "id": "ef3ac3be", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Adult\n", - "True condition\n" - ] - } - ], - "source": [ - "age = 18\n", - "\n", - "if age >= 18:\n", - " print(\"Adult\")\n", - " print(\"True condition\")\n", - " " - ] - }, - { - "cell_type": "markdown", - "id": "305e83a6", - "metadata": { - "lang": "en" - }, - "source": [ - "As we can see, the code block associated with the condition begins after the colon \":\", with an indentation that determines the code block. All statements belonging to the same block must have the same indentation. The block ends when the indent returns to the starting position of the if statement. Remember that Python uses indentation to identify blocks of code." - ] - }, - { - "cell_type": "markdown", - "id": "770fbd1a", - "metadata": { - "lang": "en" - }, - "source": [ - "### else\n", - "Sometimes you want if the condition is not met (and only then) another action is executed. They are mutually exclusive actions. This is achieved with the `else` statement." - ] - }, - { - "cell_type": "markdown", - "id": "median-injection", - "metadata": {}, - "source": [ - "![if](https://www.tutorialspoint.com/scala/images/scala_decision_making.jpg)" - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "id": "5c7a1210", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Not an adult\n" - ] - } - ], - "source": [ - "age = 17\n", - "\n", - "if age >= 18:\n", - " print(\"Adult\")\n", - "else:\n", - " print(\"Not an adult\")" - ] - }, - { - "cell_type": "markdown", - "id": "baa11a80", - "metadata": { - "lang": "en" - }, - "source": [ - "### elif\n", - "Sometimes there are more than two possibilities. Think of something like:\n", - "\"If Bob can get into the disco, let him in. If not, if he's over 16, recommend the nearby light disco. If not, send him home.\"\n", - "\n", - "We can achieve this with the `elif` clause and chained conditionals:" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "id": "82badc29", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Not Human\n" - ] - } - ], - "source": [ - "age = 130\n", - "\n", - "if age >= 130:\n", - " print(\"Not Human\")\n", - "\n", - "elif age >= 21:\n", - " print(\"adult\")\n", - " \n", - "elif age < 21:\n", - " print(\"Not an adult\")\n", - "\n", - "else:\n", - " print(\"It is good practice to have an else\")" - ] - }, - { - "cell_type": "code", - "execution_count": 32, - "id": "8dfc81eb", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Not Human\n" - ] - } - ], - "source": [ - "age = 200\n", - "\n", - "if age >= 130:\n", - " print(\"Not Human\")\n", - "elif age >21:\n", - " print(\"Adult\")" - ] - }, - { - "cell_type": "markdown", - "id": "b3d55cd1", - "metadata": {}, - "source": [ - "### IF; ELIF; ELSE" - ] - }, - { - "cell_type": "markdown", - "id": "50d43411", - "metadata": {}, - "source": [ - "In Python programming, a nested if statement is an if statement that is the target of another if statement. Nested if statements mean an if statement inside another if statement. Yes, Python allows us to nest if statements within if statements. Here is the general form of a nested if statement." - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "id": "36b1c6ee", - "metadata": {}, - "outputs": [ - { - "ename": "ValueError", - "evalue": "invalid literal for int() with base 10: ''", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[33], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# 1. Takes the input of a user (controlling it's an int)\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m age \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28minput\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease input your age: \u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[0;32m 4\u001b[0m \u001b[38;5;66;03m# 2. Based on conditions: return something or something else\u001b[39;00m\n\u001b[0;32m 5\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m age \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n", - "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: ''" - ] - } - ], - "source": [ - "# 1. Takes the input of a user (controlling it's an int)\n", - "age = int(input(\"Please input your age: \"))\n", - "\n", - "# 2. Based on conditions: return something or something else\n", - "if age > 0:\n", - " if age < 16:\n", - " print(\"You CANNOT get in\")\n", - "\n", - " elif age >= 16 and age < 18:\n", - " print(\"Alcohol free club\")\n", - "\n", - " elif age >= 18:\n", - " print(\"You can get in\")\n", - " \n", - "else:\n", - " print(\"Sorry, wrong info: negative value?\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f2fd273c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "ok\n", - "startswith L\n", - "has letters\n", - "Name is Lola\n" - ] - } - ], - "source": [ - "# Assign a string which is intended to represent a password to the variable 'password'\n", - "password = \"12345678909876dcvbnhjytfgvb+\"\n", - "\n", - "# Check if the length of the password is greater than 8, if true, print \"ok\"\n", - "if len(password) > 8:\n", - " print(\"ok\")\n", - "\n", - "# Check if the password contains any letters, if true, print \"startswith L\"\n", - "# (Note: The current condition will always evaluate to True as it's checking if password is non-empty.\n", - "# To check for letters, you might need to modify the condition using any(x.isalpha() for x in password))\n", - "if password: # has letters\n", - " print(\"startswith L\")\n", - " \n", - "if any(x.isalpha() for x in password):\n", - " print(\"has letters\")\n", - " \n", - "# Check if the password contains any numbers, if true, print \"Name is Lola\"\n", - "# (Note: Similar to the previous note, the condition here checks if password is non-empty, \n", - "# to check for numbers you might want to modify the condition using any(x.isdigit() for x in password))\n", - "if password: # has numbers:\n", - " print(\"Name is Lola\")\n", - " \n", - "# (... modify the code for expected output\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3169c74a", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1\n", - "2\n", - "3\n", - "4\n", - "5\n", - "6\n", - "7\n", - "8\n", - "9\n", - "0\n", - "9\n", - "8\n", - "7\n", - "6\n", - "d\n", - "c\n", - "v\n", - "b\n", - "n\n", - "h\n", - "j\n", - "y\n", - "t\n", - "f\n", - "g\n", - "v\n", - "b\n", - "+\n" - ] - } - ], - "source": [ - "for x in password:\n", - " print(x)" - ] - }, - { - "cell_type": "markdown", - "id": "a62738d2", - "metadata": {}, - "source": [ - "### Exercise extension:\n", - "Now use if statements, to calculate on top of total sales, which day had the maximum sales." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "65c946ed", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The day with the max sales is 7 \n" - ] - } - ], - "source": [ - "# List representing sales data for seven days (in units)\n", - "sales_data = [200, 300, 400, 500, 600, 700, 800]\n", - "\n", - "# Initialize total_sales to 0 before starting the loop\n", - "total_sales = 0\n", - "\n", - "# Initialize max_sales before starting the loop\n", - "max_sales = 0\n", - "max_day=1\n", - "\n", - "# {YOUR CODE HERE}\n", - "for i in range (0,len(sales_data)):\n", - " if sales_data[i]>max_sales:\n", - " max_sales=sales_data[i]\n", - " max_day=i+1\n", - "print(f\"The day with the max sales is {max_day} \")\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 35, - "id": "4202238e", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "200\n", - "300\n", - "400\n", - "500\n", - "600\n", - "700\n", - "800\n", - "1000\n" - ] - } - ], - "source": [ - "max_sales = 0\n", - "for day in sales_data:\n", - " if day > max_sales:\n", - " max_sales = day\n", - " \n", - " print(max_sales)" - ] - }, - { - "cell_type": "markdown", - "id": "4119b21e", - "metadata": { - "lang": "en" - }, - "source": [ - "# Challenge 🤔\n", - "\n", - "Write a simple program that prints if bob can get into the club\n", - "for any two values ​​of the following variables:\n", - "`print(age_bob,age_minimum)`" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "5d8caf12", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "club\n" - ] - } - ], - "source": [ - "age_bob = 18\n", - "age_minimum = 16\n", - "\n", - "if age_bob >= age_minimum:\n", - " print(\"club\")\n", - "elif age_bob == 18:\n", - " print(\"cannot club\")\n", - "else:\n", - " print(\"cannot club\")" - ] - }, - { - "cell_type": "markdown", - "id": "a9b00fd8", - "metadata": { - "lang": "en" - }, - "source": [ - "### Your examples 🚀" - ] - }, - { - "cell_type": "markdown", - "id": "0833207b", - "metadata": {}, - "source": [ - "Modify your schedule so that when Bob can't get into the club but is over the legal age (age=21)\n", - "send it to disco light" - ] - }, - { - "cell_type": "markdown", - "id": "5f01cb54", - "metadata": { - "lang": "en" - }, - "source": [ - "# Challenge 🤔\n" - ] - }, - { - "cell_type": "markdown", - "id": "b217ff64", - "metadata": { - "lang": "en" - }, - "source": [ - "Let's imagine that we are building the program for a robot that sorts eggs by size. Our robot arm receives information from a scale, which indicates, in grams, the weight of the egg to be classified. The arm must, from the weight, place the egg in one box or another as follows:\n", - "\n", - "- **Box S** (small): weight less than 53 grams.\n", - "- **Box M** (medium) : weight greater than or equal to 53 grams and less than 63 grams.\n", - "- **Box L** (large): weight greater than or equal to 63 grams and less than 73 grams.\n", - "- **XL box** (super large): weight greater than or equal to 73 grams." - ] - }, - { - "cell_type": "markdown", - "id": "4ed9f405", - "metadata": {}, - "source": [] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "d61dba2f", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Box L (large)\n" - ] - } - ], - "source": [ - "weight = 70 # in kilograms\n", - "if weight < 53:\n", - " print(\"Box S (small)\")\n", - "elif weight < 63:\n", - " print(\"Box M (medium)\")\n", - "elif weight < 73:\n", - " print(\"Box L (large)\") \n", - "else:\n", - " print(\"Box XL (extra large)\")" - ] - }, - { - "cell_type": "markdown", - "id": "61cfbb7c", - "metadata": {}, - "source": [ - "## Summary\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4524a456", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "7deb669c", - "metadata": {}, - "source": [ - "[Rubber duck debugging](https://www.freecodecamp.org/news/rubber-duck-debugging/#:~:text=The%20idea%20behind%20the%20rubber,loud%20before%20you%20post%20it)\n", - "\n", - "The \"Rubber Duck Theory\" or \"Rubber Duck Debugging\" is a method used to find errors or bugs in a code. In this technique, the programmer explains their code, line by line, to an inanimate object such as a rubber duck. The act of explaining the code in detail often helps the programmer to see errors or improvements that they didn't notice before. This is similar to writing a message on social media and then reading it aloud before posting it, which can sometimes help in spotting errors or reconsidering the message's content." - ] - }, - { - "cell_type": "markdown", - "id": "toxic-romance", - "metadata": {}, - "source": [ - "## Further materials\n", - "\n", - "* [Python docs](https://docs.python.org/3/tutorial/controlflow.html)\n", - "* Short tutorial on [Python Booleans](https://realpython.com/python-boolean/)\n", - "* Just a nice [talk by Feynman](https://www.youtube.com/watch?v=EKWGGDXe5MA) on the principles of computation" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "base", - "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.13.5" - }, - "nbTranslate": { - "displayLangs": [ - "en", - "es" - ], - "hotkey": "alt-a", - "langInMainMenu": true, - "sourceLang": "es", - "targetLang": "en", - "useGoogleTranslate": true - }, - "toc": { - "base_numbering": 1, - "nav_menu": {}, - "number_sections": true, - "sideBar": true, - "skip_h1_title": true, - "title_cell": "Table of Contents", - "title_sidebar": "Contents", - "toc_cell": true, - "toc_position": { - "height": "calc(100% - 180px)", - "left": "10px", - "top": "150px", - "width": "165px" - }, - "toc_section_display": true, - "toc_window_display": true - } - }, - "nbformat": 4, - "nbformat_minor": 5 -}