Python Programming Important Notes
Python Programming Important Notes
1. Context Managers
Explanation:
Code Example:
@contextmanager
def manage_file(filename):
file = open(filename, 'r')
yield file
file.close()
2. Metaclasses
Explanation:
Code Example:
class Meta(type):
def __new__(cls, name, bases, dct):
print(f"Creating class {name}")
return super().__new__(cls, name, bases, dct)
class MyClass(metaclass=Meta):
pass
Explanation:
Code Example:
def decorator_one(func):
def wrapper():
print("Decorator One")
func()
return wrapper
def decorator_two(func):
def wrapper():
print("Decorator Two")
func()
return wrapper
@decorator_one
@decorator_two
def my_function():
print("Original Function")
my_function()
4. Function Annotations
Explanation:
Code Example:
Explanation:
Code Example:
print(multiply(3)) # Output: 9
print(multiply(3, 4)) # Output: 12
Explanation:
Abstract Base Classes are used to define a common interface for derived
classes. They allow you to define methods that must be implemented in
any child class.
Code Example:
class MyABC(ABC):
@abstractmethod
def my_abstract_method(self):
pass
class ConcreteClass(MyABC):
def my_abstract_method(self):
print("Implemented abstract method")
obj = ConcreteClass()
obj.my_abstract_method() # Output: Implemented abstract method
Explanation:
Code Example:
import importlib
math_module = importlib.import_module('math')
sqrt = math_module.sqrt
print(sqrt(16)) # Output: 4.0
Explanation:
Code Example:
# Iterator
class MyIterator:
def __iter__(self):
self.num = 1
return self
def __next__(self):
self.num *= 2
return self.num
# Generator
def my_generator():
num = 1
while True:
num *= 2
yield num
print(next(iter(iter_obj))) # Output: 2
print(next(gen_obj)) # Output: 2
9. Coroutines
Explanation:
Code Example:
import asyncio
asyncio.run(my_coroutine())
Explanation:
Code Example:
import aiofiles
Explanation:
Code Example:
class AsyncCounter:
def __init__(self, limit):
self.limit = limit
def __aiter__(self):
self.count = 0
return self
Explanation:
Code Example:
import asyncio
asyncio.run(print_numbers())
13. Concurrent.Futures
Explanation:
Code Example:
def task(n):
return n * 2
14. ThreadPoolExecutor
Explanation:
Code Example:
def square(n):
return n * n
Explanation:
Code Example:
def multiply(n):
return n * 2
Explanation:
Code Example:
import gc
Explanation:
Code Example:
import cProfile
def factorial(n):
return 1 if n == 0 else n * factorial(n-1)
def main():
print(factorial(10))
cProfile.run('main()')
18. Cython
Explanation:
Code Example:
# my_cython_module.pyx
def multiply(a, b):
return a * b
# Usage in Python
import my_cython_module
print(my_cython_module.multiply(3, 4)) # Output: 12
Explanation:
Code Example:
Explanation:
The Global Interpreter Lock (GIL) is a mutex that allows only one
thread to execute in the interpreter at a time. This can limit the
performance of CPU-bound multithreaded applications.
Code Example:
def count_up_to(n):
count = 0
while count < n:
count += 1
21. C Extensions
Explanation:
Code Example:
// example.c
#include <Python.h>
PyMODINIT_FUNC PyInit_example(void) {
return PyModule_Create(&examplemodule);
}
# Compiling
gcc -shared -o example.so -I /usr/include/python3.8 example.c
# Usage in Python
import example
print(example.multiply(2, 3)) # Output: 6
Explanation:
Code Example:
23. NumPy
Explanation:
Code Example:
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# Element-wise addition
result = a + b
print(result) # Output: [5 7 9]
24. SciPy
Explanation:
Code Example:
def objective_function(x):
result = minimize(objective_function, 0)
print(result.x) # Output: Approximation of the minimum
25. pandas
Explanation:
Code Example:
import pandas as pd
data = {
'Name': ['Alice', 'Bob'],
'Age': [25, 30]
}
df = pd.DataFrame(data)
# Filtering by age
filtered_df = df[df['Age'] > 26]
print(filtered_df) # Output: Data for Bob
26. Matplotlib
Explanation:
Code Example:
x = [1, 2, 3]
y = [4, 6, 8]
plt.plot(x, y)
plt.xlabel('X Label')
plt.ylabel('Y Label')
27. Seaborn
Explanation:
Code Example:
tips = sns.load_dataset('tips')
sns.boxplot(x='day', y='total_bill', data=tips)
plt.show()
28. Plotly
Explanation:
Code Example:
import plotly.express as px
iris = px.data.iris()
fig = px.scatter(iris, x="sepal_width", y="sepal_length", color="species")
fig.show()
29. Bokeh
Explanation:
Code Example:
30. scikit-learn
Explanation:
Code Example:
prediction = model.predict([[4]])
print(prediction) # Output: Approximation of 8
31. TensorFlow
Explanation:
Code Example:
import tensorflow as tf
32. PyTorch
Explanation:
Code Example:
import torch
import torch.nn as nn
# Training loop
for t in range(100):
y_pred = model(x)
loss = loss_fn(y_pred, y)
33. Keras
Explanation:
Code Example:
Explanation:
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D((2, 2)),
Flatten(),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')
Explanation:
# First: train only the top layers (which were randomly initialized)
for layer in base_model.layers:
layer.trainable = False
model.compile(optimizer='adam', loss='binary_crossentropy')
Explanation:
import numpy as np
# Q-table initialization
Q = np.zeros((3, 2))
# Hyperparameters
alpha = 0.1
gamma = 0.9
# Training loop
for episode in range(1000):
state = 0
done = False
while not done:
action = np.random.choice([0, 1])
next_state = transition_matrix[state][action]
reward = 1 if next_state == 2 else 0
Q[state, action] = (1 - alpha) * Q[state, action] + alpha * (reward +
gamma * np.max(Q[next_state]))
state = next_state
if state == 2:
done = True
import nltk
Explanation:
Code Example:
import re
Explanation:
def square_number(n):
return n * n
numbers = [1, 2, 3, 4]
with Pool() as pool:
results = pool.map(square_number, numbers)
print(results) # Output: [1, 4, 9, 16]
Explanation:
Explanation:
url = "http://example.com"
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")
Explanation:
import requests
response = requests.get("https://jsonplaceholder.typicode.com/todos/1")
data = response.json()
print(data['title']) # Output: 'delectus aut autem'
43. GraphQL
Explanation:
client = GraphQLClient('https://api.graph.cool/simple/v1/swapi')
result = client.execute('''
{
allPersons {
name
}
}
''')
print(result)
Explanation:
FROM python:3.8
WORKDIR /app
COPY . /app
45. Kubernetes
Explanation:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image
Explanation:
import json
Explanation:
Web Development Frameworks like Flask and Django help in building web
applications by providing reusable code patterns.
app = Flask(__name__)
@app.route('/')
def home():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
app = Flask(__name__)
@app.route('/users/<int:user_id>', methods=['GET'])
def get_user(user_id):
user = {'id': user_id, 'name': 'Alice'} # Example data
return jsonify(user)
if __name__ == '__main__':
app.run()
Explanation:
class Query(graphene.ObjectType):
hello = graphene.String()
app = Flask(__name__)
app.add_url_rule('/graphql', view_func=GraphQLView.as_view('graphql',
schema=graphene.Schema(query=Query)))
Explanation:
app = Flask(__name__)
socketio = SocketIO(app)
@app.route('/')
def main():
return render_template('index.html')
@socketio.on('message')
def handle_message(msg):
print('Received message:', msg)
if __name__ == '__main__':
socketio.run(app)
Explanation:
app = Flask(__name__)
oauth = OAuth(app)
@app.route('/')
def index():
return 'Welcome to the OAuth Example'
@app.route('/login')
def login():
return google.authorize(callback=url_for('authorized', _external=True))
@app.route('/logout')
def logout():
session.pop('google_token')
return redirect(url_for('index'))
@app.route('/login/authorized')
def authorized():
response = google.authorized_response()
if response is None or response.get('access_token') is None:
return 'Access denied'
session['google_token'] = (response['access_token'], '')
return 'Logged in'
if __name__ == '__main__':
app.run()
app = Flask(__name__)
app.config['SECRET_KEY'] = 'super-secret'
def identity(payload):
return {'id': 1, 'username': 'user'}
@app.route('/protected')
@jwt_required()
def protected():
return jsonify({'message': 'This is a protected view.'})
if __name__ == '__main__':
app.run()
53. SQLAlchemy
Explanation:
Code Example:
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
engine = create_engine('sqlite:///users.db')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
new_user = User(name='Alice')
session.add(new_user)
session.commit()
Explanation:
55. Multithreading
By: Waleed Mousa
Explanation:
import threading
def print_numbers():
for i in range(10):
print(i)
thread1 = threading.Thread(target=print_numbers)
thread2 = threading.Thread(target=print_numbers)
thread1.start()
thread2.start()
thread1.join()
thread2.join()
56. Multiprocessing
Explanation:
def print_numbers():
for i in range(10):
print(i)
process1 = Process(target=print_numbers)
process2 = Process(target=print_numbers)
process1.start()
process2.start()
57. Decorators
Explanation:
Code Example:
def my_decorator(func):
def wrapper():
print("Something is happening before the function is called.")
func()
print("Something is happening after the function is called.")
return wrapper
@my_decorator
def say_hello():
print("Hello!")
say_hello()
58. Logging
Explanation:
Code Example:
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
Explanation:
pip is the package installer for Python. It allows you to install and
manage additional packages that are not part of the Python standard
library.
Code Example:
Explanation:
Introduced in Python 3.8, the walrus operator (:=) allows you to assign
a value to a variable as part of an expression.
Code Example:
Explanation:
The dis module in Python allows you to disassemble Python byte code,
which can be useful for understanding low-level details.
Code Example:
import dis
def my_function():
a = 10
b = 20
return a + b
dis.dis(my_function)
Explanation:
Code Example:
@lru_cache
def fibonacci(n):
if n < 2:
return n
return fibonacci(n-1) + fibonacci(n-2)
Explanation:
An else clause after a loop (for/while) will run if the loop finishes
normally (i.e., without encountering a break statement).
Code Example:
for i in range(3):
print(i)
else:
print("Loop finished without break.")
Explanation:
You can use the asterisk (*) to unpack lists/tuples and the double
asterisk (**) to unpack dictionaries when calling a function.
Code Example:
Explanation:
The Abstract Syntax Tree (ast) module in Python allows you to interact
with Python source code programmatically, such as analyzing, modifying,
or even generating code.
Code Example:
import ast
66. Contextlib.suppress
Explanation:
Code Example:
with suppress(ZeroDivisionError):
result = 1 / 0
print("Code continues without raising an exception.")
Explanation:
Code Example:
Explanation:
The array module defines a sequence data structure that looks like a
list but contains elements of the same type stored in a more
memory-efficient way.
Code Example:
Explanation:
Code Example:
def new_function(self):
print("New functionality added.")
SomeClass.new_function = new_function
Explanation:
Using yield from, you can delegate part of its operations to another
generator, simplifying the code.
Code Example:
Explanation:
Code Example:
import shutil
shutil.copyfile('source.txt', 'destination.txt')
72. collections.Counter
Explanation:
Code Example:
Explanation:
path = Path('folder/file.txt')
print(path.exists())
74. Descriptors
Explanation:
Code Example:
class Descriptor:
def __get__(self, instance, owner):
print("Getting the attribute")
class MyClass:
attribute = Descriptor()
Explanation:
Code Example:
import heapq
heap = [3, 1, 4, 1, 5]
heapq.heapify(heap)
print(heapq.heappop(heap)) # Output: 1
Explanation:
In the REPL (interactive shell), you can use _ to access the result of
the last expression.
>>> 5 + 2
7
>>> _ + 3
10
Explanation:
Code Example:
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
Explanation:
Code Example:
Explanation:
The queue module provides a way to create thread-safe queues that are
used to safely communicate between threads.
Code Example:
q = Queue()
q.put('item')
item = q.get()
Explanation:
Code Example:
@dataclass
class Point:
x: float
y: float
Explanation:
Code Example:
def factorial(n):
if n == 1:
return 1
return n * factorial(n-1)
Explanation:
Explanation:
Code Example:
def my_function():
x = 10
print(locals())
Explanation:
Code Example:
def my_coroutine():
while True:
received = yield
print('Received:', received)
coro = my_coroutine()
next(coro)
coro.send(42) # Output: Received: 42
Explanation:
Code Example:
87. unittest.mock
Explanation:
Code Example:
mock = MagicMock(return_value=42)
print(mock()) # Output: 42
Explanation:
Code Example:
import threading
def print_numbers():
for i in range(5):
print(i)
Explanation:
Code Example:
class MyClass:
__slots__ = ['name', 'age']
def __init__(self, name, age):
self.name = name
self.age = age
Explanation:
Code Example:
x = 5 # Binary: 101
y = 3 # Binary: 011
print(x & y) # Output: 1 (Binary: 001)
Explanation:
These modules provide tools for working with compressed files using the
bzip2 and gzip file formats, respectively.
Code Example:
import bz2
Explanation:
memoryview gives you shared memory access to data without copying it,
allowing efficient manipulation of large data sets.
Code Example:
Explanation:
In Python 3, you can use the from keyword to chain exceptions, which
helps in understanding the cause-effect relationship between
exceptions.
Code Example:
try:
int("not_a_number")
except ValueError as e:
raise TypeError("Something went wrong") from e
Explanation:
The warnings module allows you to warn users about changes in your code
or deprecations.
Code Example:
import warnings
def old_function():
warnings.warn("This function is deprecated", DeprecationWarning)
Explanation:
Code Example:
Explanation:
Code Example:
x = 0.75
print(x.as_integer_ratio()) # Output: (3, 4)
Explanation:
Code Example:
import cmd
class MyShell(cmd.Cmd):
def do_greet(self, line):
print("Hello, World!")
shell = MyShell()
shell.cmdloop()
Explanation:
Code Example:
class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
Explanation:
The codecs module provides functions for encoding and decoding data,
including handling various text encodings.
Code Example:
import codecs
Explanation:
The if __name__ == "__main__": idiom allows you to write code that can
be both used by other programs via import and run standalone.
def main():
print("This script is running standalone")
if __name__ == "__main__":
main()
Explanation:
Code Example:
import inspect
def my_function():
pass