-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPython.py
314 lines (183 loc) · 5.65 KB
/
Python.py
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
# PYTHON - ... - by Beumsk
# To create a comment. Shown in code only. Indent 2spaces
# /* Multiple line comment */
# Python project structure
# my-python-project/
# ├── venv/ # Virtual environment
# ├── src/ # Source code
# │ ├── __init__.py # Package initializer
# │ ├── app.py # Entry point
# │ ├── utils.py # Utility module
# ├── requirements.txt # Dependency list
# Print
print("Hello world!")
# Variables
name = "John" # string
age = 25 # int
grade = 7.8 # float
isAdult = true # boolean
undefined = None # None
# Lists
list1 = [1, 2, 3]
list1.append(4) # Adds 4 to the end
list1.insert(1, 10) # Inserts 10 at index 1
list1.remove(2) # Removes the first occurrence of 2
print(list1) # Output: [1, 10, 3, 4]
# Tuples, immutable version of list
tuple1 = (1, "two") # cannot be modified
# Sets, list of unique elements
fruits = {1, 2, 3}
fruits.add(4) # Adds 4 to the set
fruits.discard(2) # Removes 2 from the set
print(fruits) # Output: {1, 3, 4}
# Dicts, dictionaries
dict1 = {"name": "John", "age": 25}
dict1["city"] = "Brussels" # Adding a new key-value pair
print(dict1["name"]) # Output: John
del dict1["age"] # Removing a key-value pair
print(dict1) # Output: {"name": "John", "city": "Brusselss"}
# Comparison
25 == 25 # true
# Conditionals
if age > 18:
print("Adult")
else:
print("Minor")
# For loop
for i in range(5):
print(i)
# While loop
count = 0
while count < 5:
print(count)
count += 1
# JSON
import json
dict1 = {"name": "John", "age": 25}
dict1JSON = json.dumps(dict1) # Convert dictionary to JSON string
print(dict1JSON) # Output: {"name": "John", "age": 25}
parsedJSON = json.loads(dict1JSON) # Parse JSON string to dictionary
print(parsedJSON) # Output: {'name': 'John', 'age': 25}
# Functions
def greet(name):
return f"Hello, {name}!"
print(greet("John")) # Output: Hello, John!
# Lambda
square = lambda x: x ** 2
print(square(5)) # Output: 25
# Using lambda in a map function
numbers = [1, 2, 3, 4]
squared = map(lambda x: x ** 2, numbers)
print(list(squared)) # Output: [1, 4, 9, 16]
# Function parameters
def greet(name="World", *args, **kwargs):
print(f"Hello, {name}!")
print("Arguments:", args)
print("Keyword Arguments:", kwargs)
greet("John", 1, 2, color="blue", age=30)
# Hello, John!
# Arguments: (1, 2)
# Keyword Arguments: {'color': 'blue', 'age': 30}
# Scope: LEGB rule
# Local: Variables defined inside a function.
# Enclosing: Variables in the nearest enclosing scope (for example, nested functions).
# Global: Variables defined at the top level of the module.
# Built-in: Predefined names in Python (for example, len, print).
x = "global"
def outer_function():
x = "enclosing"
def inner_function():
x = "local"
print(x)
inner_function()
outer_function() # Output: local
print(x) # Output: global
# Classes
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return f"{self.name} makes a sound."
class Dog(Animal):
def speak(self):
return f"{self.name} barks."
# Using the classes
generic_animal = Animal("Generic Animal")
dog = Dog("Buddy")
print(generic_animal.speak()) # Output: Generic Animal makes a sound.
print(dog.speak()) # Output: Buddy barks.
# Polymorphism
class Bird:
def fly(self):
return "Birds can fly."
class Penguin(Bird):
def fly(self):
return "Penguins cannot fly."
def get_flight_ability(bird):
print(bird.fly())
sparrow = Bird()
penguin = Penguin()
get_flight_ability(sparrow) # Output: Birds can fly.
get_flight_ability(penguin) # Output: Penguins cannot fly.
# Prototypes
class Calculator:
def add(self, a, b):
return a + b
def multiply(self, a, b):
return a * b
calc = Calculator()
print(calc.add(5, 3)) # Output: 8
print(calc.multiply(5, 3)) # Output: 15
# Export (utils.py) & Import (main.py)
def add(a, b):
return a + b
def multiply(a, b):
return a * b
from utils import add, multiply
print(add(2, 3)) # Output: 5
print(multiply(2, 3)) # Output: 6
# Package manager: pip (python installer package)
# pip install requests # or any other package
import requests
response = requests.get("https://api.example.com/data")
print(response.json())
# bashCopy codepip install -r requirements.txt # usually listing dependencies in that file
# Virtual environments (isolate dependencies)
# python -m venv myenv # Creating a Virtual Environment
# myenv\Scripts\activate # Activating the Virtual Environment for windows
# source myenv/bin/activate # Activating the Virtual Environment for macOS/Linux
# pip install flask # Installing Libraries in the Virtual Environment
# deactivate # Deactivating the Virtual Environment
# Exception handling
try:
result = 10 / 0
except ZeroDivisionError as e:
print(f"Error: {e}")
else:
print("No errors occurred!")
finally:
print("Execution complete.")
# Output:
# Error: division by zero
# Execution complete.
# Common errors
# SyntaxError: Occurs when code violates Python's syntax rules.
print("Hello World" # Missing closing parenthesis
# TypeError: Raised when an operation is applied to an object of inappropriate type.
print("Hello" + 5) # Cannot concatenate str and int
# ValueError: Raised when a function receives an argument of the correct type but invalid value.
int("abc") # Cannot convert string to int
# Web development (backend)
# with Django or Flask
# Testing
# with pytest
# Scraping
# with BeautifulSoup
# REST API
# with Flask
# Automation
# with os & shutil
# Data visiualisation
# with pandas & matplotlib
# Machine learning
# with tensorflow