Python Modules
A file with reusable Python code
(functions, classes, variables).
Example: custom
.py
files orsys, math
module
Python Package
A
folder with related modules
, including an__init__.py
file.Example:
numpy
or custom module directories.pip
is a tool in Python toinstall & manage Python packages
or libraries from the Python Package Index(PyPI)
.
Python Virtual Environment
Isolated environment
for project-specific dependencies.Create:
python -m venv <env_name>
Activate:
source <env_name>/bin/activate
Command Line Args
Python in build sys module
import sys # python inbuild sys module, which is used for command line args
num1 = float(sys.argv[1])
Environment Variables
Env vars used for sensitive data, which we can’t hardcoded:
API keys
passwords
tokens
certificates
Declare Env vars in terminal:
export password=”pass@123”
Code:
import os
pass = os.getenv("password")
File Operation of Windows
Open:
open()
with modes (r
,w
, etc.), e.g.,open("file.txt", "r")
.Read: Use
read()
,readline()
, orreadlines()
to fetch content.Write: Use
write()
orwritelines()
in modes likew
ora
.Close: Use
close()
orwith
for auto-closing.
Module
Requests
The requests
module in Python simplifies HTTP requests to interact with web servers.
Purpose: Send HTTP methods (GET, POST, etc.) and handle responses via
API
.Features: Manage headers, cookies, auth, and work with
JSON or text
.Install:
pip install requests
import requests
response = requests.get("https://example.com")
print(response.status_code) # Status code
print(response.text) # Response body
Boto3
AWS SDK for Python
to interact programmatically with AWS services.Ideal for
automating AWS workflows
and managing resources efficientlyBuilding serverless applications
with services like Lambda and DynamoDB.
import boto3
# Initialize S3 client
s3 = boto3.client('s3')
# Upload a file to S3
s3.upload_file('local_file.txt', 'my-bucket', 'remote_file.txt')
Flask
It’s a lightweight web framework
in Python used to build web applications
with added functionality.
Decorators:
It’s a
special function
in Python used to modify the behaviour of another function.It written
above function
with@
symbol.
from flask import Flask
app = Flask(__name__)
@app.route("/greet") # Flask decorator that connects a URL('/greet') route to a function
def greet():
return "Greetings from Flask!"
if __name__ == "__main__":
app.run()