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
199
200
201
202
|
\section{Control Structures}\label{control-structures}
Control structures allow us to control the flow of execution in a Python
program. The two main types are \textbf{conditional statements
(\texttt{if} statements)} and \textbf{loops (\texttt{for} and
\texttt{while} loops)}.
\subsection{Conditional Statements}\label{conditional-statements}
Conditional statements allow a program to execute different blocks of
code depending on whether a given condition is \texttt{True} or
\texttt{False}. These conditions are typically comparisons, such as
checking if one number is greater than another.
\subsubsection{\texorpdfstring{The \texttt{if}
Statement}{The if Statement}}\label{the-if-statement}
The simplest form of a conditional statement is the \texttt{if}
statement. If the condition evaluates to \texttt{True}, the indented
block of code runs. Otherwise, the program moves on without executing
the statement.
For example, consider a situation where we need to determine if a person
is an adult based on their age. If the age is 18 or greater, we print a
message saying they are an adult.
\subsubsection{\texorpdfstring{The \texttt{if-else}
Statement}{The if-else Statement}}\label{the-if-else-statement}
Sometimes, we need to specify what should happen if the condition is
\texttt{False}. The \texttt{else} clause allows us to handle this case.
Instead of just skipping over the block, the program can execute an
alternative action.
For instance, if a person is younger than 18, they are considered a
minor. If the condition of being an adult is not met, the program will
print a message indicating that the person is a minor.
\subsubsection{\texorpdfstring{The \texttt{if-elif-else}
Statement}{The if-elif-else Statement}}\label{the-if-elif-else-statement}
When dealing with multiple conditions, the \texttt{if-elif-else}
structure is useful. The program evaluates conditions in order,
executing the first one that is \texttt{True}. If none of the conditions
are met, the \texttt{else} block runs.
For example, in a grading system, different score ranges correspond to
different letter grades. If a student's score is 90 or higher, they
receive an ``A''. If it's between 80 and 89, they get a ``B'', and so
on. If none of the conditions match, they receive an ``F''.
\subsubsection{\texorpdfstring{Nested \texttt{if}
Statements}{Nested if Statements}}\label{nested-if-statements}
Sometimes, we need to check conditions within other conditions. This is
known as \textbf{nesting}. For example, if we first determine that a
person is an adult, we can then check if they are a student. Based on
that information, we print different messages.
\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{\# Getting user input for the student\textquotesingle{}s score}
\NormalTok{score }\OperatorTok{=} \BuiltInTok{int}\NormalTok{(}\BuiltInTok{input}\NormalTok{(}\StringTok{"Enter the student\textquotesingle{}s score (0{-}100): "}\NormalTok{))}
\ControlFlowTok{if} \DecValTok{0} \OperatorTok{\textless{}=}\NormalTok{ score }\OperatorTok{\textless{}=} \DecValTok{100}\NormalTok{:}
\ControlFlowTok{if}\NormalTok{ score }\OperatorTok{\textgreater{}=} \DecValTok{90}\NormalTok{:}
\NormalTok{ grade }\OperatorTok{=} \StringTok{"A"}
\ControlFlowTok{elif}\NormalTok{ score }\OperatorTok{\textgreater{}=} \DecValTok{80}\NormalTok{:}
\NormalTok{ grade }\OperatorTok{=} \StringTok{"B"}
\ControlFlowTok{elif}\NormalTok{ score }\OperatorTok{\textgreater{}=} \DecValTok{70}\NormalTok{:}
\NormalTok{ grade }\OperatorTok{=} \StringTok{"C"}
\ControlFlowTok{elif}\NormalTok{ score }\OperatorTok{\textgreater{}=} \DecValTok{60}\NormalTok{:}
\NormalTok{ grade }\OperatorTok{=} \StringTok{"D"}
\ControlFlowTok{else}\NormalTok{:}
\NormalTok{ grade }\OperatorTok{=} \StringTok{"F"} \CommentTok{\# Score below 60 is a failing grade}
\ControlFlowTok{if}\NormalTok{ grade }\OperatorTok{==} \StringTok{"F"}\NormalTok{:}
\BuiltInTok{print}\NormalTok{(}\StringTok{"The student has failed."}\NormalTok{)}
\NormalTok{ retake\_eligible }\OperatorTok{=} \BuiltInTok{input}\NormalTok{(}\StringTok{"Is the student eligible for a retest? (yes/no): "}\NormalTok{).strip().lower()}
\ControlFlowTok{if}\NormalTok{ retake\_eligible }\OperatorTok{==} \StringTok{"yes"}\NormalTok{:}
\BuiltInTok{print}\NormalTok{(}\StringTok{"The student is eligible for a retest."}\NormalTok{)}
\ControlFlowTok{else}\NormalTok{:}
\BuiltInTok{print}\NormalTok{(}\StringTok{"The student has failed the course and must retake it next semester."}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\subsection{Loops in Python}\label{loops-in-python}
Loops allow a program to execute a block of code multiple times. This is
especially useful for tasks such as processing lists of data, performing
repetitive calculations, or automating tasks.
\subsubsection{\texorpdfstring{The \texttt{for}
Loop}{The for Loop}}\label{the-for-loop}
A \texttt{for} loop iterates over a sequence, such as a list, tuple,
string, or a range of numbers. Each iteration assigns the next value in
the sequence to a loop variable, which can then be used inside the loop.
For instance, if we have a list of fruits and want to print each fruit's
name, a \texttt{for} loop can iterate over the list and display each
item.
Another useful feature of \texttt{for} loops is the \texttt{range()}
function, which generates a sequence of numbers. This is commonly used
when we need to repeat an action a specific number of times. For
example, iterating from 0 to 4 allows us to print a message five times.
Additionally, the \texttt{enumerate()} function can be used to loop
through a list while keeping track of the index of each item. This is
useful when both the position and the value in a sequence are needed.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{fruits }\OperatorTok{=}\NormalTok{ [}\StringTok{"apple"}\NormalTok{, }\StringTok{"banana"}\NormalTok{, }\StringTok{"cherry"}\NormalTok{] }
\ControlFlowTok{for}\NormalTok{ x }\KeywordTok{in}\NormalTok{ fruits: }
\BuiltInTok{print}\NormalTok{(x)}
\end{Highlighting}
\end{Shaded}
\begin{Shaded}
\begin{Highlighting}[]
\ControlFlowTok{for}\NormalTok{ x }\KeywordTok{in} \BuiltInTok{range}\NormalTok{(}\DecValTok{6}\NormalTok{): }
\BuiltInTok{print}\NormalTok{(x) }
\ControlFlowTok{else}\NormalTok{: }
\BuiltInTok{print}\NormalTok{(}\StringTok{"Finally finished!"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\subsubsection{\texorpdfstring{The \texttt{while}
Loop}{The while Loop}}\label{the-while-loop}
Unlike \texttt{for} loops, which iterate over a sequence, \texttt{while}
loops continue running as long as a specified condition remains
\texttt{True}. This is useful when the number of iterations is not known
in advance.
For example, a countdown timer can be implemented using a \texttt{while}
loop. The loop will continue decreasing the count until it reaches zero.
It's important to be careful with \texttt{while} loops to avoid infinite
loops, which occur when the condition never becomes \texttt{False}. To
prevent this, ensure that the condition will eventually change during
the execution of the loop.
A \texttt{while} loop can also be used to wait for a certain event to
occur. For example, in interactive programs, a \texttt{while\ True} loop
can keep running until the user provides a valid input, at which point
we break out of the loop.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{i }\OperatorTok{=} \DecValTok{1}
\ControlFlowTok{while}\NormalTok{ i }\OperatorTok{\textless{}} \DecValTok{6}\NormalTok{: }
\NormalTok{ print(i) }
\NormalTok{ i }\OperatorTok{+=} \DecValTok{1}
\end{Highlighting}
\end{Shaded}
\subsection{Loop Control Statements}\label{loop-control-statements}
Python provides special statements to control the behavior of loops.
These can be used to break out of a loop, skip certain iterations, or
simply include a placeholder for future code.
\subsubsection{\texorpdfstring{The \texttt{break}
Statement}{The break Statement}}\label{the-break-statement}
The \texttt{break} statement is used to exit a loop before it has
iterated through all its elements. When the \texttt{break} statement is
encountered, the loop stops immediately, and the program continues
executing the next statement outside the loop.
For instance, if we are searching for a specific value in a list, we can
use a \texttt{break} statement to stop the loop as soon as we find the
item, instead of continuing unnecessary iterations.
\subsubsection{\texorpdfstring{The \texttt{continue}
Statement}{The continue Statement}}\label{the-continue-statement}
The \texttt{continue} statement is used to skip the current iteration
and proceed to the next one. Instead of exiting the loop entirely, it
simply moves on to the next cycle.
For example, if we are iterating over numbers and want to skip
processing number 2, we can use \texttt{continue}. The loop will ignore
that iteration and proceed with the next number.
\subsubsection{\texorpdfstring{The \texttt{pass}
Statement}{The pass Statement}}\label{the-pass-statement}
The \texttt{pass} statement is a placeholder that does nothing. It is
useful when a block of code is syntactically required but no action
needs to be performed yet.
For example, in a loop where a condition has not yet been implemented,
using \texttt{pass} ensures that the code remains valid while avoiding
errors.
|