Skip to content

Commit 065cfe9

Browse files
committed
lesson 3 addition
1 parent cbd488d commit 065cfe9

File tree

1 file changed

+372
-0
lines changed

1 file changed

+372
-0
lines changed

Lesson-3.ipynb

+372
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,372 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "bdc4946b-43bb-4bf3-aff0-4473917363ae",
6+
"metadata": {},
7+
"source": [
8+
"Conditional Statements\n",
9+
"Conditional statements allow us to execute certain pieces of code based on certain conditions. The basic conditional statements in Python are if, elif, and else.\n",
10+
"\n",
11+
"If Statement\n",
12+
"The if statement checks a condition and executes the code block inside it if the condition is True."
13+
]
14+
},
15+
{
16+
"cell_type": "code",
17+
"execution_count": null,
18+
"id": "b38b4a9f-3fe2-4a49-b847-f672ef1dc007",
19+
"metadata": {},
20+
"outputs": [],
21+
"source": [
22+
"if condition:\n",
23+
" # code to execute if condition is True"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": null,
29+
"id": "6f223503-048d-4567-b0ef-a9280921ff65",
30+
"metadata": {},
31+
"outputs": [],
32+
"source": [
33+
"age = 18\n",
34+
"\n",
35+
"if age >= 18:\n",
36+
" print(\"You are an adult.\")"
37+
]
38+
},
39+
{
40+
"cell_type": "markdown",
41+
"id": "b4691206-dac4-479d-8246-970988b794e6",
42+
"metadata": {},
43+
"source": [
44+
"Working mechanism:\n",
45+
"Python checks if the condition (age >= 18) is True.\n",
46+
"If it is True, it runs the code inside the if block (print(\"You are an adult.\"))."
47+
]
48+
},
49+
{
50+
"cell_type": "markdown",
51+
"id": "acf0f396-4c73-4bf5-8897-31c88ba6338b",
52+
"metadata": {},
53+
"source": [
54+
"Elif Statement\n",
55+
"The elif (short for \"else if\") statement allows us to check multiple conditions."
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": null,
61+
"id": "11a02d7b-164e-4345-8c1d-df2103ef4127",
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"if condition1:\n",
66+
" # code to execute if condition1 is True\n",
67+
"elif condition2:\n",
68+
" # code to execute if condition2 is True"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": null,
74+
"id": "a75c5de6-eace-4c32-85a2-2b9139b84062",
75+
"metadata": {},
76+
"outputs": [],
77+
"source": [
78+
"age = 16\n",
79+
"\n",
80+
"if age >= 18:\n",
81+
" print(\"You are an adult.\")\n",
82+
"elif age >= 13:\n",
83+
" print(\"You are a teenager.\")\n"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"id": "11d6d922-27be-4b28-aa32-741ef5319290",
89+
"metadata": {},
90+
"source": [
91+
"How it works:\n",
92+
"\n",
93+
"Python checks the first condition (age >= 18).\n",
94+
"If it is False, it moves to the elif condition (age >= 13).\n",
95+
"If the elif condition is True, it runs the code inside the elif block."
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"id": "703f383c-186f-47a0-821e-8b1562732a61",
101+
"metadata": {},
102+
"source": [
103+
"Else Statement\n",
104+
"The else statement is used to execute a block of code if none of the previous conditions are True."
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": null,
110+
"id": "0cabcdce-064d-4f5e-9264-87c02861afbf",
111+
"metadata": {},
112+
"outputs": [],
113+
"source": [
114+
"if condition1:\n",
115+
" # code to execute if condition1 is True\n",
116+
"elif condition2:\n",
117+
" # code to execute if condition2 is True\n",
118+
"else:\n",
119+
" # code to execute if none of the above conditions are True"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": null,
125+
"id": "6cf01916-c907-4246-a64b-9dcdaa865ba0",
126+
"metadata": {},
127+
"outputs": [],
128+
"source": [
129+
"age = 10\n",
130+
"\n",
131+
"if age >= 18:\n",
132+
" print(\"You are an adult.\")\n",
133+
"elif age >= 13:\n",
134+
" print(\"You are a teenager.\")\n",
135+
"else:\n",
136+
" print(\"You are a child.\")"
137+
]
138+
},
139+
{
140+
"cell_type": "markdown",
141+
"id": "b5073df7-02be-4188-b059-fa4ea0febfa6",
142+
"metadata": {},
143+
"source": [
144+
"How it works:\n",
145+
"\n",
146+
"Python checks the if condition.\n",
147+
"If it is False, it checks the elif condition.\n",
148+
"If both are False, it executes the code inside the else block."
149+
]
150+
},
151+
{
152+
"cell_type": "markdown",
153+
"id": "2f1b384a-9f37-4633-93f5-1911dc108e84",
154+
"metadata": {},
155+
"source": [
156+
"Loops\n",
157+
"Loops allow us to execute a block of code multiple times. The main types of loops in Python are for loops and while loops.\n",
158+
"\n",
159+
"For Loop\n",
160+
"The for loop is used to iterate over a sequence (like a list, tuple, or string)."
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": null,
166+
"id": "0c0364f6-9657-4ee5-a05b-1d1da03ec914",
167+
"metadata": {},
168+
"outputs": [],
169+
"source": [
170+
"for item in sequence:\n",
171+
" # code to execute for each item in the sequence"
172+
]
173+
},
174+
{
175+
"cell_type": "code",
176+
"execution_count": null,
177+
"id": "d9ea428d-c587-4522-be66-9e9f75710f53",
178+
"metadata": {},
179+
"outputs": [],
180+
"source": [
181+
"numbers = [1, 2, 3, 4, 5]\n",
182+
"\n",
183+
"for number in numbers:\n",
184+
" print(number)"
185+
]
186+
},
187+
{
188+
"cell_type": "markdown",
189+
"id": "0eaebb7e-fc24-405e-bbc1-6b031e4af940",
190+
"metadata": {},
191+
"source": [
192+
"How it works:\n",
193+
"\n",
194+
"Python takes each item in the sequence (numbers list) one by one.\n",
195+
"It assigns the item to the variable (number).\n",
196+
"It runs the code inside the loop for each item."
197+
]
198+
},
199+
{
200+
"cell_type": "markdown",
201+
"id": "ee9a9d72-c6aa-4b1b-9912-70b9613b401f",
202+
"metadata": {},
203+
"source": [
204+
"While Loop\n",
205+
"The while loop is used to execute a block of code as long as a condition is True."
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": null,
211+
"id": "70f54597-562e-4d91-a417-411ef1a5e4a3",
212+
"metadata": {},
213+
"outputs": [],
214+
"source": [
215+
"while condition:\n",
216+
" # code to execute as long as condition is True"
217+
]
218+
},
219+
{
220+
"cell_type": "code",
221+
"execution_count": 8,
222+
"id": "fd68e335-07dd-4488-a81e-b76892bca674",
223+
"metadata": {},
224+
"outputs": [
225+
{
226+
"name": "stdout",
227+
"output_type": "stream",
228+
"text": [
229+
"0\n",
230+
"1\n",
231+
"2\n",
232+
"3\n",
233+
"4\n"
234+
]
235+
}
236+
],
237+
"source": [
238+
"count = 0\n",
239+
"\n",
240+
"while count < 5:\n",
241+
" print(count)\n",
242+
" count += 1"
243+
]
244+
},
245+
{
246+
"cell_type": "markdown",
247+
"id": "f097113d-212c-491a-abf7-6cfbc81f8c0c",
248+
"metadata": {},
249+
"source": [
250+
"How it works:\n",
251+
"\n",
252+
"Python checks if the condition (count < 5) is True.\n",
253+
"If it is True, it runs the code inside the while block.\n",
254+
"It repeats this process until the condition becomes False."
255+
]
256+
},
257+
{
258+
"cell_type": "markdown",
259+
"id": "4052458b-105f-46da-96f5-c3123e37bfe9",
260+
"metadata": {},
261+
"source": [
262+
"Break and Continue Statements\n",
263+
"These statements control the flow of loops.\n",
264+
"\n",
265+
"Break Statement\n",
266+
"The break statement is used to exit a loop before it has iterated over all items."
267+
]
268+
},
269+
{
270+
"cell_type": "code",
271+
"execution_count": null,
272+
"id": "c1875e75-0380-47b2-81d6-33e31b62d2c1",
273+
"metadata": {},
274+
"outputs": [],
275+
"source": [
276+
"for number in range(10):\n",
277+
" if number == 5:\n",
278+
" break\n",
279+
" print(number)"
280+
]
281+
},
282+
{
283+
"cell_type": "markdown",
284+
"id": "e4a6b768-6418-4006-bf4f-f2c0ac4d0757",
285+
"metadata": {},
286+
"source": [
287+
"How it works:\n",
288+
"\n",
289+
"Python iterates over the range (0 to 9).\n",
290+
"When number equals 5, the break statement stops the loop.\n",
291+
"It exits the loop, and the program continues with the next section of code."
292+
]
293+
},
294+
{
295+
"cell_type": "markdown",
296+
"id": "ca79b09f-44ff-4d8a-a7c9-5ddb2d8c2eb3",
297+
"metadata": {},
298+
"source": [
299+
"Continue Statement\n",
300+
"The continue statement is used to skip the current iteration and move to the next iteration of the loop."
301+
]
302+
},
303+
{
304+
"cell_type": "code",
305+
"execution_count": null,
306+
"id": "c2af13c9-f2f5-4bb8-be3d-ed0de39d8eda",
307+
"metadata": {},
308+
"outputs": [],
309+
"source": [
310+
"for number in range(10):\n",
311+
" if number % 2 == 0:\n",
312+
" continue\n",
313+
" print(number)"
314+
]
315+
},
316+
{
317+
"cell_type": "markdown",
318+
"id": "35a18d94-d299-4ebe-a12b-8d5d895e3532",
319+
"metadata": {},
320+
"source": [
321+
"How it works:\n",
322+
"\n",
323+
"Python iterates over the range (0 to 9).\n",
324+
"When number is even, the continue statement skips the current iteration.\n",
325+
"It moves to the next iteration, printing only odd numbers."
326+
]
327+
},
328+
{
329+
"cell_type": "markdown",
330+
"id": "12af8d1b-340d-4903-9920-ae614a67ef9f",
331+
"metadata": {},
332+
"source": [
333+
"Summary: \n",
334+
"\n",
335+
"Conditional statements (if, elif, else) let you run code based on conditions.\n",
336+
"Loops (for, while) let you run code multiple times.\n",
337+
"Break and continue statements control the flow of loops, allowing you to exit or skip iterations."
338+
]
339+
},
340+
{
341+
"cell_type": "markdown",
342+
"id": "583246f8-ec99-4315-9445-f13162246a8a",
343+
"metadata": {},
344+
"source": [
345+
"- Python has various data types like integers, floats, strings, booleans, lists, tuples, and dictionaries.\n",
346+
"- Type conversion allows you to convert a value from one data type to another.\n",
347+
"- Use built-in functions like int(), float(), str(), list(), tuple(), and dict() for conversions."
348+
]
349+
}
350+
],
351+
"metadata": {
352+
"kernelspec": {
353+
"display_name": "Python 3 (ipykernel)",
354+
"language": "python",
355+
"name": "python3"
356+
},
357+
"language_info": {
358+
"codemirror_mode": {
359+
"name": "ipython",
360+
"version": 3
361+
},
362+
"file_extension": ".py",
363+
"mimetype": "text/x-python",
364+
"name": "python",
365+
"nbconvert_exporter": "python",
366+
"pygments_lexer": "ipython3",
367+
"version": "3.12.4"
368+
}
369+
},
370+
"nbformat": 4,
371+
"nbformat_minor": 5
372+
}

0 commit comments

Comments
 (0)