Repository navigation
Expand file tree
/
Copy pathpython.html
More file actions
492 lines (436 loc) · 19.9 KB
/
Copy pathpython.html
File metadata and controls
492 lines (436 loc) · 19.9 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Basics · Python to AI</title>
<meta name="description" content="Learn the fundamental concepts of Python programming: Data Types, Structures, and Functions.">
<meta property="og:title" content="Python Basics · Python to AI">
<meta property="og:description" content="Learn the fundamental concepts of Python programming: Data Types, Structures, and Functions.">
<meta property="og:type" content="website">
<meta property="og:url" content="https://ikathuria.github.io/python-to-ai/app/pages/python.html">
<!-- Tailwind CSS -->
<!-- FontAwesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<!-- Google Fonts -->
<!-- Highlight.js for Syntax Highlighting -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<!-- Add language support -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/python.min.js"></script>
<script>hljs.highlightAll();</script>
<link rel="canonical" href="https://ikathuria.github.io/python-to-ai/app/pages/python.html">
<meta property="og:image" content="https://ikathuria.github.io/python-to-ai/app/images/og/python.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://ikathuria.github.io/python-to-ai/app/images/og/python.png">
<link rel="apple-touch-icon" href="../images/og/apple-touch-icon.png">
<link rel="icon" href="../../favicon.svg" type="image/svg+xml">
<link rel="stylesheet" href="../../app/styles/brand.css">
<link rel="stylesheet" href="../../app/styles/site.css">
<script src="../../app/styles/site.js"></script>
<link rel="stylesheet" href="../../app/styles/lessons.css">
<style>
body {
}
.nav-link {
cursor: pointer;
border-radius: 0.375rem;
padding-left: 0.75rem;
padding-right: 0.75rem;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 500;
color: var(--muted);
transition-property: color, background-color, border-color, text-decoration-color, fill, stroke;
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
transition-duration: 150ms;
}
.nav-link:hover {
background-color: var(--bg);
color: var(--link);
}
.nav-link.active {
background-color: var(--tint);
font-weight: 700;
color: var(--link-strong);
}
/* Sidebar Styling */
.sidebar-link {
display: block;
border-radius: 0.375rem;
padding-left: 1rem;
padding-right: 1rem;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
font-size: 0.875rem;
line-height: 1.25rem;
color: var(--muted);
transition-property: color, background-color, border-color, text-decoration-color, fill, stroke;
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
transition-duration: 150ms;
}
.sidebar-link:hover {
background-color: var(--case);
color: var(--link);
}
.sidebar-link.active {
border-right-width: 4px;
border-color: var(--link);
background-color: var(--tint);
font-weight: 700;
color: var(--link-strong);
}
.content-section {
margin-bottom: 3rem;
scroll-margin-top: 6rem;
border-bottom-width: 1px;
border-color: var(--case);
padding-bottom: 2rem;
}
h2 {
margin-bottom: 1rem;
display: flex;
align-items: center;
font-size: 1.5rem;
line-height: 2rem;
font-weight: 700;
color: var(--text);
}
h3 {
margin-top: 1.5rem;
margin-bottom: 0.75rem;
font-size: 1.25rem;
line-height: 1.75rem;
font-weight: 600;
color: var(--muted);
}
p {
margin-bottom: 1rem;
line-height: 1.625;
color: var(--muted);
}
ul {
margin-bottom: 1rem;
margin-left: 1rem;
list-style-position: inside;
list-style-type: disc;
}
ul > :not([hidden]) ~ :not([hidden]) {
--tw-space-y-reverse: 0;
margin-top: calc(0.5rem * calc(1 - var(--tw-space-y-reverse)));
margin-bottom: calc(0.5rem * var(--tw-space-y-reverse));
}
ul {
color: var(--muted);
}
code:not(.hljs) {
border-radius: 0.25rem;
background-color: var(--case);
padding-left: 0.25rem;
padding-right: 0.25rem;
padding-top: 0.125rem;
padding-bottom: 0.125rem;
font-family: Space Mono, ui-monospace, monospace;
font-size: 0.875rem;
line-height: 1.25rem;
color: var(--bad-solid);
}
pre {
margin-top: 1rem;
margin-bottom: 1rem;
overflow: hidden;
border-radius: var(--radius-sm);
border-width: 1px;
border-color: var(--line);
--tw-shadow: 0 1px 2px 0 rgb(0 0 0 / 0.05);
--tw-shadow-colored: 0 1px 2px 0 var(--tw-shadow-color);
box-shadow: var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);
}
details.try-it > summary {
list-style: none;
}
details.try-it > summary::-webkit-details-marker {
display: none;
}
</style>
</head>
<body class="bg-gray-50 min-h-screen flex flex-col">
<a class="skip-link" href="#main">Skip to lesson</a>
<header class="menubar">
<div class="menubar-inner">
<a class="wordmark" href="../../index.html"><i aria-hidden="true"></i>Python to AI</a>
<div class="mb-desktop">
<a class="mb-link" href="history_of_ai.html">History</a>
<details class="is-current">
<summary>Lessons ▾</summary>
<nav class="dropdown" aria-label="Lessons">
<a href="python.html" aria-current="page"><span>00</span>Python basics</a>
<a href="ml_basics.html"><span>01</span>ML basics</a>
<a href="supervised_learning.html"><span>02</span>Supervised learning</a>
<a href="unsupervised_learning.html"><span>03</span>Unsupervised learning</a>
<a href="recommendation_system.html"><span>04</span>Recommendation systems</a>
<a href="deep_learning.html"><span>05</span>Deep learning</a>
<a href="computer_vision.html"><span>06</span>Computer vision</a>
<a href="time_series.html"><span>07</span>Time series</a>
<a href="natural_language_processing.html"><span>08</span>Language (NLP)</a>
<a href="generative_ai.html"><span>09</span>Generative AI</a>
<a href="adversarial_ai.html"><span>10</span>Adversarial AI</a>
</nav>
</details>
<a class="mb-link" href="videos.html">Videos</a>
<a class="mb-link" href="https://github.com/ikathuria/python-to-ai" target="_blank" rel="noopener">GitHub</a>
</div>
<span class="mb-spacer"></span>
<button class="theme-toggle" type="button" aria-label="Toggle dark mode"><span class="when-light">◐ <span class="label">Dark</span></span><span class="when-dark">◑ <span class="label">Light</span></span></button>
<button class="mb-mobile-btn" type="button" aria-expanded="false" aria-controls="mobile-panel">Menu</button>
</div>
<nav id="mobile-panel" class="mobile-panel" aria-label="Site">
<a href="history_of_ai.html"><span>--</span>History of AI</a>
<a href="python.html" aria-current="page"><span>00</span>Python basics</a>
<a href="ml_basics.html"><span>01</span>ML basics</a>
<a href="supervised_learning.html"><span>02</span>Supervised learning</a>
<a href="unsupervised_learning.html"><span>03</span>Unsupervised learning</a>
<a href="recommendation_system.html"><span>04</span>Recommendation systems</a>
<a href="deep_learning.html"><span>05</span>Deep learning</a>
<a href="computer_vision.html"><span>06</span>Computer vision</a>
<a href="time_series.html"><span>07</span>Time series</a>
<a href="natural_language_processing.html"><span>08</span>Language (NLP)</a>
<a href="generative_ai.html"><span>09</span>Generative AI</a>
<a href="adversarial_ai.html"><span>10</span>Adversarial AI</a>
<a href="videos.html"><span>▶</span>Videos</a>
<a href="https://github.com/ikathuria/python-to-ai" target="_blank" rel="noopener"><span>{}</span>GitHub</a>
</nav>
</header>
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<div class="flex flex-col md:flex-row gap-8">
<!-- LEFT SIDEBAR -->
<aside class="md:w-64 flex-shrink-0">
<div class="sticky top-24 bg-white rounded-xl shadow-sm border border-gray-100 p-4">
<h3 class="text-xs font-bold text-gray-400 uppercase tracking-wider mb-3">Contents</h3>
<nav class="space-y-1">
<a href="#intro" class="sidebar-link active" onclick="setActive(this)">Introduction</a>
<a href="#data-types" class="sidebar-link" onclick="setActive(this)">Data Types</a>
<a href="#data-structures" class="sidebar-link" onclick="setActive(this)">Data Structures</a>
<a href="#functions" class="sidebar-link" onclick="setActive(this)">Functions</a>
<a href="#examples" class="sidebar-link" onclick="setActive(this)">Code Examples</a>
</nav>
</div>
</aside>
<!-- MAIN CONTENT -->
<main id="main" class="flex-grow min-w-0">
<!-- Introduction -->
<section id="intro" class="content-section">
<h1 class="text-4xl font-extrabold text-gray-900 mb-6">Understanding Basic Python Concepts</h1>
<p class="text-lg text-gray-600 mb-4">
This module covers the fundamental concepts of Python programming, including data types, data structures, functions, and error handling. By the end of this module, you should have a solid understanding of the basics of Python and be able to write simple programs.
</p>
</section>
<!-- Data Types -->
<section id="data-types" class="content-section">
<h2><i class="fa-solid fa-shapes mr-3 text-indigo-500"></i> Data Types</h2>
<p>Python has several built-in data types that are essential for handling different kinds of information:</p>
<div class="grid grid-cols-1 sm:grid-cols-2 gap-4 mt-4">
<div class="bg-white p-4 rounded-lg shadow-sm border border-gray-100">
<span class="font-bold text-gray-800">Numeric Types</span>
<div class="text-sm text-gray-500 mt-1">
<code>int</code> (integers), <code>float</code> (decimals), <code>complex</code>
</div>
</div>
<div class="bg-white p-4 rounded-lg shadow-sm border border-gray-100">
<span class="font-bold text-gray-800">Sequence Types</span>
<div class="text-sm text-gray-500 mt-1">
<code>list</code>, <code>tuple</code>, <code>range</code>
</div>
</div>
<div class="bg-white p-4 rounded-lg shadow-sm border border-gray-100">
<span class="font-bold text-gray-800">Mapping Type</span>
<div class="text-sm text-gray-500 mt-1">
<code>dict</code> (Key-Value pairs)
</div>
</div>
<div class="bg-white p-4 rounded-lg shadow-sm border border-gray-100">
<span class="font-bold text-gray-800">Boolean Type</span>
<div class="text-sm text-gray-500 mt-1">
<code>bool</code> (True/False)
</div>
</div>
</div>
</section>
<!-- Data Structures -->
<section id="data-structures" class="content-section">
<h2><i class="fa-solid fa-layer-group mr-3 text-indigo-500"></i> Data Structures</h2>
<p>Effectively using data structures is key to writing efficient Python code.</p>
<h3>Lists</h3>
<p>Ordered, mutable collections. Great for storing sequences of items.</p>
<pre><code class="language-python">my_list = [1, 2, 3, "apple"]
my_list.append("banana")
print(my_list[0]) # Output: 1</code></pre>
<h3>Dictionaries</h3>
<p>Unordered collections of key-value pairs. Ideal for lookups.</p>
<pre><code class="language-python">my_dict = {'name': 'Alice', 'role': 'Developer'}
print(my_dict['name']) # Output: Alice</code></pre>
<h3>NumPy Arrays</h3>
<p>Foundation for ML. Homogeneous collections enabling fast mathematical operations.</p>
<pre><code class="language-python">import numpy as np
my_array = np.array([1, 2, 3])
print(my_array * 2) # Output: [2 4 6]</code></pre>
</section>
<!-- Functions -->
<section id="functions" class="content-section">
<h2><i class="fa-solid fa-code-branch mr-3 text-indigo-500"></i> Functions</h2>
<p>Functions allow you to encapsulate logic and reuse code.</p>
<pre><code class="language-python">def calculate_area(radius):
"""Calculates the area of a circle."""
pi = 3.14159
return pi * (radius ** 2)
result = calculate_area(5)
print(f"Area: {result}")</code></pre>
</section>
<!-- Code Examples -->
<section id="examples" class="content-section">
<h2><i class="fa-solid fa-laptop-code mr-3 text-indigo-500"></i> Example: Basic Programs</h2>
<p>Here are some standard algorithms implemented in Python.</p>
<h3>Fibonacci Series</h3>
<pre><code class="language-python">def fibonnaci_series(n):
"""Prints n terms of the Fibonacci series."""
a, b = 0, 1
print(a, b, end=' ')
for i in range(n - 2):
c = a + b
print(c, end=' ')
a, b = b, c
print()
# Run it
fibonnaci_series(10)</code></pre>
<h3>Matrix Addition</h3>
<pre><code class="language-python">def add_matrices(mat1, mat2):
# Create result matrix
final = [[0 for _ in range(len(mat1[0]))] for _ in range(len(mat1))]
for i in range(len(mat1)):
for j in range(len(mat1[i])):
final[i][j] = mat1[i][j] + mat2[i][j]
return final
m1 = [[1, 2], [3, 4]]
m2 = [[5, 6], [7, 8]]
print(add_matrices(m1, m2))</code></pre>
</section>
<!-- Try it yourself -->
<details class="try-it bg-white border border-indigo-100 rounded-xl overflow-hidden mt-4">
<summary class="px-5 py-3 cursor-pointer font-semibold text-indigo-700 bg-indigo-50 hover:bg-indigo-100 transition flex items-center gap-2 select-none"><i class="fa-solid fa-laptop-code mr-1"></i> Try it yourself — List comprehension challenge</summary>
<div class="p-5"><p class="text-gray-700 mb-3">Write a single list comprehension that returns the squares of all odd numbers from 1 to 20.</p><details class="mt-3"><summary class="text-sm text-indigo-500 cursor-pointer hover:text-indigo-700 font-medium">Show hint</summary><div class="mt-2 p-3 bg-gray-50 border border-gray-200 rounded-lg text-sm text-gray-700"><code>[x**2 for x in range(1, 21) if x % 2 != 0]</code><br>Expected: [1, 9, 25, 49, 81, 121, 169, 225, 289, 361]</div></details></div>
</details>
<details class="try-it bg-white border border-indigo-100 rounded-xl overflow-hidden mt-4">
<summary class="px-5 py-3 cursor-pointer font-semibold text-indigo-700 bg-indigo-50 hover:bg-indigo-100 transition flex items-center gap-2 select-none"><i class="fa-solid fa-laptop-code mr-1"></i> Try it yourself — Function with *args</summary>
<div class="p-5"><p class="text-gray-700 mb-3">Write a function <code>running_total(*numbers)</code> that returns a list of cumulative sums.<br><code>running_total(1, 2, 3, 4)</code> should return <code>[1, 3, 6, 10]</code>.</p><details class="mt-3"><summary class="text-sm text-indigo-500 cursor-pointer hover:text-indigo-700 font-medium">Show hint</summary><div class="mt-2 p-3 bg-gray-50 border border-gray-200 rounded-lg text-sm text-gray-700"><pre style='margin:0'><code>def running_total(*numbers):
total = 0
result = []
for n in numbers:
total += n
result.append(total)
return result</code></pre></div></details></div>
</details>
<!-- Module Progress & Navigation -->
<div class="mt-12 pt-8 border-t border-gray-200">
<div class="flex flex-col sm:flex-row items-center justify-between gap-4">
<button id="complete-btn" onclick="toggleComplete()" class="flex items-center gap-2 px-5 py-2.5 rounded-xl font-semibold text-sm border-2 border-gray-300 text-gray-600 hover:border-indigo-300 hover:text-indigo-600 transition">
<i class="fa-regular fa-circle-check text-lg"></i>
<span>Mark as Complete</span>
</button>
<div class="flex gap-3">
<a href="ml_basics.html" class="flex items-center gap-2 px-4 py-2 bg-indigo-600 text-white rounded-xl text-sm font-semibold hover:bg-indigo-700 transition">ML Basics <i class="fa-solid fa-arrow-right text-xs"></i></a>
</div>
</div>
</div>
<script>
(function() {
var KEY = 'p2ai_done_python';
function sync() {
var btn = document.getElementById('complete-btn');
if (!btn) return;
var done = localStorage.getItem(KEY) === '1';
if (done) {
btn.className = 'flex items-center gap-2 px-5 py-2.5 rounded-xl font-semibold text-sm border-2 border-indigo-500 bg-indigo-50 text-indigo-700';
btn.innerHTML = '<i class=\"fa-solid fa-circle-check text-lg\"></i> <span>Completed!</span>';
} else {
btn.className = 'flex items-center gap-2 px-5 py-2.5 rounded-xl font-semibold text-sm border-2 border-gray-300 text-gray-600 hover:border-indigo-300 hover:text-indigo-600 transition';
btn.innerHTML = '<i class=\"fa-regular fa-circle-check text-lg\"></i> <span>Mark as Complete</span>';
}
}
window.markLessonComplete = function() {
try { localStorage.setItem(KEY, '1'); } catch (e) { }
sync();
};
window.toggleComplete = function() {
var done = localStorage.getItem(KEY) === '1';
localStorage.setItem(KEY, done ? '0' : '1');
sync();
};
document.addEventListener('DOMContentLoaded', sync);
})();
</script>
</main>
</div>
</div>
<!-- SCRIPT FOR SIDEBAR HIGHLIGHTING -->
<script>
function setActive(element) {
document.querySelectorAll('.sidebar-link').forEach(el => el.classList.remove('active'));
element.classList.add('active');
}
// Optional: Auto-highlight on scroll could be added here
</script>
<!-- FOOTER -->
<footer class="site-footer">
<div class="band-thin"></div>
<div class="inner"><div class="row">
<span>> built by Ishani Kathuria_</span>
<span class="links">
<a href="https://www.youtube.com/@DrIshaniKathuria" target="_blank" rel="noopener">YouTube</a>
<a href="https://github.com/ikathuria/python-to-ai" target="_blank" rel="noopener">GitHub</a>
<a href="https://linkedin.com/in/ishani-kathuria" target="_blank" rel="noopener">LinkedIn</a>
<a href="https://ishani.kathuria.net" target="_blank" rel="noopener">Website</a>
</span>
</div></div>
</footer>
<script>
document.querySelectorAll('pre').forEach(function(pre) {
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.className = 'copy-btn';
pre.style.position = 'relative';
pre.appendChild(btn);
btn.addEventListener('click', function() {
var code = pre.querySelector('code') ? pre.querySelector('code').innerText : pre.innerText;
navigator.clipboard.writeText(code).then(function() {
btn.textContent = 'Copied!';
btn.classList.add('copied');
setTimeout(function() { btn.textContent = 'Copy'; btn.classList.remove('copied'); }, 2000);
}).catch(function() { btn.textContent = 'Error'; setTimeout(function() { btn.textContent = 'Copy'; }, 2000); });
});
});
</script>
<script>
(function () {
var sections = Array.from(document.querySelectorAll("section[id]"));
var links = Array.from(document.querySelectorAll(".sidebar-link[href^='#']"));
if (!sections.length || !links.length) return;
function update() {
var threshold = window.innerHeight * 0.3;
var active = sections[0];
for (var i = 0; i < sections.length; i++) {
if (sections[i].getBoundingClientRect().top <= threshold) {
active = sections[i];
}
}
links.forEach(function (a) {
a.classList.toggle("active", a.getAttribute("href") === "#" + active.id);
});
}
window.addEventListener("scroll", update, { passive: true });
update();
})();
</script>
</body>
</html>