-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodelab-python.html
More file actions
523 lines (424 loc) · 23.6 KB
/
codelab-python.html
File metadata and controls
523 lines (424 loc) · 23.6 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ai CodeLab - Core Documentation</title>
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<!-- Modern Styles -->
<link rel="stylesheet" href="styles.css">
<style>
.study-grid {
display: grid;
grid-template-columns: 3fr 1fr;
gap: 3rem;
max-width: 1400px;
margin: 0 auto;
}
.study-content {
background: linear-gradient(135deg, rgba(255,255,255,0.02), rgba(255,255,255,0.01));
border: 1px solid var(--glass-border);
border-radius: 20px;
padding: 3rem;
backdrop-filter: blur(20px);
color: var(--text-pure);
}
.study-content h1, .study-content h2, .study-content h3 {
color: var(--secondary);
margin-top: 2.5rem;
margin-bottom: 1rem;
border-bottom: 1px solid var(--glass-border);
padding-bottom: 0.5rem;
}
.study-content h1 {
color: var(--primary);
font-size: 3rem;
}
.study-content p, .study-content li {
margin-bottom: 1.2rem;
color: var(--text-muted);
line-height: 1.8;
font-size: 1.05rem;
}
.study-content ul, .study-content ol {
margin-left: 2rem;
margin-bottom: 2rem;
}
.study-content pre {
background: rgba(0,0,0,0.6);
border: 1px solid var(--glass-border);
padding: 1.5rem;
border-radius: 10px;
overflow-x: auto;
margin-bottom: 2rem;
color: var(--accent);
font-family: monospace;
box-shadow: inset 0 0 20px rgba(0,0,0,0.5);
}
.study-sidebar {
background: linear-gradient(135deg, rgba(255,255,255,0.02), rgba(255,255,255,0.01));
border: 1px solid var(--glass-border);
border-radius: 20px;
padding: 2rem;
backdrop-filter: blur(20px);
position: sticky;
top: 100px;
height: fit-content;
}
.study-sidebar h2 {
color: var(--primary);
margin-bottom: 1.5rem;
border-bottom: 1px solid var(--glass-border);
padding-bottom: 0.5rem;
font-size: 1.2rem;
}
.study-sidebar a {
display: flex;
align-items: center;
gap: 1rem;
padding: 0.5rem 0;
color: var(--text-muted);
transition: all 0.3s;
}
.study-sidebar a:hover {
color: var(--secondary);
transform: translateX(5px);
}
@media (max-width: 900px) {
.study-grid { grid-template-columns: 1fr; }
.study-sidebar { position: relative; top: 0; }
}
</style>
</head>
<body>
<!-- Custom Cursor -->
<div id="custom-cursor"></div>
<!-- Animated Mesh Gradient Background -->
<div class="mesh-bg">
<div class="blob blob-1"></div>
<div class="blob blob-2"></div>
<div class="blob blob-3"></div>
</div>
<!-- Modern Extreme Navbar -->
<nav>
<div class="logo-container">
<i class="fas fa-robot"></i>
<span>CodeLab<span style="color:var(--primary)">.ai</span></span>
</div>
<div class="nav-links">
<a href="index.html">Platform</a>
<a href="courses.html">Syllabus</a>
<a href="study.html" class="active">Resources</a>
<a href="join.html">Community</a>
</div>
<div style="display:flex;gap:10px;">
<a href="login.html" class="magnetic-btn" style="padding: 0.5rem 1.5rem; font-size: 0.9rem;"><span>Login</span></a>
</div>
</nav>
<!-- Page Title Header -->
<header style="margin-top: 120px; text-align: center; padding: 2rem;">
<h1 class="hover-target" style="font-size: clamp(2.5rem, 5vw, 4rem);"><span class="gradient-text">Protocol</span> Documentation</h1>
</header>
<main style="padding: 2rem;">
<div class="study-grid">
<div class="study-content reveal hover-target" data-tilt>
<h1 id="introduction">Introduction to Python</h1>
<p>Python is a high-level, interpreted programming language known for its simplicity and readability. Created by <strong>Guido van Rossum</strong> in 1991, Python is widely used in web development, data science, artificial intelligence, automation, and more.</p>
<h2 id="why-learn-python">Why Learn Python?</h2>
<ul>
<li><strong>Easy to Learn</strong> – Simple syntax similar to English.</li>
<li><strong>Versatile</strong> – Can be used for web apps, data science, AI, etc.</li>
<li><strong>Huge Community Support</strong> – Many resources and libraries.</li>
<li><strong>Career Opportunities</strong> – High demand in various industries.</li>
<li><strong>Cross-Platform</strong> – Runs on Windows, macOS, and Linux.</li>
</ul>
<h2 id="installing-python">Installing Python</h2>
<ol>
<li>Download Python from <a href="https://www.python.org/downloads/">python.org</a>.</li>
<li>Install it and check by running:</li>
<pre><code>python --version</code></pre>
<li>Use <strong>IDLE</strong> or <strong>VS Code/PyCharm</strong> for writing Python programs.</li>
</ol>
<h2 id="python-basics">Python Basics</h2>
<h3 id="hello-world">Hello World Program</h3>
<pre><code>print("Hello, World!")</code></pre>
<!-- <strong id="variables-data-types">Variables and Data Types</strong > -->
<p><strong id="variables-data-types">Variables and Data Types in Python</strong></p>
<p>In Python, a <strong>variable</strong> is like a container that stores data. You can assign a value to a variable using the <code>=</code> sign. For example, <code>name = "Alice"</code> stores the text "Alice" in the variable <code>name</code>. Python has different <strong>data types</strong> to handle various kinds of data:</p>
<p><strong>Integer:</strong> Whole numbers e.g., <code>x = 10</code> </p>
<p><strong>Float:</strong> Decimal numbers e.g., <code>price = 10.5</code></p>
<p><strong>String:</strong> Text e.g., <code>name = "Alice"</code> </p>
<p><strong>Boolean:</strong> True or False values e.g, <code>is_raining = False</code></p>
<p>Python is <strong>dynamically typed</strong>, meaning you don’t need to specify the data type—it’s assigned automatically. You can check a variable’s type using the <code>type()</code> function, like <code>print(type(name))</code></p>
<pre><code>name = "Shifa"
age = 20
is_student = True</code></pre>
<h3 id="input-output">Input and Output</h3>
<pre><code>name = input("Enter your name: ")
print("Hello,", name)</code></pre>
<h2 id="control-statements">Control Statements in Python</h2>
<p>Control statements help us <strong>control the flow of a program</strong> based on conditions and loops. There are two main types:</p>
<ul>
<li><strong>If-Else Statements</strong> – Used to make decisions</li>
<li><strong>Loops</strong> – Used to repeat a block of code</li>
</ul>
<h3 id="if-else">1️⃣ If-Else Statement</h3>
<p>An <strong>if-else statement</strong> checks a condition and decides what to do based on whether the condition is <strong>True or False</strong>.</p>
<pre><code>age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")</code></pre>
<p><strong>How it works?</strong></p>
<ul>
<li>The program asks the user to enter their age.</li>
<li>The <code>if</code> condition checks if the age is <strong>18 or more</strong>.</li>
<li>If <strong>True</strong>, it prints <em>"You are an adult."</em></li>
<li>If <strong>False</strong>, it prints <em>"You are a minor."</em></li>
</ul>
<p>💡 <strong>Use case:</strong> If-Else is useful when making <strong>decisions</strong>, like checking if a user is old enough to vote.</p>
<hr>
<h3 id="loops">2️⃣ Loops in Python</h3>
<p>Loops are used to <strong>repeat</strong> a block of code multiple times. Python has two main loops:</p>
<h4 id="for-loop">🔹 For Loop</h4>
<p>A <strong>for loop</strong> is used when we know <strong>how many times</strong> we want to repeat something.</p>
<pre><code>for i in range(5):
print(i)</code></pre>
<p><strong>How it works?</strong></p>
<ul>
<li><code>range(5)</code> generates numbers <strong>0 to 4</strong> (not including 5).</li>
<li>The loop runs 5 times, printing numbers <strong>0 to 4</strong> one by one.</li>
</ul>
<p>💡 <strong>Use case:</strong> Use a <strong>for loop</strong> when you want to repeat a task a set number of times, like printing numbers or looping through a list.</p>
<hr>
<h4 id="while-loop">🔹 While Loop</h4>
<p>A <strong>while loop</strong> runs as long as a condition is <strong>True</strong>.</p>
<pre><code>x = 0
while x < 5:
print(x)
x += 1</code></pre>
<p><strong>How it works?</strong></p>
<ul>
<li>The loop starts with <code>x = 0</code></li>
<li>The condition <code>x < 5</code> is checked before each loop run.</li>
<li>Inside the loop, <code>x</code> is printed, and then <strong>increased by 1</strong> <code>x += 1</code></li>
<li>The loop stops when <code>x</code> becomes <strong>5</strong></li>
</ul>
<p>💡 <strong>Use case:</strong> Use a <strong>while loop</strong> when you <strong>don't know how many times</strong> the loop should run, like waiting for user input.</p>
<hr>
<h2>Summary</h2>
<ul>
<li>✅ <strong>If-Else</strong> → Makes decisions e.g., checking age.</li>
<li>✅ <strong>For Loop</strong> → Repeats a block a fixed number of times.</li>
<li>✅ <strong>While Loop</strong> → Repeats until a condition is false.</li>
</ul>
<!-- Functions -->
<h2 id="functions">Functions in Pyton</h2>
<p>A <strong>function</strong> in Python is a reusable block of code that performs a specific task. Instead of writing the same code again and again, we can use functions to make our programs shorter and easier to understand.</p>
<h3>📌 Example Code:</h3>
<pre><code>def greet(name):
return "Hello, " + name
print(greet("Shifa"))</code></pre>
<h3>🔹 How This Code Works?</h3>
<ul>
<li>The keyword <code>def</code> is used to <strong>define a function</strong></li>
<li>The function <code>greet(name)</code> takes one <strong>input (name)</strong></li>
<li>Inside the function, it returns <strong>"Hello, " + name</strong> which means it adds the name to "Hello, "</li>
<li>We call the function using <code>print(greet("Shifa"))</code></li>
<li>This prints <strong>"Hello, Shifa"</strong> on the screen.</li>
</ul>
<h3>✅ Why Use Functions?</h3>
<ul>
<li>⭐ <strong>Reusability:</strong> We can call the function many times instead of writing the same code again.</li>
<li>⭐ <strong>Better Readability:</strong> Code looks cleaner and is easier to understand.</li>
<li>⭐ <strong>Easier to Fix Errors:</strong> If something goes wrong, we only need to change the function instead of the entire program.</li>
</ul>
<h3>🚀 Example: Calling the Function Multiple Times</h3>
<pre><code>print(greet("Alice"))
print(greet("Bob"))
print(greet("Charlie"))</code></pre>
<p><strong>Output:</strong></p>
<pre><code>Hello, Alice
Hello, Bob
Hello, Charlie</code></pre>
<p>This shows how functions help us avoid repeating code! 🎯</p>
<!-- list tuples and dictanries -->
<h2 id="lists-tuples">Lists and Tuples in Python</h2>
<p>Python provides different ways to store multiple values in a single variable. Two common types are:</p>
<ul>
<li><strong>Lists</strong> - A collection of items that can be changed (mutable).</li>
<li><strong>Tuples</strong> - A collection of items that cannot be changed (immutable).</li>
</ul>
<h3 id="list">🔹 Lists</h3>
<p>A <strong>list</strong> is like a container that holds multiple values. You can add, remove, or change items in a list.</p>
<pre><code>fruits = ["Apple", "Banana", "Cherry"]
fruits.append("Mango")
print(fruits)</code></pre>
<p><strong>How it works?</strong></p>
<ul>
<li><code>fruits</code> is a list containing <em>Apple, Banana, Cherry</em>.</li>
<li><code>append("Mango")</code> adds <em>Mango</em> to the list.</li>
<li>After running this code, the list becomes <code>["Apple", "Banana", "Cherry", "Mango"]</code>.</li>
</ul>
<h3 id="tuple">🔹 Tuples</h3>
<p>A <strong>tuple</strong> is similar to a list, but you <strong>cannot change</strong> its values after creating it.</p>
<pre><code>days = ("Monday", "Tuesday", "Wednesday")
print(days[0])</code></pre>
<p><strong>How it works?</strong></p>
<ul>
<li><code>days</code> is a tuple containing <em>Monday, Tuesday, and Wednesday</em>.</li>
<li><code>print(days[0])</code> prints the first value, which is <strong>Monday</strong>.</li>
<li>Unlike lists, you cannot add or remove items from a tuple.</li>
</ul>
<hr>
<h2 id="dictionaries">Dictionaries in Python</h2>
<p>A <strong>dictionary</strong> stores data in <strong>key-value pairs</strong>. It is useful for organizing related information.</p>
<pre><code>person = {"name": "Shifa", "age": 20}
print(person["name"]) # Output: Shifa</code></pre>
<p><strong>How it works?</strong></p>
<ul>
<li><code>person</code> is a dictionary with two keys: <em>name</em> and <em>age</em>.</li>
<li><code>person["name"]</code> returns <strong>Shifa</strong>, which is stored under the <em>name</em> key.</li>
<li>Dictionaries allow storing different types of information in an organized way.</li>
</ul>
<h3>✅ Why Use These Data Types?</h3>
<ul>
<li><strong>Lists</strong> → When you need a collection of items that can change.</li>
<li><strong>Tuples</strong> → When you need a collection that should stay the same.</li>
<li><strong>Dictionaries</strong> → When you need to store related data using key-value pairs.</li>
</ul>
<p>These are some of the most useful data types in Python for organizing data efficiently!</p>
<!-- oops -->
<h2 id="oop">Object-Oriented Programming (OOP) in Python</h2>
<p>Object-Oriented Programming (OOP) is a way to organize code by creating objects. An <strong>object</strong> is like a real-world item, such as a car or a person. These objects have <strong>attributes</strong> (characteristics) and <strong>methods</strong> (actions).</p>
<h3>🔹 What are Classes and Objects?</h3>
<ul>
<li><strong>Class</strong> → A template used to create objects.</li>
<li><strong>Object</strong> → A specific example created from a class.</li>
</ul>
<h3>📌 Example Code:</h3>
<pre><code>class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def greet(self):
print("Hello, my name is " + self.name)
p = Person("Shifa", 20)
p.greet()</code></pre>
<h3>🔹 Explanation:</h3>
<ul>
<li><code>class Person</code> creates a blueprint for a person.</li>
<li><code>__init__</code> is a special function that runs when we create a new person.</li>
<li><code>self.name</code> and <code>self.age</code> store the person’s name and age.</li>
<li>The <code>greet()</code> function prints a message with the person’s name.</li>
<li><code>p = Person("Shifa", 20)</code> creates a person named Shifa, who is 20 years old.</li>
<li><code>p.greet()</code> calls the function and prints <strong>"Hello, my name is Shifa"</strong>.</li>
</ul>
<h3>✅ Why Use OOP?</h3>
<ul>
<li><strong>Easy to Understand</strong> → Code is organized into objects, like real-world things.</li>
<li><strong>Reusable</strong> → You can create multiple objects from the same class.</li>
<li><strong>Less Repetition</strong> → Functions inside classes make code shorter.</li>
</ul>
<h3>🚀 Creating Multiple Objects</h3>
<pre><code>p1 = Person("Alice", 25)
p2 = Person("Bob", 30)
p1.greet() # Output: Hello, my name is Alice
p2.greet() # Output: Hello, my name is Bob</code></pre>
<p>OOP is widely used in programming, from making apps to building websites. Learning OOP makes Python easier to use!</p>
<!-- handling -->
<h2 id="conclusion">Conclusion</h2>
<p>Python is an excellent language for beginners due to its simplicity and flexibility. By mastering the basics, you can move on to advanced topics like web development, AI, and automation.</p>
<p><strong>Start coding today!</strong></p> -->
<h2 id="file-handling">File Handling in Python</h2>
<p>File handling allows us to <strong>read</strong> and <strong>write</strong> data to files. This is useful for saving and retrieving information.</p>
<h3 id="reading-file">🔹 Reading a File</h3>
<p>The following code reads the content of a file and prints it:</p>
<pre><code>with open("file.txt", "r") as file:
print(file.read())</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>open("file.txt", "r")</code> opens the file in <strong>read mode</strong>.</li>
<li><code>file.read()</code> reads the file’s content.</li>
<li>The <code>with</code> statement ensures the file is closed automatically.</li>
</ul>
<h3 id="writing-file">🔹 Writing to a File</h3>
<p>The following code writes text into a file:</p>
<pre><code>with open("file.txt", "w") as file:
file.write("Hello, Python!")</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li><code>open("file.txt", "w")</code> opens the file in <strong>write mode</strong>.</li>
<li><code>file.write("Hello, Python!")</code> writes text into the file.</li>
<li>If the file doesn’t exist, Python will create it.</li>
</ul>
<hr>
<h2 id="exception-handling">Exception Handling in Python</h2>
<p>Exceptions are errors that occur during program execution. Python allows us to <strong>handle</strong> these errors to prevent crashes.</p>
<h3>📌 Example Code:</h3>
<pre><code>try:
x = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")</code></pre>
<p><strong>Explanation:</strong></p>
<ul>
<li>The <code>try</code> block contains code that might cause an error.</li>
<li>The <code>except</code> block catches the error and prevents the program from stopping.</li>
<li>Here, dividing by zero is not allowed, so the program prints <strong>"Cannot divide by zero!"</strong>.</li>
</ul>
<hr>
<h2 id="libraries-frameworks">Python Libraries and Frameworks</h2>
<p>Python has many <strong>libraries</strong> and <strong>frameworks</strong> that make programming easier:</p>
<ul>
<li><strong>NumPy, Pandas</strong> – Used for data analysis.</li>
<li><strong>Django, Flask</strong> – Used for web development.</li>
<li><strong>TensorFlow, PyTorch</strong> – Used for machine learning.</li>
<li><strong>Matplotlib, Seaborn</strong> – Used for data visualization.</li>
</ul>
<hr>
<h2 id="conclusion">Conclusion</h2>
<p>Python is a beginner-friendly language because of its <strong>simplicity</strong> and <strong>flexibility</strong>. Once you master the basics, you can explore areas like web development, AI, and automation.</p>
<p><strong>Start coding today and build amazing projects! 🚀</strong></p>\n </div>\n <aside class="study-sidebar reveal">\n<h2><i class="fas fa-list-alt"></i> Article Topics</h2>
<a href="#introduction"><i class="fas fa-book"></i> Introduction to Python</a>
<a href="#why-learn-python"><i class="fas fa-book"></i> Why Learn Python?</a>
<a href="#installing-python"><i class="fas fa-book"></i> Installing Python</a>
<a href="#python-basics"><i class="fas fa-book"></i> Python Basics</a>
<a href="#hello-world"><i class="fas fa-book"></i> Hello World Program</a>
<a href="#variables-data-types"><i class="fas fa-book"></i> Variables and Data Types</a>
<a href="#input-output"><i class="fas fa-book"></i> Input and Output</a>
<a href="#control-statements"><i class="fas fa-book"></i> Control Statements</a>
<a href="#if-else"><i class="fas fa-book"></i> If-Else</a>
<a href="#loops"><i class="fas fa-book"></i> Loops</a>
<a href="#for-loop"><i class="fas fa-book"></i> For Loop</a>
<a href="#while-loop"><i class="fas fa-book"></i> While Loop</a>
<a href="#functions"><i class="fas fa-book"></i> Functions</a>
<a href="#lists-tuples"><i class="fas fa-book"></i> Lists and Tuples</a>
<a href="#list"><i class="fas fa-book"></i> List</a>
<a href="#tuple"><i class="fas fa-book"></i> Tuple</a>
<a href="#dictionaries"><i class="fas fa-book"></i> Dictionaries</a>
<a href="#oop"><i class="fas fa-book"></i> Object-Oriented Programming (OOP)</a>
<a href="#file-handling"><i class="fas fa-book"></i> File Handling</a>
<a href="#reading-file"><i class="fas fa-book"></i> Reading a File</a>
<a href="#writing-file"><i class="fas fa-book"></i> Writing to a File</a>
<a href="#exception-handling"><i class="fas fa-book"></i> Exception Handling</a>
<a href="#libraries-frameworks"><i class="fas fa-book"></i> Python Libraries and Frameworks</a>
<a href="#conclusion"><i class="fas fa-book"></i> Conclusion</a>\n </aside>
</div>
</div>
</main>
<!-- Footer -->
<footer>
<div class="footer-grid reveal">
<div>
<h2 class="footer-logo">CodeLab<span style="color:var(--primary)">.ai</span></h2>
<p class="footer-p">The frontier of learning.</p>
</div>
</div>
<div style="text-align: center; border-top: 1px solid var(--glass-border); padding-top: 2rem; margin-top:2rem; color:var(--text-muted); font-size: 0.9rem;">
© 2026 CodeLab.ai Operations. All Rights Reserved.
</div>
</footer>
<script src="script.js"></script>
</body>
</html>