Python Overview

Python Modules
A file with reusable Python code
(functions, classes, variables).Example: custom
.pyfiles orsys, mathmodule
Python Package
A
folder with related modules, including an__init__.pyfile.Example:
numpyor custom module directories.pipis a tool in Python toinstall & manage Python packagesor libraries from the Python Package Index(PyPI).
Python Virtual Environment
Isolated environmentfor 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 likewora.Close: Use
close()orwithfor 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 Pythonto interact programmatically with AWS services.Ideal for
automating AWS workflowsand managing resources efficientlyBuilding serverless applicationswith 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 functionin Python used to modify the behaviour of another function.It written
above functionwith@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()
Constructor
It’s special method in a class that runs automatically when you create an object.
__init__()
# __name__ special build-in variable
__name__ = __main__ # Runs code only when file is executed directly



