-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_cheat_sheet.py
More file actions
183 lines (170 loc) · 3.77 KB
/
python_cheat_sheet.py
File metadata and controls
183 lines (170 loc) · 3.77 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
# Create a new PDF with more comprehensive examples
from fpdf import FPDF
pdf = FPDF()
pdf.set_auto_page_break(auto=True, margin=15)
pdf.add_page()
pdf.set_font("Arial", size=12)
# Title
pdf.set_font("Arial", size=16, style="B")
pdf.cell(0, 10, "Comprehensive Python Cheat Sheet with Examples", ln=True, align="C")
pdf.ln(10)
# Add Sections with Examples
sections_with_examples = {
"1. Variables and Data Types": """
- Integer: x = 10
- Float: pi = 3.14
- String: greeting = "Hello, World!"
- Boolean: is_active = True
- List: colors = ["red", "green", "blue"]
- Tuple: point = (5, 10)
- Dictionary: student = {"name": "Alice", "age": 25}
- Set: unique_numbers = {1, 2, 3, 4}
""",
"Example:":
"""
x = 10
greeting = "Hello, World!"
colors = ["red", "green", "blue"]
print(f"Integer: {x}, Greeting: {greeting}, Colors: {colors}")
""",
"2. Arithmetic and Logical Operations": """
- Addition: a + b
- Subtraction: a - b
- Multiplication: a * b
- Division: a / b
- Floor Division: a // b
- Modulus: a % b
- Exponentiation: a ** b
- Logical: and, or, not
""",
"Example:":
"""
a, b = 10, 3
print(f"Sum: {a + b}, Product: {a * b}, Power: {a ** b}")
if a > b and b > 0:
print("Both conditions are True")
""",
"3. Conditional Statements": """
if condition:
# Code block
elif another_condition:
# Another block
else:
# Default block
""",
"Example:":
"""
x = 5
if x > 10:
print("x is greater than 10")
elif x == 5:
print("x is 5")
else:
print("x is less than 10")
""",
"4. Loops": """
- For Loop:
for item in iterable:
# Code block
- While Loop:
while condition:
# Code block
""",
"Example:":
"""
# For Loop
for i in range(5):
print(f"Iteration {i}")
# While Loop
count = 0
while count < 5:
print(f"Count: {count}")
count += 1
""",
"5. Functions": """
def function_name(parameters):
# Code block
return value
""",
"Example:":
"""
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
""",
"6. Classes and Objects": """
class MyClass:
def __init__(self, name):
self.name = name
def greet(self):
return f"Hello, {self.name}"
obj = MyClass("Python")
print(obj.greet())
""",
"7. File Handling": """
- Reading a File:
with open("file.txt", "r") as file:
content = file.read()
- Writing to a File:
with open("file.txt", "w") as file:
file.write("Hello, World!")
""",
"Example:":
"""
with open("example.txt", "w") as file:
file.write("This is a test.")
with open("example.txt", "r") as file:
print(file.read())
""",
"8. Common Libraries": """
- NumPy: import numpy as np
- Pandas: import pandas as pd
- Matplotlib: import matplotlib.pyplot as plt
- Requests: import requests
""",
"Example:":
"""
import numpy as np
arr = np.array([1, 2, 3])
print(f"Numpy Array: {arr}")
""",
"9. List Comprehensions": """
- Syntax: [expression for item in iterable if condition]
""",
"Example:":
"""
squares = [x**2 for x in range(10)]
print(f"Squares: {squares}")
""",
"10. Error Handling": """
try:
# Code block that may raise an exception
except Exception as e:
print(f"Error: {e}")
finally:
print("Cleanup code")
""",
"Example:":
"""
try:
result = 10 / 0
except ZeroDivisionError as e:
print(f"Error: {e}")
finally:
print("Execution complete.")
"""
}
# Add sections and examples to PDF
for section, content in sections_with_examples.items():
if section.endswith("Example:"):
pdf.set_font("Arial", style="I", size=11)
else:
pdf.set_font("Arial", style="B", size=12)
pdf.cell(0, 10, section, ln=True)
pdf.set_font("Arial", size=11)
pdf.multi_cell(0, 10, content)
pdf.ln(5)
# Save PDF with Examples
file_path_with_examples = "/mnt/data/Comprehensive_Python_Cheat_Sheet.pdf"
pdf.output(file_path_with_examples)
file_path_with_examples