-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnp.html
More file actions
478 lines (435 loc) · 22 KB
/
np.html
File metadata and controls
478 lines (435 loc) · 22 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
<!DOCTYPE html>
<html lang="en" class="light">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ultimate Python Cheatsheet</title>
<script src="https://cdn.tailwindcss.com"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<script>
tailwind.config = {
darkMode: 'class',
theme: {
extend: {
colors: {
python: {
blue: '#3776AB',
yellow: '#FFD43B',
dark: '#0D1117',
light: '#F8F9FA'
}
}
}
}
}
</script>
<style>
.modal {
transition: opacity 0.3s ease, transform 0.3s ease;
}
.modal-hidden {
opacity: 0;
transform: translateY(20px);
pointer-events: none;
}
.code-block {
font-family: 'Fira Code', monospace;
}
</style>
</head>
<body class="bg-gray-50 dark:bg-gray-900 transition-colors duration-200">
<!-- Header -->
<header class="bg-python-blue text-white shadow-lg">
<div class="container mx-auto px-4 py-6">
<div class="flex justify-between items-center">
<div class="flex items-center space-x-4">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Python-logo-notext.svg/1869px-Python-logo-notext.svg.png"
alt="Python Logo" class="h-12 w-12">
<h1 class="text-3xl font-bold">Ultimate Python Cheatsheet</h1>
</div>
<div class="flex items-center space-x-4">
<button id="theme-toggle" class="p-2 rounded-full bg-python-yellow text-python-blue hover:bg-yellow-300 transition">
<i class="fas fa-moon dark:hidden"></i>
<i class="fas fa-sun hidden dark:block"></i>
</button>
<a href="https://github.com/femonla02" class="p-2 rounded-full bg-python-dark hover:bg-gray-800 transition">
<i class="fab fa-github"></i>
</a>
</div>
</div>
<p class="mt-4 text-python-yellow font-medium">Everything you need to know about Python in one place</p>
</div>
</header>
<!-- Main Content -->
<main class="container mx-auto px-4 py-8">
<!-- Search Bar -->
<div class="mb-8">
<div class="relative max-w-2xl mx-auto">
<input type="text" placeholder="Search Python concepts..."
class="w-full px-6 py-3 rounded-lg border border-gray-300 dark:border-gray-700 bg-white dark:bg-gray-800 focus:outline-none focus:ring-2 focus:ring-python-blue focus:border-transparent transition">
<i class="fas fa-search absolute right-6 top-4 text-gray-400"></i>
</div>
</div>
<!-- Categories Grid -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
<!-- Basic Syntax -->
<div class="modal-trigger bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden hover:shadow-lg transition cursor-pointer" data-modal="basic-syntax">
<div class="p-6">
<div class="flex items-center mb-4">
<div class="bg-python-blue text-white p-3 rounded-lg mr-4">
<i class="fas fa-code text-xl"></i>
</div>
<h3 class="text-xl font-bold text-gray-800 dark:text-white">Basic Syntax</h3>
</div>
<p class="text-gray-600 dark:text-gray-300">Variables, comments, print statements, and basic program structure</p>
</div>
</div>
<!-- Data Types -->
<div class="modal-trigger bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden hover:shadow-lg transition cursor-pointer" data-modal="data-types">
<div class="p-6">
<div class="flex items-center mb-4">
<div class="bg-python-blue text-white p-3 rounded-lg mr-4">
<i class="fas fa-cubes text-xl"></i>
</div>
<h3 class="text-xl font-bold text-gray-800 dark:text-white">Data Types</h3>
</div>
<p class="text-gray-600 dark:text-gray-300">Strings, numbers, booleans, type conversion and checking</p>
</div>
</div>
<!-- Control Flow -->
<div class="modal-trigger bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden hover:shadow-lg transition cursor-pointer" data-modal="control-flow">
<div class="p-6">
<div class="flex items-center mb-4">
<div class="bg-python-blue text-white p-3 rounded-lg mr-4">
<i class="fas fa-random text-xl"></i>
</div>
<h3 class="text-xl font-bold text-gray-800 dark:text-white">Control Flow</h3>
</div>
<p class="text-gray-600 dark:text-gray-300">Conditionals, loops, and program flow control</p>
</div>
</div>
<!-- Functions -->
<div class="modal-trigger bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden hover:shadow-lg transition cursor-pointer" data-modal="functions">
<div class="p-6">
<div class="flex items-center mb-4">
<div class="bg-python-blue text-white p-3 rounded-lg mr-4">
<i class="fas fa-cogs text-xl"></i>
</div>
<h3 class="text-xl font-bold text-gray-800 dark:text-white">Functions</h3>
</div>
<p class="text-gray-600 dark:text-gray-300">Defining, calling, parameters, return values, and scope</p>
</div>
</div>
<!-- Data Structures -->
<div class="modal-trigger bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden hover:shadow-lg transition cursor-pointer" data-modal="data-structures">
<div class="p-6">
<div class="flex items-center mb-4">
<div class="bg-python-blue text-white p-3 rounded-lg mr-4">
<i class="fas fa-layer-group text-xl"></i>
</div>
<h3 class="text-xl font-bold text-gray-800 dark:text-white">Data Structures</h3>
</div>
<p class="text-gray-600 dark:text-gray-300">Lists, tuples, dictionaries, sets, and their operations</p>
</div>
</div>
<!-- OOP -->
<div class="modal-trigger bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden hover:shadow-lg transition cursor-pointer" data-modal="oop">
<div class="p-6">
<div class="flex items-center mb-4">
<div class="bg-python-blue text-white p-3 rounded-lg mr-4">
<i class="fas fa-project-diagram text-xl"></i>
</div>
<h3 class="text-xl font-bold text-gray-800 dark:text-white">OOP</h3>
</div>
<p class="text-gray-600 dark:text-gray-300">Classes, objects, inheritance, and special methods</p>
</div>
</div>
<!-- Error Handling -->
<div class="modal-trigger bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden hover:shadow-lg transition cursor-pointer" data-modal="error-handling">
<div class="p-6">
<div class="flex items-center mb-4">
<div class="bg-python-blue text-white p-3 rounded-lg mr-4">
<i class="fas fa-exclamation-triangle text-xl"></i>
</div>
<h3 class="text-xl font-bold text-gray-800 dark:text-white">Error Handling</h3>
</div>
<p class="text-gray-600 dark:text-gray-300">Try/except blocks, raising exceptions, and custom errors</p>
</div>
</div>
<!-- Modules -->
<div class="modal-trigger bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden hover:shadow-lg transition cursor-pointer" data-modal="modules">
<div class="p-6">
<div class="flex items-center mb-4">
<div class="bg-python-blue text-white p-3 rounded-lg mr-4">
<i class="fas fa-boxes text-xl"></i>
</div>
<h3 class="text-xl font-bold text-gray-800 dark:text-white">Modules & Packages</h3>
</div>
<p class="text-gray-600 dark:text-gray-300">Importing, creating, and organizing Python code</p>
</div>
</div>
<!-- File I/O -->
<div class="modal-trigger bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden hover:shadow-lg transition cursor-pointer" data-modal="file-io">
<div class="p-6">
<div class="flex items-center mb-4">
<div class="bg-python-blue text-white p-3 rounded-lg mr-4">
<i class="fas fa-file-alt text-xl"></i>
</div>
<h3 class="text-xl font-bold text-gray-800 dark:text-white">File I/O</h3>
</div>
<p class="text-gray-600 dark:text-gray-300">Reading, writing, and manipulating files</p>
</div>
</div>
<!-- Standard Library -->
<div class="modal-trigger bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden hover:shadow-lg transition cursor-pointer" data-modal="standard-library">
<div class="p-6">
<div class="flex items-center mb-4">
<div class="bg-python-blue text-white p-3 rounded-lg mr-4">
<i class="fas fa-book text-xl"></i>
</div>
<h3 class="text-xl font-bold text-gray-800 dark:text-white">Standard Library</h3>
</div>
<p class="text-gray-600 dark:text-gray-300">Essential modules included with Python</p>
</div>
</div>
<!-- Testing -->
<div class="modal-trigger bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden hover:shadow-lg transition cursor-pointer" data-modal="testing">
<div class="p-6">
<div class="flex items-center mb-4">
<div class="bg-python-blue text-white p-3 rounded-lg mr-4">
<i class="fas fa-vial text-xl"></i>
</div>
<h3 class="text-xl font-bold text-gray-800 dark:text-white">Testing</h3>
</div>
<p class="text-gray-600 dark:text-gray-300">Writing and running tests in Python</p>
</div>
</div>
<!-- Advanced -->
<div class="modal-trigger bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden hover:shadow-lg transition cursor-pointer" data-modal="advanced">
<div class="p-6">
<div class="flex items-center mb-4">
<div class="bg-python-blue text-white p-3 rounded-lg mr-4">
<i class="fas fa-rocket text-xl"></i>
</div>
<h3 class="text-xl font-bold text-gray-800 dark:text-white">Advanced Topics</h3>
</div>
<p class="text-gray-600 dark:text-gray-300">Decorators, generators, async/await, and more</p>
</div>
</div>
</div>
</main>
<!-- Footer -->
<footer class="bg-python-dark text-white py-8">
<div class="container mx-auto px-4">
<div class="flex flex-col md:flex-row justify-between items-center">
<div class="mb-4 md:mb-0">
<h2 class="text-xl font-bold flex items-center">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Python-logo-notext.svg/1869px-Python-logo-notext.svg.png"
alt="Python Logo" class="h-8 w-8 mr-2">
Python Cheatsheet
</h2>
<p class="text-python-yellow mt-2">For Python 3.8+</p>
</div>
<div class="flex space-x-6">
<a href="#" class="hover:text-python-yellow transition">GitHub</a>
<a href="#" class="hover:text-python-yellow transition">Docs</a>
<a href="#" class="hover:text-python-yellow transition">About</a>
</div>
</div>
<div class="mt-8 pt-8 border-t border-gray-700 text-center text-gray-400">
<p>Created with ♥ by femonla02 | MIT License | Last updated: August 2023</p>
</div>
</div>
</footer>
<!-- Modals -->
<!-- Basic Syntax Modal -->
<div id="basic-syntax-modal" class="modal fixed inset-0 z-50 flex items-center justify-center p-4 bg-black bg-opacity-50 modal-hidden">
<div class="bg-white dark:bg-gray-800 rounded-xl shadow-2xl max-w-4xl w-full max-h-[90vh] overflow-y-auto">
<div class="p-6">
<div class="flex justify-between items-center mb-6">
<h3 class="text-2xl font-bold text-gray-800 dark:text-white">
<i class="fas fa-code mr-2 text-python-blue"></i>
Basic Syntax
</h3>
<button class="modal-close p-2 rounded-full hover:bg-gray-200 dark:hover:bg-gray-700 transition">
<i class="fas fa-times text-xl"></i>
</button>
</div>
<div class="space-y-6">
<div>
<h4 class="text-xl font-semibold mb-3 text-gray-700 dark:text-gray-300">Comments</h4>
<div class="bg-gray-100 dark:bg-gray-700 p-4 rounded-lg code-block">
<pre><code class="text-gray-800 dark:text-gray-200"># This is a single-line comment
"""
This is a
multi-line comment
or docstring
"""</code></pre>
</div>
</div>
<div>
<h4 class="text-xl font-semibold mb-3 text-gray-700 dark:text-gray-300">Variables</h4>
<div class="bg-gray-100 dark:bg-gray-700 p-4 rounded-lg code-block">
<pre><code class="text-gray-800 dark:text-gray-200"># Variable assignment
name = "Alice"
age = 30
is_active = True
# Multiple assignment
x, y, z = 1, 2, 3
# Type hints (Python 3.5+)
def greet(name: str) -> str:
return f"Hello, {name}"</code></pre>
</div>
</div>
<div>
<h4 class="text-xl font-semibold mb-3 text-gray-700 dark:text-gray-300">Print Statements</h4>
<div class="bg-gray-100 dark:bg-gray-700 p-4 rounded-lg code-block">
<pre><code class="text-gray-800 dark:text-gray-200"># Basic print
print("Hello, World!")
# Print multiple values
print("Name:", name, "Age:", age)
# Formatted strings (f-strings, Python 3.6+)
print(f"{name} is {age} years old")
# Debug print (Python 3.8+)
print(f"{name=} {age=}") # Output: name='Alice' age=30</code></pre>
</div>
</div>
<div>
<h4 class="text-xl font-semibold mb-3 text-gray-700 dark:text-gray-300">Indentation</h4>
<div class="bg-gray-100 dark:bg-gray-700 p-4 rounded-lg code-block">
<pre><code class="text-gray-800 dark:text-gray-200"># Python uses indentation to define blocks
if age > 18:
print("Adult") # This is indented
print("You can vote") # Same block
else:
print("Minor") # Different block
# Standard is 4 spaces (PEP 8 recommendation)
# Never mix tabs and spaces!</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Data Types Modal -->
<div id="data-types-modal" class="modal fixed inset-0 z-50 flex items-center justify-center p-4 bg-black bg-opacity-50 modal-hidden">
<div class="bg-white dark:bg-gray-800 rounded-xl shadow-2xl max-w-4xl w-full max-h-[90vh] overflow-y-auto">
<div class="p-6">
<div class="flex justify-between items-center mb-6">
<h3 class="text-2xl font-bold text-gray-800 dark:text-white">
<i class="fas fa-cubes mr-2 text-python-blue"></i>
Data Types
</h3>
<button class="modal-close p-2 rounded-full hover:bg-gray-200 dark:hover:bg-gray-700 transition">
<i class="fas fa-times text-xl"></i>
</button>
</div>
<div class="space-y-6">
<div>
<h4 class="text-xl font-semibold mb-3 text-gray-700 dark:text-gray-300">Basic Types</h4>
<div class="bg-gray-100 dark:bg-gray-700 p-4 rounded-lg code-block">
<pre><code class="text-gray-800 dark:text-gray-200"># Strings
name = "Alice" # or 'Alice'
multiline = """Line 1
Line 2"""
# Numbers
age = 30 # Integer
price = 19.99 # Float
complex_num = 3 + 4j # Complex
# Boolean
is_active = True
is_admin = False
# None
result = None</code></pre>
</div>
</div>
<div>
<h4 class="text-xl font-semibold mb-3 text-gray-700 dark:text-gray-300">Type Conversion</h4>
<div class="bg-gray-100 dark:bg-gray-700 p-4 rounded-lg code-block">
<pre><code class="text-gray-800 dark:text-gray-200"># Convert between types
int("10") # 10 (string to int)
float("3.14") # 3.14 (string to float)
str(42) # "42" (int to string)
bool(1) # True (int to bool)
bool("") # False (empty string to bool)
list("abc") # ['a', 'b', 'c'] (string to list)
tuple([1,2,3]) # (1, 2, 3) (list to tuple)</code></pre>
</div>
</div>
<div>
<h4 class="text-xl font-semibold mb-3 text-gray-700 dark:text-gray-300">Type Checking</h4>
<div class="bg-gray-100 dark:bg-gray-700 p-4 rounded-lg code-block">
<pre><code class="text-gray-800 dark:text-gray-200"># Check types
type("hello") is str # True
isinstance(42, int) # True
# Python 3.10+ pattern matching
match x:
case int():
print("It's an integer")
case str():
print("It's a string")
case _:
print("Unknown type")</code></pre>
</div>
</div>
<div>
<h4 class="text-xl font-semibold mb-3 text-gray-700 dark:text-gray-300">Bytes and Encoding</h4>
<div class="bg-gray-100 dark:bg-gray-700 p-4 rounded-lg code-block">
<pre><code class="text-gray-800 dark:text-gray-200"># Bytes
data = b"binary data"
# Encoding/decoding
text = "Hello"
encoded = text.encode('utf-8') # b'Hello'
decoded = encoded.decode('utf-8') # 'Hello'</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- All other modals would follow the same pattern -->
<!-- For brevity, I'm showing just 2 modals, but you would include all sections -->
<!-- JavaScript -->
<script>
// Theme toggle
const themeToggle = document.getElementById('theme-toggle');
themeToggle.addEventListener('click', () => {
document.documentElement.classList.toggle('dark');
localStorage.setItem('theme', document.documentElement.classList.contains('dark') ? 'dark' : 'light');
});
// Check for saved theme preference
if (localStorage.getItem('theme') === 'dark' ||
(!localStorage.getItem('theme') && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.classList.add('dark');
}
// Modal functionality
document.querySelectorAll('.modal-trigger').forEach(trigger => {
trigger.addEventListener('click', () => {
const modalId = trigger.getAttribute('data-modal') + '-modal';
document.getElementById(modalId).classList.remove('modal-hidden');
});
});
document.querySelectorAll('.modal-close').forEach(closeBtn => {
closeBtn.addEventListener('click', () => {
closeBtn.closest('.modal').classList.add('modal-hidden');
});
});
// Close modal when clicking outside
document.querySelectorAll('.modal').forEach(modal => {
modal.addEventListener('click', (e) => {
if (e.target === modal) {
modal.classList.add('modal-hidden');
}
});
});
// Close with Escape key
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
document.querySelectorAll('.modal').forEach(modal => {
modal.classList.add('modal-hidden');
});
}
});
</script>
</body>
</html>