summaryrefslogtreecommitdiff
path: root/tutorials/module_1/notebook_1/control_structures.ipynb
blob: ce69e3ac5e1216188e71fdbc406833c1d9006c5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Control Structures\n",
    "\n",
    "Control structures allow us to control the flow of execution in a Python\n",
    "program. The two main types are **conditional statements (`if`\n",
    "statements)** and **loops (`for` and `while` loops)**.\n",
    "\n",
    "## Conditional Statements\n",
    "\n",
    "Conditional statements allow a program to execute different blocks of\n",
    "code depending on whether a given condition is `True` or `False`. These\n",
    "conditions are typically comparisons, such as checking if one number is\n",
    "greater than another.\n",
    "\n",
    "### The `if` Statement\n",
    "\n",
    "The simplest form of a conditional statement is the `if` statement. If\n",
    "the condition evaluates to `True`, the indented block of code runs.\n",
    "Otherwise, the program moves on without executing the statement.\n",
    "\n",
    "For example, consider a situation where we need to determine if a person\n",
    "is an adult based on their age. If the age is 18 or greater, we print a\n",
    "message saying they are an adult.\n",
    "\n",
    "### The `if-else` Statement\n",
    "\n",
    "Sometimes, we need to specify what should happen if the condition is\n",
    "`False`. The `else` clause allows us to handle this case. Instead of\n",
    "just skipping over the block, the program can execute an alternative\n",
    "action.\n",
    "\n",
    "For instance, if a person is younger than 18, they are considered a\n",
    "minor. If the condition of being an adult is not met, the program will\n",
    "print a message indicating that the person is a minor.\n",
    "\n",
    "### The `if-elif-else` Statement\n",
    "\n",
    "When dealing with multiple conditions, the `if-elif-else` structure is\n",
    "useful. The program evaluates conditions in order, executing the first\n",
    "one that is `True`. If none of the conditions are met, the `else` block\n",
    "runs.\n",
    "\n",
    "For example, in a grading system, different score ranges correspond to\n",
    "different letter grades. If a student’s score is 90 or higher, they\n",
    "receive an “A”. If it’s between 80 and 89, they get a “B”, and so on. If\n",
    "none of the conditions match, they receive an “F”.\n",
    "\n",
    "### Nested `if` Statements\n",
    "\n",
    "Sometimes, we need to check conditions within other conditions. This is\n",
    "known as **nesting**. For example, if we first determine that a person\n",
    "is an adult, we can then check if they are a student. Based on that\n",
    "information, we print different messages.\n",
    "\n",
    "``` python\n",
    "# Getting user input for the student's score\n",
    "score = int(input(\"Enter the student's score (0-100): \"))\n",
    "\n",
    "if 0 <= score <= 100:\n",
    "    if score >= 90:\n",
    "        grade = \"A\"\n",
    "    elif score >= 80:\n",
    "        grade = \"B\"\n",
    "    elif score >= 70:\n",
    "        grade = \"C\"\n",
    "    elif score >= 60:\n",
    "        grade = \"D\"\n",
    "    else:\n",
    "        grade = \"F\"  # Score below 60 is a failing grade\n",
    "\n",
    "\n",
    "    if grade == \"F\":\n",
    "        print(\"The student has failed.\")\n",
    "        retake_eligible = input(\"Is the student eligible for a retest? (yes/no): \").strip().lower()\n",
    "        \n",
    "        if retake_eligible == \"yes\":\n",
    "            print(\"The student is eligible for a retest.\")\n",
    "        else:\n",
    "            print(\"The student has failed the course and must retake it next semester.\")\n",
    "\n",
    "    \n",
    "```\n",
    "\n",
    "------------------------------------------------------------------------\n",
    "\n",
    "## Loops in Python\n",
    "\n",
    "Loops allow a program to execute a block of code multiple times. This is\n",
    "especially useful for tasks such as processing lists of data, performing\n",
    "repetitive calculations, or automating tasks.\n",
    "\n",
    "### The `for` Loop\n",
    "\n",
    "A `for` loop iterates over a sequence, such as a list, tuple, string, or\n",
    "a range of numbers. Each iteration assigns the next value in the\n",
    "sequence to a loop variable, which can then be used inside the loop.\n",
    "\n",
    "For instance, if we have a list of fruits and want to print each fruit’s\n",
    "name, a `for` loop can iterate over the list and display each item.\n",
    "\n",
    "Another useful feature of `for` loops is the `range()` function, which\n",
    "generates a sequence of numbers. This is commonly used when we need to\n",
    "repeat an action a specific number of times. For example, iterating from\n",
    "0 to 4 allows us to print a message five times.\n",
    "\n",
    "Additionally, the `enumerate()` function can be used to loop through a\n",
    "list while keeping track of the index of each item. This is useful when\n",
    "both the position and the value in a sequence are needed.\n",
    "\n",
    "``` python\n",
    "fruits = [\"apple\", \"banana\", \"cherry\"]  \n",
    "for x in fruits:  \n",
    "  print(x)\n",
    "```\n",
    "\n",
    "``` python\n",
    "for x in range(6):  \n",
    "  print(x)  \n",
    "else:  \n",
    "  print(\"Finally finished!\")\n",
    "```\n",
    "\n",
    "### The `while` Loop\n",
    "\n",
    "Unlike `for` loops, which iterate over a sequence, `while` loops\n",
    "continue running as long as a specified condition remains `True`. This\n",
    "is useful when the number of iterations is not known in advance.\n",
    "\n",
    "For example, a countdown timer can be implemented using a `while` loop.\n",
    "The loop will continue decreasing the count until it reaches zero.\n",
    "\n",
    "It’s important to be careful with `while` loops to avoid infinite loops,\n",
    "which occur when the condition never becomes `False`. To prevent this,\n",
    "ensure that the condition will eventually change during the execution of\n",
    "the loop.\n",
    "\n",
    "A `while` loop can also be used to wait for a certain event to occur.\n",
    "For example, in interactive programs, a `while True` loop can keep\n",
    "running until the user provides a valid input, at which point we break\n",
    "out of the loop.\n",
    "\n",
    "``` python\n",
    "i = 1  \n",
    "while i < 6:  \n",
    "  print(i)  \n",
    "  i += 1\n",
    "```\n",
    "\n",
    "------------------------------------------------------------------------\n",
    "\n",
    "## Loop Control Statements\n",
    "\n",
    "Python provides special statements to control the behavior of loops.\n",
    "These can be used to break out of a loop, skip certain iterations, or\n",
    "simply include a placeholder for future code.\n",
    "\n",
    "### The `break` Statement\n",
    "\n",
    "The `break` statement is used to exit a loop before it has iterated\n",
    "through all its elements. When the `break` statement is encountered, the\n",
    "loop stops immediately, and the program continues executing the next\n",
    "statement outside the loop.\n",
    "\n",
    "For instance, if we are searching for a specific value in a list, we can\n",
    "use a `break` statement to stop the loop as soon as we find the item,\n",
    "instead of continuing unnecessary iterations.\n",
    "\n",
    "### The `continue` Statement\n",
    "\n",
    "The `continue` statement is used to skip the current iteration and\n",
    "proceed to the next one. Instead of exiting the loop entirely, it simply\n",
    "moves on to the next cycle.\n",
    "\n",
    "For example, if we are iterating over numbers and want to skip\n",
    "processing number 2, we can use `continue`. The loop will ignore that\n",
    "iteration and proceed with the next number.\n",
    "\n",
    "### The `pass` Statement\n",
    "\n",
    "The `pass` statement is a placeholder that does nothing. It is useful\n",
    "when a block of code is syntactically required but no action needs to be\n",
    "performed yet.\n",
    "\n",
    "For example, in a loop where a condition has not yet been implemented,\n",
    "using `pass` ensures that the code remains valid while avoiding errors."
   ],
   "id": "bd5766da-a032-4dda-a018-e6fb3314f09b"
  }
 ],
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {}
}