-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
4537 lines (3893 loc) · 225 KB
/
index.html
File metadata and controls
4537 lines (3893 loc) · 225 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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python Docs - Complete Tutorial</title>
<link rel="stylesheet" href="style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.44.0/min/vs/loader.js"></script>
<script src="script.js" defer></script>
</head>
<body>
<!-- NAVBAR -->
<nav id="navbar">
<div class="logo"><img src="Assets/logo.jpeg" alt="image"></div>
<div class="acc">
<button id="sign-in">Sign in</button>
</div>
</nav>
<!-- SIDEBAR -->
<aside id="sidebar" class="sidebar">
<h1>PYTHON TUTORIAL</h1>
<ul class="topics">
<li><a href="#" onclick="showSection('intro'); return false;" id="link-intro" class="active">INTRO</a></li>
<li><a href="#" onclick="showSection('syntax'); return false;" id="link-syntax">SYNTAX AND OUTPUT</a></li>
<li><a href="#" onclick="showSection('dandv'); return false;" id="link-dandv">DATATYPES AND VARIABLES</a>
</li>
<li><a href="#" onclick="showSection('typecast'); return false;" id="link-typecast">TYPECASTING</a></li>
<li><a href="#" onclick="showSection('operator'); return false;" id="link-operator">OPERATORS</a></li>
<li><a href="#" onclick="showSection('cs'); return false;" id="link-cs">CONDITIONAL STATEMENTS</a></li>
<li><a href="#" onclick="showSection('loop'); return false;" id="link-loop">LOOPS</a></li>
<li><a href="#" onclick="showSection('jump'); return false;" id="link-jump">JUMPING STATEMENTS</a></li>
<li><a href="#" onclick="showSection('function'); return false;" id="link-function">FUNCTIONS</a></li>
<li><a href="#" onclick="showSection('list'); return false;" id="link-list">LISTS</a></li>
<li><a href="#" onclick="showSection('dst'); return false;" id="link-dst">DICTIONARIES,SETS,TUPLES</a></li>
<li><a href="#" onclick="showSection('strings'); return false;" id="link-strings">STRINGS</a></li>
<li><a href="#" onclick="showSection('recursion'); return false;" id="link-recursion">RECURSION</a></li>
</ul>
<div id="toggle"><button id="mode-btn">Dark Mode</button></div>
</aside>
<!-- MAIN CONTENT -->
<main id="content" class="content">
<div id="intro" class="page-section" style="display: block;">
<!-- Page heading -->
<h1>Introduction to Python</h1>
<!-- SECTION: History of Python -->
<section id="history">
<h2>History</h2>
<p>Python was created by Guido van Rossum in the late 1980s and first released in 1991. It was designed
to emphasize code readability and developer productivity, using significant indentation to structure
code. Over time Python grew a strong community and a rich standard library.</p>
<p>Major milestones include Python 2 (widely used for many years) and the release of Python 3 in 2008,
which introduced improvements and some backward-incompatible changes. The community gradually
migrated to Python 3 as the primary version. Today, Python 3 is the standard, with Python 2
officially deprecated since January 1, 2020.</p>
<p><strong>Key Milestones:</strong></p>
<ul>
<li><strong>1991:</strong> Python 0.9.0 released with classes, exception handling, functions, and
core datatypes</li>
<li><strong>2000:</strong> Python 2.0 introduced list comprehensions and garbage collection</li>
<li><strong>2008:</strong> Python 3.0 released with major improvements (print function, better
Unicode support, cleaner syntax)</li>
<li><strong>2020:</strong> Python 2 officially sunset; Python 3 is the only supported version</li>
</ul>
</section>
<!-- SECTION: Why Choose Python? -->
<section id="why-python">
<h2>Why Choose Python?</h2>
<p>Python has become one of the most popular programming languages worldwide for several compelling
reasons:</p>
<ul>
<li><strong>Readable and Clean Syntax:</strong> Python's syntax is designed to be intuitive and
readable, often resembling plain English. This makes it easier to learn and maintain code.</li>
<li><strong>Beginner-Friendly:</strong> Python is often the first language taught in schools and
bootcamps because of its gentle learning curve.</li>
<li><strong>Versatile and Powerful:</strong> From web development to AI/ML, Python can handle
diverse tasks across multiple domains.</li>
<li><strong>Rich Ecosystem:</strong> Thousands of libraries and frameworks (NumPy, pandas, Django,
Flask, TensorFlow) extend Python's capabilities.</li>
<li><strong>Strong Community:</strong> Active community support, extensive documentation, and
countless tutorials make problem-solving easier.</li>
<li><strong>Cross-Platform:</strong> Python runs on Windows, macOS, Linux, and more.</li>
<li><strong>High Demand:</strong> Python developers are in high demand across industries, making it
a valuable career skill.</li>
</ul>
</section>
<!-- CODE TESTING BOX 1: Basic Python syntax example -->
<section id="try-basic">
<h3>Try It: Your First Python Program</h3>
<p>Below is a space to experiment with basic Python syntax. Try creating variables, printing output, and
performing simple calculations.</p>
<!-- Empty code box for future JavaScript implementation -->
<div class="code-box" id="code-box-1">
<!-- JavaScript will enable code execution here later -->
</div>
</section>
<!-- SECTION: Key Features of Python -->
<section id="key-features">
<h2>Key Features</h2>
<h3>Interpreted Language</h3>
<p>Python code is executed line by line by the Python interpreter, making debugging easier and allowing
for interactive programming in environments like IDLE or Jupyter notebooks.</p>
<h3>Dynamically Typed</h3>
<p>You don't need to declare variable types explicitly. Python infers types at runtime, making code more
flexible but requiring careful testing.</p>
<h3>Object-Oriented</h3>
<p>Python supports object-oriented programming with classes and objects, enabling code reusability and
modular design.</p>
<h3>Extensive Standard Library</h3>
<p>Python comes with a "batteries included" philosophy, providing modules for file I/O, regular
expressions, web protocols, data serialization, and much more.</p>
<h3>Multi-Paradigm</h3>
<p>Python supports procedural, object-oriented, and functional programming paradigms, giving developers
flexibility in how they structure their code.</p>
</section>
<!-- SECTION: Common Use Cases -->
<section id="use-cases">
<h2>Use Cases</h2>
<p>Python is a versatile language used across many domains because of its simple syntax and large
ecosystem of libraries and frameworks. Common use cases include:</p>
<h3>Web Development</h3>
<p>Frameworks like <strong>Django</strong> and <strong>Flask</strong> make it easy to build robust,
scalable web applications quickly. Django provides a full-featured MVC framework, while Flask offers
a lightweight, flexible approach.</p>
<h3>Data Science and Analysis</h3>
<p>Python dominates the data science field with libraries like <strong>NumPy</strong>,
<strong>pandas</strong>, and <strong>Matplotlib</strong>. These tools enable data manipulation,
statistical analysis, and visualization.</p>
<h3>Machine Learning and AI</h3>
<p>Libraries such as <strong>scikit-learn</strong>, <strong>TensorFlow</strong>, and
<strong>PyTorch</strong> make Python the go-to language for machine learning, deep learning, and
artificial intelligence research and applications.</p>
<h3>Automation and Scripting</h3>
<p>Python excels at automating repetitive tasks like file management, web scraping (with
<strong>BeautifulSoup</strong> and <strong>Scrapy</strong>), and system administration.</p>
<h3>Scientific Computing</h3>
<p>Python is widely used in scientific research and engineering with tools like <strong>SciPy</strong>,
<strong>SymPy</strong>, and <strong>Biopython</strong> for complex calculations and simulations.</p>
<h3>Game Development</h3>
<p>While not the primary choice for AAA games, Python is used for game development with libraries like
<strong>Pygame</strong> for 2D games and prototyping.</p>
<h3>Education</h3>
<p>Python's simplicity makes it ideal for teaching programming fundamentals in schools, universities,
and online courses.</p>
<h3>DevOps and Cloud</h3>
<p>Python is used for infrastructure automation, configuration management (Ansible), and cloud service
interactions (AWS SDK, Google Cloud SDK).</p>
</section>
<!-- CODE TESTING BOX 2: Exploring Python's versatility -->
<section id="try-versatility">
<h3>Try It: Explore Python's Versatility</h3>
<p>Experiment with different Python operations: string manipulation, list operations, or simple math
calculations.</p>
<!-- Empty code box for future JavaScript implementation -->
<div class="code-box" id="code-box-2">
<!-- JavaScript will enable code execution here later -->
</div>
</section>
<!-- SECTION: Getting Started -->
<section id="getting-started">
<h2>Getting Started</h2>
<h3>Installing Python</h3>
<p>To start programming in Python, you need to install it on your computer:</p>
<ol>
<li>Visit the official Python website: <a href="https://www.python.org"
target="_blank">python.org</a></li>
<li>Download the latest version of Python 3</li>
<li>Run the installer (make sure to check "Add Python to PATH" on Windows)</li>
<li>Verify installation by opening a terminal/command prompt and typing:
<code>python --version</code></li>
</ol>
<h3>Your First Program</h3>
<p>The traditional first program in any language is "Hello, World!". In Python, it's remarkably simple:
</p>
<div class="callout">
<code>print("Hello, World!")</code>
</div>
<p>That's it! Just one line creates output. This simplicity is what makes Python so appealing to
beginners.</p>
<h3>Development Environments</h3>
<p>You can write Python code in various environments:</p>
<ul>
<li><strong>IDLE:</strong> Comes bundled with Python, simple and lightweight</li>
<li><strong>VS Code:</strong> Popular, feature-rich editor with excellent Python support</li>
<li><strong>PyCharm:</strong> Professional IDE specifically designed for Python</li>
<li><strong>Jupyter Notebook:</strong> Interactive environment perfect for data science and learning
</li>
<li><strong>Sublime Text / Atom:</strong> Lightweight text editors with Python plugins</li>
</ul>
</section>
<!-- CODE TESTING BOX 3: Hello World variations -->
<section id="try-hello">
<h3>Try It: Create Your Own Hello World</h3>
<p>Practice writing your first Python program. Try variations of Hello World with different messages.
</p>
<!-- Empty code box for future JavaScript implementation -->
<div class="code-box" id="code-box-3">
<!-- JavaScript will enable code execution here later -->
</div>
</section>
<!-- SECTION: What's Next? -->
<section id="whats-next">
<h2>What's Next?</h2>
<p>Now that you understand what Python is and why it's valuable, you're ready to dive into the
fundamentals:</p>
<ul>
<li><strong>Syntax and Output:</strong> Learn how Python code is structured and how to display
information</li>
<li><strong>Datatypes and Variables:</strong> Understand how to store and manage data</li>
<li><strong>Control Flow:</strong> Make decisions and repeat actions in your programs</li>
<li><strong>Functions:</strong> Organize code into reusable blocks</li>
<li><strong>Data Structures:</strong> Work with collections of data efficiently</li>
</ul>
<p>Use the sidebar to navigate to the next topic and continue your Python learning journey!</p>
</section>
<footer>© Pyman Docs all rights limited</footer>
</div>
<div id="syntax" class="page-section" style="display: none;">
<!-- Page heading -->
<h1>Syntax and Output</h1>
<!-- SECTION: Overview -->
<section id="overview">
<h2>Overview</h2>
<p>Python code is written using a clean, easy-to-read syntax that emphasizes readability and simplicity.
Unlike many other programming languages, Python uses <strong>indentation (whitespace)</strong> to
define code blocks instead of braces or keywords. Output is typically produced with the built-in
<code>print()</code> function.</p>
<p>This design philosophy makes Python code look almost like pseudocode, which reduces the learning
curve and makes programs easier to understand and maintain.</p>
</section>
<!-- SECTION: Basic Syntax Rules -->
<section id="syntax-rules">
<h2>Basic Syntax Rules</h2>
<h3>1. Statements and Line Endings</h3>
<p>In Python, each statement typically appears on its own line. Unlike languages like C or Java, you
don't need to end statements with a semicolon (though you can use semicolons to separate multiple
statements on one line).</p>
<div class="callout">
<code>name = "Alice" # This is a complete statement</code><br>
<code>age = 25 # Each variable assignment is on its own line</code>
</div>
<h3>2. Line Continuation</h3>
<p>Long statements can be split across multiple lines using a backslash (<code>\</code>) or by placing
expressions inside parentheses, brackets, or braces.</p>
<div class="callout">
<code>total = 1 + 2 + 3 + \</code><br>
<code> 4 + 5 + 6</code><br><br>
<code># Or using parentheses:</code><br>
<code>total = (1 + 2 + 3 +</code><br>
<code> 4 + 5 + 6)</code>
</div>
<h3>3. Case Sensitivity</h3>
<p>Python is case-sensitive, meaning <code>Variable</code>, <code>variable</code>, and
<code>VARIABLE</code> are three different identifiers.</p>
</section>
<!-- CODE TESTING BOX 1: Basic syntax practice -->
<section id="try-basic-syntax">
<h3>Try It: Practice Basic Syntax</h3>
<p>Write simple Python statements and see how line endings work. Try creating variables and printing
them.</p>
<!-- Empty code box for future JavaScript implementation -->
<div class="code-box" id="code-box-1">
<!-- JavaScript will enable code execution here later -->
</div>
</section>
<!-- SECTION: Indentation -->
<section id="indentation">
<h2>Indentation</h2>
<p><strong>Indentation is critical in Python</strong> – it's not just for readability, it's part of the
syntax. Indentation defines code blocks and scope.</p>
<h3>Why Indentation Matters</h3>
<ul>
<li>Indentation replaces braces <code>{ }</code> used in other languages</li>
<li>All statements within a block must be indented by the same amount</li>
<li>The standard is <strong>4 spaces</strong> per indentation level (not tabs)</li>
<li>Inconsistent indentation will cause an <code>IndentationError</code></li>
</ul>
<h3>Example:</h3>
<div class="callout">
<code>if True:</code><br>
<code> print("This is indented") # 4 spaces</code><br>
<code> print("Still in the block") # Same indentation</code><br>
<code>print("Back to no indentation") # Outside the block</code>
</div>
<p><strong>Important:</strong> Never mix tabs and spaces for indentation. Most modern code editors
automatically convert tabs to spaces.</p>
</section>
<!-- CODE TESTING BOX 2: Indentation practice -->
<section id="try-indentation">
<h3>Try It: Practice Indentation</h3>
<p>Experiment with indentation by creating if statements or other block structures. See what happens
with incorrect indentation.</p>
<!-- Empty code box for future JavaScript implementation -->
<div class="code-box" id="code-box-2">
<!-- JavaScript will enable code execution here later -->
</div>
</section>
<!-- SECTION: Comments -->
<section id="comments">
<h2>Comments</h2>
<p>Comments are notes in your code that Python ignores during execution. They're essential for
documenting your code and making it understandable.</p>
<h3>Single-Line Comments</h3>
<p>Use the hash symbol <code>#</code> to create a single-line comment. Everything after <code>#</code>
on that line is ignored.</p>
<div class="callout">
<code># This is a comment</code><br>
<code>x = 5 # This is also a comment (inline)</code>
</div>
<h3>Multi-Line Comments</h3>
<p>Python doesn't have a specific multi-line comment syntax, but you can use multiple <code>#</code>
symbols or triple quotes (which create multi-line strings that can serve as comments).</p>
<div class="callout">
<code>"""</code><br>
<code>This is a multi-line comment.</code><br>
<code>It can span several lines.</code><br>
<code>Useful for documentation.</code><br>
<code>"""</code>
</div>
<h3>Best Practices for Comments</h3>
<ul>
<li>Write comments that explain <em>why</em>, not <em>what</em> (the code itself shows what)</li>
<li>Keep comments up-to-date when code changes</li>
<li>Use comments to explain complex logic or algorithms</li>
<li>Avoid obvious comments like <code># increment x</code> for <code>x = x + 1</code></li>
</ul>
</section>
<!-- SECTION: The Print Function -->
<section id="print-function">
<h2>The Print Function</h2>
<p>The <code>print()</code> function is your primary tool for displaying output in Python. It sends text
or data to the console/terminal.</p>
<h3>Basic Usage</h3>
<div class="callout">
<code>print("Hello, World!")</code><br>
<code>print(42)</code><br>
<code>print(3.14159)</code>
</div>
<h3>Printing Multiple Items</h3>
<p>You can print multiple items separated by commas. Python automatically adds a space between them.</p>
<div class="callout">
<code>print("Age:", 25)</code><br>
<code># Output: Age: 25</code>
</div>
<h3>Customizing Separators</h3>
<p>Use the <code>sep</code> parameter to change how items are separated:</p>
<div class="callout">
<code>print("apple", "banana", "cherry", sep=", ")</code><br>
<code># Output: apple, banana, cherry</code>
</div>
<h3>Controlling Line Endings</h3>
<p>By default, <code>print()</code> adds a newline at the end. Use the <code>end</code> parameter to
change this:</p>
<div class="callout">
<code>print("Hello", end=" ")</code><br>
<code>print("World")</code><br>
<code># Output: Hello World</code>
</div>
<h3>String Formatting in Print</h3>
<p>Modern Python offers several ways to format strings for output:</p>
<h4>1. f-strings (Python 3.6+) - Recommended</h4>
<div class="callout">
<code>name = "Alice"</code><br>
<code>age = 30</code><br>
<code>print(f"My name is {name} and I am {age} years old")</code>
</div>
<h4>2. .format() Method</h4>
<div class="callout">
<code>print("My name is {} and I am {} years old".format(name, age))</code>
</div>
<h4>3. % Operator (Older Style)</h4>
<div class="callout">
<code>print("My name is %s and I am %d years old" % (name, age))</code>
</div>
</section>
<!-- CODE TESTING BOX 3: Print function practice -->
<section id="try-print">
<h3>Try It: Master the Print Function</h3>
<p>Practice using print() with different parameters. Try sep, end, and various formatting methods.</p>
<!-- Empty code box for future JavaScript implementation -->
<div class="code-box" id="code-box-3">
<!-- JavaScript will enable code execution here later -->
</div>
</section>
<!-- SECTION: Receiving Input -->
<section id="input-function">
<h2>Receiving Input</h2>
<p>The <code>input()</code> function allows your program to receive text input from the user. It
displays a prompt and waits for the user to type something and press Enter.</p>
<h3>Basic Usage</h3>
<div class="callout">
<code>name = input("Enter your name: ")</code><br>
<code>print(f"Hello, {name}!")</code>
</div>
<p><strong>Important:</strong> <code>input()</code> always returns a <strong>string</strong>. If you
need a number, you must convert it:</p>
<div class="callout">
<code>age = int(input("Enter your age: ")) # Convert to integer</code><br>
<code>height = float(input("Enter height: ")) # Convert to float</code>
</div>
</section>
<!-- CODE TESTING BOX 4: Input and Output combined -->
<section id="try-input-output">
<h3>Try It: Combine Input and Output</h3>
<p>Create a program that asks for user input and displays formatted output. Try different data types and
formatting.</p>
<!-- Empty code box for future JavaScript implementation -->
<div class="code-box" id="code-box-4">
<!-- JavaScript will enable code execution here later -->
</div>
</section>
<!-- SECTION: Common Syntax Errors -->
<section id="common-errors">
<h2>Common Syntax Errors to Avoid</h2>
<ul>
<li><strong>IndentationError:</strong> Mixing tabs and spaces, or inconsistent indentation levels
</li>
<li><strong>SyntaxError:</strong> Missing colons after if, for, while, def, or class statements</li>
<li><strong>NameError:</strong> Using variables before defining them or misspelling variable names
</li>
<li><strong>TypeError:</strong> Trying to concatenate strings with numbers without conversion</li>
</ul>
<p>Always read error messages carefully – Python error messages are usually helpful in identifying what
went wrong and where.</p>
</section>
<!-- SECTION: Quick Reference -->
<section id="quick-reference">
<h2>Quick Reference</h2>
<div class="callout">
<strong>Key Takeaways:</strong>
<ul>
<li>Statements end with a newline (no semicolons needed)</li>
<li>Use <strong>4 spaces</strong> for each indentation level</li>
<li>Comments start with <code>#</code></li>
<li><code>print()</code> displays output</li>
<li><code>input()</code> receives user input (always returns a string)</li>
<li>Python is case-sensitive</li>
<li>Indentation defines code blocks</li>
</ul>
</div>
</section>
<!-- SECTION: Next Steps -->
<section id="next-steps">
<h2>Next Steps</h2>
<p>Now that you understand Python's syntax and how to produce output, you're ready to learn about
<strong>datatypes and variables</strong> – the building blocks for storing and manipulating data in
your programs.</p>
</section>
<footer>© Pyman Docs all rights limited</footer>
</div>
<div id="dandv" class="page-section" style="display: none;">
<!-- Page heading -->
<h1>Datatypes & Variables</h1>
<!-- SECTION: Overview -->
<section id="overview">
<h2>Overview</h2>
<p>Python provides several built-in datatypes for storing different kinds of information: numbers, text,
collections, and more. <strong>Variables</strong> are containers that store these values, and Python
is <strong>dynamically typed</strong>, meaning you don't need to declare a variable's type
explicitly – Python infers it automatically.</p>
<p>Understanding datatypes is fundamental to programming because different types of data support
different operations and behave differently in your programs.</p>
</section>
<!-- SECTION: Variables -->
<section id="variables">
<h2>Variables</h2>
<h3>What is a Variable?</h3>
<p>A variable is a named location in memory that stores a value. Think of it as a labeled box where you
can put data and retrieve it later by using the label (variable name).</p>
<h3>Creating Variables</h3>
<p>In Python, you create a variable simply by assigning a value to it using the equals sign
<code>=</code>:</p>
<div class="callout">
<code>name = "Alice" # String variable</code><br>
<code>age = 25 # Integer variable</code><br>
<code>height = 5.6 # Float variable</code><br>
<code>is_student = True # Boolean variable</code>
</div>
<h3>Variable Naming Rules</h3>
<p>Follow these rules when naming variables:</p>
<ul>
<li>Must start with a letter (a-z, A-Z) or underscore (_)</li>
<li>Can contain letters, numbers, and underscores</li>
<li>Cannot start with a number</li>
<li>Cannot use Python keywords (like <code>if</code>, <code>for</code>, <code>while</code>, etc.)
</li>
<li>Case-sensitive: <code>age</code> and <code>Age</code> are different variables</li>
</ul>
<h3>Naming Conventions (Best Practices)</h3>
<ul>
<li>Use <strong>snake_case</strong> for variable names: <code>user_name</code>,
<code>total_amount</code></li>
<li>Use descriptive names: <code>student_count</code> instead of <code>sc</code></li>
<li>Constants should be in UPPERCASE: <code>MAX_SIZE = 100</code></li>
<li>Avoid single-letter names except for counters (<code>i</code>, <code>j</code>) or mathematical
formulas</li>
</ul>
<h3>Multiple Assignment</h3>
<p>Python allows you to assign multiple variables in one line:</p>
<div class="callout">
<code>x, y, z = 10, 20, 30</code><br>
<code>a = b = c = 0 # All assigned the same value</code>
</div>
</section>
<!-- CODE TESTING BOX 1: Variable practice -->
<section id="try-variables">
<h3>Try It: Create and Use Variables</h3>
<p>Practice creating variables with different names and values. Try multiple assignment and see how
variables work.</p>
<!-- Empty code box for future JavaScript implementation -->
<div class="code-box" id="code-box-1">
<!-- JavaScript will enable code execution here later -->
</div>
</section>
<!-- SECTION: Numeric Types -->
<section id="numeric-types">
<h2>Numeric Datatypes</h2>
<h3>1. Integer (int)</h3>
<p>Whole numbers without a decimal point. Can be positive, negative, or zero.</p>
<div class="callout">
<code>count = 42</code><br>
<code>temperature = -15</code><br>
<code>zero = 0</code>
</div>
<p><strong>Note:</strong> Python integers have unlimited precision – they can be as large as your memory
allows!</p>
<h3>2. Float (float)</h3>
<p>Numbers with decimal points. Used for measurements, calculations requiring precision, etc.</p>
<div class="callout">
<code>pi = 3.14159</code><br>
<code>price = 19.99</code><br>
<code>negative_float = -7.5</code>
</div>
<p><strong>Scientific Notation:</strong> You can use <code>e</code> for powers of 10:</p>
<div class="callout">
<code>large_number = 1.5e6 # 1,500,000</code><br>
<code>small_number = 2.5e-3 # 0.0025</code>
</div>
<h3>3. Complex (complex)</h3>
<p>Numbers with real and imaginary parts, using <code>j</code> to denote the imaginary component.</p>
<div class="callout">
<code>z = 3 + 4j</code><br>
<code>print(z.real) # 3.0</code><br>
<code>print(z.imag) # 4.0</code>
</div>
<h3>Checking Type</h3>
<p>Use the <code>type()</code> function to check a variable's datatype:</p>
<div class="callout">
<code>x = 10</code><br>
<code>print(type(x)) # <class 'int'></code>
</div>
</section>
<!-- CODE TESTING BOX 2: Numeric types practice -->
<section id="try-numeric">
<h3>Try It: Work with Numbers</h3>
<p>Create variables of different numeric types. Use type() to check their types and perform basic
arithmetic.</p>
<!-- Empty code box for future JavaScript implementation -->
<div class="code-box" id="code-box-2">
<!-- JavaScript will enable code execution here later -->
</div>
</section>
<!-- SECTION: String Type -->
<section id="string-type">
<h2>String (str)</h2>
<p>Strings represent text data. They can be enclosed in single quotes, double quotes, or triple quotes
for multi-line strings.</p>
<h3>Creating Strings</h3>
<div class="callout">
<code>name = "Alice" # Double quotes</code><br>
<code>greeting = 'Hello' # Single quotes</code><br>
<code>message = """This is</code><br>
<code>a multi-line</code><br>
<code>string""" # Triple quotes</code>
</div>
<h3>String Operations</h3>
<ul>
<li><strong>Concatenation:</strong> <code>"Hello" + " " + "World"</code> →
<code>"Hello World"</code></li>
<li><strong>Repetition:</strong> <code>"Ha" * 3</code> → <code>"HaHaHa"</code></li>
<li><strong>Length:</strong> <code>len("Python")</code> → <code>6</code></li>
<li><strong>Indexing:</strong> <code>"Python"[0]</code> → <code>"P"</code></li>
</ul>
<p>Strings are <strong>immutable</strong> – once created, they cannot be changed. Any operation that
appears to modify a string actually creates a new string.</p>
</section>
<!-- SECTION: Boolean Type -->
<section id="boolean-type">
<h2>Boolean (bool)</h2>
<p>Booleans represent truth values: <code>True</code> or <code>False</code>. They're essential for
conditional logic and decision-making in programs.</p>
<div class="callout">
<code>is_active = True</code><br>
<code>has_permission = False</code><br><br>
<code># From comparisons:</code><br>
<code>result = (5 > 3) # True</code><br>
<code>equal = (10 == 20) # False</code>
</div>
<h3>Truthy and Falsy Values</h3>
<p>In Python, certain values are considered "falsy" (evaluate to False in boolean context):</p>
<ul>
<li><code>None</code></li>
<li><code>False</code></li>
<li>Zero: <code>0</code>, <code>0.0</code></li>
<li>Empty sequences: <code>""</code>, <code>[]</code>, <code>()</code>, <code>{}</code></li>
</ul>
<p>All other values are "truthy" (evaluate to True).</p>
</section>
<!-- SECTION: Collection Types -->
<section id="collection-types">
<h2>Collection Datatypes</h2>
<h3>1. List (list)</h3>
<p>Ordered, mutable collection of items. Items can be of different types.</p>
<div class="callout">
<code>fruits = ["apple", "banana", "cherry"]</code><br>
<code>mixed = [1, "text", 3.14, True]</code><br>
<code>empty_list = []</code>
</div>
<p>Lists are versatile and the most commonly used collection type in Python.</p>
<h3>2. Tuple (tuple)</h3>
<p>Ordered, <strong>immutable</strong> collection. Once created, cannot be modified.</p>
<div class="callout">
<code>coordinates = (10, 20)</code><br>
<code>rgb_color = (255, 128, 0)</code><br>
<code>single_item = (42,) # Note the comma</code>
</div>
<p>Use tuples for data that shouldn't change, like coordinates or configuration values.</p>
<h3>3. Dictionary (dict)</h3>
<p>Unordered collection of key-value pairs. Keys must be unique and immutable.</p>
<div class="callout">
<code>person = {</code><br>
<code> "name": "Alice",</code><br>
<code> "age": 30,</code><br>
<code> "city": "Seattle"</code><br>
<code>}</code><br>
<code>print(person["name"]) # Access by key</code>
</div>
<h3>4. Set (set)</h3>
<p>Unordered collection of <strong>unique</strong> items. Automatically removes duplicates.</p>
<div class="callout">
<code>unique_numbers = {1, 2, 3, 4, 5}</code><br>
<code>letters = {'a', 'b', 'c'}</code><br>
<code># Duplicates are removed:</code><br>
<code>numbers = {1, 2, 2, 3, 3, 3} # Result: {1, 2, 3}</code>
</div>
</section>
<!-- CODE TESTING BOX 3: Collection types practice -->
<section id="try-collections">
<h3>Try It: Explore Collections</h3>
<p>Create lists, tuples, dictionaries, and sets. Access their elements and see how they behave
differently.</p>
<!-- Empty code box for future JavaScript implementation -->
<div class="code-box" id="code-box-3">
<!-- JavaScript will enable code execution here later -->
</div>
</section>
<!-- SECTION: None Type -->
<section id="none-type">
<h2>None Type</h2>
<p><code>None</code> is Python's null value. It represents the absence of a value or a null reference.
</p>
<div class="callout">
<code>result = None</code><br>
<code>if result is None:</code><br>
<code> print("No value assigned")</code>
</div>
<p>Use <code>is None</code> or <code>is not None</code> to check for None values, not
<code>== None</code>.</p>
</section>
<!-- SECTION: Type Conversion -->
<section id="type-conversion-intro">
<h2>Quick Type Conversion</h2>
<p>You can convert between types using built-in functions:</p>
<div class="callout">
<code># To integer:</code><br>
<code>int("42") # "42" → 42</code><br>
<code>int(3.14) # 3.14 → 3 (truncates)</code><br><br>
<code># To float:</code><br>
<code>float("3.14") # "3.14" → 3.14</code><br>
<code>float(42) # 42 → 42.0</code><br><br>
<code># To string:</code><br>
<code>str(42) # 42 → "42"</code><br>
<code>str(3.14) # 3.14 → "3.14"</code><br><br>
<code># To boolean:</code><br>
<code>bool(0) # 0 → False</code><br>
<code>bool(42) # 42 → True</code>
</div>
<p>See the <a href="typecast.html">Typecasting</a> section for more detailed information on type
conversion.</p>
</section>
<!-- CODE TESTING BOX 4: Type conversion practice -->
<section id="try-conversion">
<h3>Try It: Convert Between Types</h3>
<p>Practice converting values between different datatypes. Use type() to verify the conversions.</p>
<!-- Empty code box for future JavaScript implementation -->
<div class="code-box" id="code-box-4">
<!-- JavaScript will enable code execution here later -->
</div>
</section>
<!-- SECTION: Quick Reference -->
<section id="quick-reference">
<h2>Quick Reference</h2>
<div class="callout">
<strong>Summary of Common Datatypes:</strong>
<ul>
<li><strong>int</strong> — Integers (whole numbers): <code>42</code>, <code>-3</code></li>
<li><strong>float</strong> — Floating point numbers: <code>3.14</code>, <code>-0.5</code></li>
<li><strong>str</strong> — Strings (text): <code>"Hello"</code>, <code>'Python'</code></li>
<li><strong>bool</strong> — Booleans: <code>True</code>, <code>False</code></li>
<li><strong>list</strong> — Ordered, mutable: <code>[1, 2, 3]</code></li>
<li><strong>tuple</strong> — Ordered, immutable: <code>(1, 2, 3)</code></li>
<li><strong>dict</strong> — Key-value pairs: <code>{"key": "value"}</code></li>
<li><strong>set</strong> — Unique items: <code>{1, 2, 3}</code></li>
<li><strong>NoneType</strong> — Null value: <code>None</code></li>
</ul>
</div>
</section>
<!-- SECTION: Next Steps -->
<section id="next-steps">
<h2>Next Steps</h2>
<p>Now that you understand Python's datatypes and how to store data in variables, you're ready to learn
about <strong>typecasting</strong> – explicitly converting data from one type to another for more
control in your programs.</p>
</section>
<footer>© Pyman Docs all rights limited</footer>
</div>
<div id="typecast" class="page-section" style="display: none;">
<!-- Page heading -->
<h1>Typecasting</h1>
<!-- SECTION: Overview -->
<section id="overview">
<h2>Overview</h2>
<p>Typecasting (also called type conversion) is the process of changing a value from one datatype to
another. Python performs some conversions automatically (<strong>implicit conversion</strong>), but
often you'll need to convert explicitly (<strong>explicit conversion</strong>) using built-in
functions like <code>int()</code>, <code>float()</code>, <code>str()</code>, and
<code>list()</code>.</p>
<p>Understanding typecasting is crucial because different operations require specific datatypes, and
user input always comes as strings and often needs conversion.</p>
</section>
<!-- SECTION: Implicit vs Explicit -->
<section id="implicit-explicit">
<h2>Implicit vs Explicit Conversion</h2>
<h3>Implicit Conversion (Automatic)</h3>
<p>Python automatically converts smaller datatypes to larger ones to prevent data loss. This happens
without your intervention.</p>
<div class="callout">
<code>x = 10 # int</code><br>
<code>y = 3.5 # float</code><br>
<code>result = x + y # Python converts x to float: 13.5</code><br>
<code>print(type(result)) # <class 'float'></code>
</div>
<p>In this example, Python automatically converts the integer to a float before adding because float is
a "wider" type that can represent both integers and decimals.</p>
<h3>Explicit Conversion (Manual)</h3>
<p>You explicitly convert values using conversion functions. This gives you full control over how data
is converted.</p>
<div class="callout">
<code>text = "42"</code><br>
<code>number = int(text) # Explicitly convert string to int</code><br>
<code>print(number + 8) # Now we can do math: 50</code>
</div>
</section>
<!-- CODE TESTING BOX 1: Implicit vs explicit practice -->
<section id="try-conversions">
<h3>Try It: Explore Implicit and Explicit Conversion</h3>
<p>Experiment with automatic and manual type conversions. See what happens when you mix different types.
</p>
<!-- Empty code box for future JavaScript implementation -->
<div class="code-box" id="code-box-1">
<!-- JavaScript will enable code execution here later -->
</div>
</section>
<!-- SECTION: Common Conversion Functions -->
<section id="conversion-functions">
<h2>Common Conversion Functions</h2>
<h3>1. Converting to Integer: int()</h3>
<p>Converts a value to an integer (whole number). Truncates decimal portions.</p>
<div class="callout">
<code># From float (truncates):</code><br>
<code>int(3.99) # → 3 (not rounded!)</code><br>
<code>int(-2.7) # → -2</code><br><br>
<code># From string:</code><br>
<code>int("42") # → 42</code><br>
<code>int(" 100 ") # → 100 (ignores whitespace)</code><br><br>
<code># From boolean:</code><br>
<code>int(True) # → 1</code><br>
<code>int(False) # → 0</code>
</div>
<p><strong>Important:</strong> <code>int("3.14")</code> will cause an error! You must convert to float
first: <code>int(float("3.14"))</code></p>
<h3>2. Converting to Float: float()</h3>
<p>Converts a value to a floating-point number (with decimal).</p>
<div class="callout">
<code># From integer:</code><br>
<code>float(42) # → 42.0</code><br><br>
<code># From string:</code><br>
<code>float("3.14") # → 3.14</code><br>
<code>float("100") # → 100.0</code><br><br>
<code># From boolean:</code><br>
<code>float(True) # → 1.0</code><br>
<code>float(False) # → 0.0</code>
</div>
<h3>3. Converting to String: str()</h3>
<p>Converts any value to a string representation. This almost always works!</p>
<div class="callout">
<code>str(42) # → "42"</code><br>
<code>str(3.14) # → "3.14"</code><br>
<code>str(True) # → "True"</code><br>
<code>str([1, 2, 3]) # → "[1, 2, 3]"</code><br>
<code>str(None) # → "None"</code>
</div>
<p><strong>Use Case:</strong> Combining numbers with text in print statements:</p>
<div class="callout">
<code>age = 25</code><br>
<code>message = "I am " + str(age) + " years old"</code><br>
<code># Or use f-strings (better): f"I am {age} years old"</code>
</div>
<h3>4. Converting to Boolean: bool()</h3>
<p>Converts any value to a boolean (True or False).</p>
<div class="callout">
<code># Falsy values → False:</code><br>
<code>bool(0) # → False</code><br>
<code>bool("") # → False (empty string)</code><br>
<code>bool([]) # → False (empty list)</code><br>
<code>bool(None) # → False</code><br><br>
<code># Everything else → True:</code><br>
<code>bool(42) # → True</code><br>
<code>bool("Hello") # → True</code><br>
<code>bool([1, 2, 3]) # → True</code>
</div>
<h3>5. Converting to List, Tuple, Set</h3>
<p>Convert between collection types:</p>
<div class="callout">
<code># String to list:</code><br>
<code>list("abc") # → ['a', 'b', 'c']</code><br><br>
<code># Tuple to list:</code><br>
<code>list((1, 2, 3)) # → [1, 2, 3]</code><br><br>
<code># List to tuple:</code><br>
<code>tuple([1, 2, 3]) # → (1, 2, 3)</code><br><br>
<code># List to set (removes duplicates):</code><br>
<code>set([1, 2, 2, 3]) # → {1, 2, 3}</code>
</div>
</section>
<!-- CODE TESTING BOX 2: Conversion functions practice -->
<section id="try-functions">
<h3>Try It: Practice Conversion Functions</h3>
<p>Use int(), float(), str(), bool(), and collection conversion functions. Try edge cases!</p>
<!-- Empty code box for future JavaScript implementation -->
<div class="code-box" id="code-box-2">
<!-- JavaScript will enable code execution here later -->
</div>
</section>