Text Processing & Cleaning
Basic Program in Python
1. Variables and Data Types
name = "Alice"age = 25height = 5.4is_student = Trueprint(name, age, height, is_student)2. Conditional Statements
x = 15if x > 10: print("Greater than 10")else: print("10 or below")3. Loops
for i in range(5): print("Count:", i)4. Functions and Arguments
def greet(name): print("Hello,", name)
greet("John")5. List Comprehensions
squares = [n*n for n in range(5)]print(squares)6. Dictionary Comprehensions
numbers = {x: x*2 for x in range(3)}print(numbers)7. String Manipulation
text = "python programming"print(text.capitalize())print(text.replace("python", "advanced python"))8. Error Handling (try/except)
try: print(10 / 0)except ZeroDivisionError: print("Cannot divide by zero!")9. Custom Exceptions
class AgeError(Exception): pass
age = -1if age < 0: raise AgeError("Age cannot be negative!")10. File Handling
with open("sample.txt", "w") as file: file.write("Hello File!")
with open("sample.txt", "r") as file: print(file.read())11. Object-Oriented Programming
class Dog: def __init__(self, name): self.name = name
def bark(self): print(self.name, "says woof!")
Dog("Buddy").bark()12. Modules and Packages
import mathprint(math.sqrt(25))13. Virtual Environment (conceptual code)
python -m venv myenvsource myenv/bin/activate14. Python Standard Library (random)
import randomprint(random.randint(1, 10))15. Lambda Functions
square = lambda x: x*xprint(square(6))16. Map, Filter, Reduce
from functools import reduce
nums = [1, 2, 3, 4]
doubled = list(map(lambda x: x*2, nums))filtered = list(filter(lambda x: x % 2 == 0, nums))summed = reduce(lambda a, b: a+b, nums)
print(doubled, filtered, summed)17. Iterators and Generators
def counter(): n = 1 while n <= 3: yield n n += 1
for num in counter(): print(num)18. Decorators
def announce(func): def wrapper(): print("Starting function") func() print("Function finished") return wrapper
@announcedef hello(): print("Hello!")
hello()19. Context Managers
with open("note.txt", "w") as f: f.write("Context Manager Example")20. Unit Testing (simple demo)
def add(a, b): return a + b
assert add(2, 3) == 5print("Test passed!")21. Type Hinting
def multiply(a: int, b: int) -> int: return a * b
print(multiply(3, 4))22. Logging
import logging
logging.basicConfig(level=logging.INFO)logging.info("This is an info message")23. Working With APIs
import requests
response = requests.get("https://jsonplaceholder.typicode.com/todos/1")print(response.json())24. Data Structures (Stack Example)
stack = []stack.append(10)stack.append(20)print(stack.pop()) # removes 2025. Algorithms (Binary Search)
def binary_search(arr, target): low, high = 0, len(arr)-1 while low <= high: mid = (low+high)//2 if arr[mid] == target: return mid elif arr[mid] < target: low = mid + 1 else: high = mid - 1
print(binary_search([1,2,3,4,5], 4))26. Multithreading
import threading
def task(): print("Running task...")
t = threading.Thread(target=task)t.start()t.join()27. JSON Handling
import json
data = {"name": "Alice", "age": 30}json_str = json.dumps(data)print(json_str)print(json.loads(json_str))28. Database Connectivity (SQLite)
import sqlite3
conn = sqlite3.connect(":memory:")cursor = conn.cursor()
cursor.execute("CREATE TABLE users(name TEXT)")cursor.execute("INSERT INTO users VALUES ('John')")
conn.commit()print(cursor.execute("SELECT * FROM users").fetchall())29. Basic Flask Web App
from flask import Flaskapp = Flask(__name__)
@app.route("/")def home(): return "Hello Flask!"
app.run(debug=True)30. Code Style & Best Practices
def add_numbers(a: int, b: int) -> int: """Return the sum of two numbers.""" return a + b
print(add_numbers(5, 7))