Mastering Python: From Beginner to Advanced**
---
#### **Week 1: Python Fundamentals**
**Chapter 1: Introduction to Python**
- **1.1 What is Python?**
- History and Features
- Installing Python
- Setting Up a Development Environment
- **1.2 Your First Python Program**
- Writing "Hello, World!"
- Running Python Scripts
- Using the Python Interpreter
**Chapter 2: Basic Syntax and Operations**
- **2.1 Variables and Data Types**
- Integers, Floats, Strings, Booleans
- Type Conversion
- **2.2 Basic Operators**
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
**Chapter 3: Control Flow**
- **3.1 Conditional Statements**
- if, elif, else
- Nested Conditions
- **3.2 Loops**
- for Loop
- while Loop
- break and continue
- **3.3 List Comprehensions**
**Chapter 4: Functions and Modules**
- **4.1 Defining Functions**
- Parameters and Return Values
- Default and Keyword Arguments
- **4.2 Modules and Packages**
- Importing Modules
- Standard Library Overview
**Chapter 5: Data Structures**
- **5.1 Lists**
- Creating, Accessing, and Modifying Lists
- List Methods
- **5.2 Tuples**
- Characteristics and Uses
- **5.3 Sets**
- Set Operations
- **5.4 Dictionaries**
- Key-Value Pairs
- Dictionary Methods
**Chapter 6: File I/O and Exception Handling**
- **6.1 Reading and Writing Files**
- open(), read(), write(), and close()
- Working with File Paths
- **6.2 Exception Handling**
- try, except, else, finally
- Common Exceptions
**Chapter 7: Object-Oriented Programming (OOP)**
- **7.1 Classes and Objects**
- Defining Classes
- Creating Objects
- **7.2 Attributes and Methods**
- Instance and Class Variables
- Method Definitions
- **7.3 Inheritance and Polymorphism**
- Inheriting from Other Classes
- Overriding Methods
**Chapter 8: Practice Projects**
- **8.1 Simple Calculator**
- **8.2 To-Do List Application**
- **8.3 Basic Text-Based Game**
---
#### **Week 2: Intermediate Python**
**Chapter 9: Advanced Data Structures**
- **9.1 Collections Module**
- namedtuple, deque, Counter, OrderedDict
- **9.2 Comprehensions**
- List, Dictionary, and Set Comprehensions
**Chapter 10: Functional Programming**
- **10.1 Lambda Functions**
- **10.2 Map, Filter, and Reduce**
- **10.3 List Comprehensions vs. Map and Filter**
**Chapter 11: Working with APIs**
- **11.1 Making HTTP Requests**
- Using requests Library
- **11.2 Parsing JSON**
- Handling API Responses
- **11.3 Authenticating with APIs**
- OAuth and API Keys
**Chapter 12: Regular Expressions**
- **12.1 Introduction to Regex**
- Basic Patterns
- **12.2 Using re Module**
- search, match, findall
- **12.3 Practical Applications**
**Chapter 13: Working with Databases**
- **13.1 SQLite**
- Creating and Connecting to a Database
- CRUD Operations
- **13.2 SQLAlchemy**
- ORM Basics
- Defining Models and Schemas
**Chapter 14: Testing and Debugging**
- **14.1 Writing Unit Tests**
- Using unittest Framework
- **14.2 Debugging Techniques**
- Using pdb
- Common Debugging Strategies
**Chapter 15: File and Data Handling**
- **15.1 CSV and JSON**
- Reading and Writing CSV Files
- Handling JSON Data
- **15.2 Working with Excel Files**
- Using openpyxl
**Chapter 16: Web Scraping**
- **16.1 BeautifulSoup**
- Parsing HTML
- Navigating and Searching the Parse Tree
- **16.2 Scrapy**
- Setting Up a Scrapy Project
- Writing Spiders
---
#### **Week 3: Advanced Python**
**Chapter 17: Advanced OOP Concepts**
- **17.1 Class and Static Methods**
- **17.2 Abstract Base Classes**
- **17.3 Metaclasses**
**Chapter 18: Concurrency and Parallelism**
- **18.1 Threading**
- Creating and Managing Threads
- **18.2 Multiprocessing**
- Running Processes in Parallel
- **18.3 Asynchronous Programming**
- Using asyncio
**Chapter 19: Design Patterns**
- **19.1 Common Design Patterns**
- Singleton, Factory, Observer
- **19.2 Implementing Patterns in Python**
**Chapter 20: GUI Development**
- **20.1 Tkinter**
- Creating Basic Windows
- Handling Events
- **20.2 PyQt**
- Designing Advanced Interfaces
**Chapter 21: Networking**
- **21.1 Socket Programming**
- Creating TCP and UDP Connections
- **21.2 Building a Simple Server**
**Chapter 22: Advanced Web Development**
- **22.1 Flask**
- Creating Web Applications
- Routing and Templates
- **22.2 Django**
- Setting Up Projects
- Models, Views, and Templates
**Chapter 23: Data Science and Machine Learning**
- **23.1 NumPy and Pandas**
- Data Manipulation and Analysis
- **23.2 Matplotlib and Seaborn**
- Data Visualization Techniques
- **23.3 Introduction to Scikit-Learn**
- Basic Machine Learning Models
**Chapter 24: DevOps and Automation**
- **24.1 Scripting for Automation**
- Writing Scripts to Automate Tasks
- **24.2 Using Ansible**
- Configuration Management
- **24.3 Docker and Containerization**
---
#### **Week 4: Specialized Topics and Projects**
**Chapter 25: Advanced Data Handling**
- **25.1 Handling Large Datasets**
- Using Dask
- **25.2 Working with Time Series Data**
- pandas and statsmodels
**Chapter 26: Image and Signal Processing**
- **26.1 Working with PIL/Pillow**
- Image Manipulation and Analysis
- **26.2 Signal Processing with SciPy**
**Chapter 27: Natural Language Processing (NLP)**
- **27.1 Text Processing with NLTK and SpaCy**
- Tokenization, Lemmatization, Named Entity Recognition
- **27.2 Building NLP Pipelines**
**Chapter 28: Game Development**
- **28.1 Pygame Basics**
- Creating Simple Games
- **28.2 Advanced Game Mechanics**
**Chapter 29: Security and Cryptography**
- **29.1 Understanding Encryption and Hashing**
- Using hashlib and cryptography Libraries
- **29.2 Building Secure Applications**
**Chapter 30: Final Projects and Best Practices**
- **30.1 Developing a Complete Application**
- Bringing Together Different Skills
- **30.2 Code Optimization and Refactoring**
- Writing Efficient and Clean Code
- **30.3 Best Practices in Python Programming**
- PEP 8 Guidelines
- Code Reviews and Testing
Certainly! Here is a detailed write-up for the first few chapters of the book
"Mastering Python: From Beginner to Advanced". The sections will include
explanations, examples, and exercises to solidify your understanding.
---
### **Mastering Python: From Beginner to Advanced**
---
#### **Week 1: Python Fundamentals**
---
### **Chapter 1: Introduction to Python**
**1.1 What is Python?**
Python is a high-level, interpreted programming language known for its easy-to-read
syntax and versatility. Created by Guido van Rossum and first released in 1991,
Python supports multiple programming paradigms, including procedural, object-
oriented, and functional programming.
**Key Features of Python:**
- **Simple and Readable Syntax:** Python’s syntax is designed to be intuitive and
mirrors human language.
- **Interpreted Language:** Python code is executed line by line, which makes
debugging easier.
- **Extensive Standard Library:** Python comes with a large library of modules and
packages, covering areas like web development, data analysis, machine learning, and
more.
- **Cross-Platform:** Python runs on various platforms such as Windows, macOS, and
Linux.
**Installing Python:**
1. **Download Python:** Visit the [official Python
website](https://www.python.org/downloads/) and download the latest version of
Python.
2. **Install Python:** Follow the installation instructions for your operating
system. Ensure you add Python to your PATH variable during installation.
3. **Verify Installation:** Open your terminal or command prompt and type `python
--version` to confirm the installation.
**Setting Up a Development Environment:**
- **IDEs and Code Editors:** Popular choices include VS Code, PyCharm, and Jupyter
Notebook.
- **Virtual Environments:** Use `venv` to create isolated environments for your
projects.
```bash
# Creating a virtual environment
python -m venv myenv
# Activating the virtual environment
# On Windows
myenv\Scripts\activate
# On macOS/Linux
source myenv/bin/activate
```
**1.2 Your First Python Program**
Let’s start with a simple "Hello, World!" program.
```python
# hello.py
print("Hello, World!")
```
To run the script, save it as `hello.py` and execute it in your terminal:
```bash
python hello.py
```
---
### **Chapter 2: Basic Syntax and Operations**
**2.1 Variables and Data Types**
Variables in Python are used to store data values. You don’t need to declare the
type of a variable explicitly.
```python
# Variables
name = "Alice"
age = 30
height = 5.5
is_student = True
# Printing variables
print(name, age, height, is_student)
```
**Data Types:**
- **Integers:** Whole numbers (e.g., 10, -5)
- **Floats:** Decimal numbers (e.g., 3.14, -2.5)
- **Strings:** Text enclosed in quotes (e.g., "Hello", 'Python')
- **Booleans:** True or False
**Type Conversion:**
```python
# Converting data types
age = "30"
age = int(age) # Converts string to integer
height = 170
height = float(height) # Converts integer to float
```
**2.2 Basic Operators**
Operators are used to perform operations on variables and values.
**Arithmetic Operators:**
```python
# Addition
print(5 + 3)
# Subtraction
print(10 - 2)
# Multiplication
print(4 * 7)
# Division
print(16 / 4)
# Floor Division
print(16 // 3)
# Modulus
print(16 % 3)
# Exponentiation
print(2 ** 3)
```
**Comparison Operators:**
```python
print(5 == 5) # Equal to
print(5 != 3) # Not equal to
print(5 > 3) # Greater than
print(5 < 3) # Less than
print(5 >= 3) # Greater than or equal to
print(5 <= 3) # Less than or equal to
```
**Logical Operators:**
```python
print(True and False) # Logical AND
print(True or False) # Logical OR
print(not True) # Logical NOT
```
**Assignment Operators:**
```python
x = 5
x += 3 # x = x + 3
x -= 2 # x = x - 2
x *= 4 # x = x * 4
x /= 2 # x = x / 2
x //= 3 # x = x // 3
x %= 2 # x = x % 2
x **= 3 # x = x ** 3
```
**Exercises:**
1. Write a program that calculates the area of a rectangle given its width and
height.
2. Write a program that converts temperature from Celsius to Fahrenheit.
---
### **Chapter 3: Control Flow**
**3.1 Conditional Statements**
Conditional statements allow you to execute code based on certain conditions.
```python
# if statement
age = 18
if age >= 18:
print("You are an adult.")
# if-else statement
if age < 18:
print("You are a minor.")
else:
print("You are an adult.")
# if-elif-else statement
score = 85
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
else:
print("D")
```
**3.2 Loops**
Loops are used to repeat a block of code multiple times.
**For Loop:**
```python
# Looping through a range of numbers
for i in range(5):
print(i)
# Looping through a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
```
**While Loop:**
```python
# Using while loop
count = 0
while count < 5:
print(count)
count += 1
```
**Break and Continue:**
```python
# Break statement
for i in range(10):
if i == 5:
break
print(i)
# Continue statement
for i in range(10):
if i % 2 == 0:
continue
print(i)
```
**List Comprehensions:**
List comprehensions provide a concise way to create lists.
```python
# Creating a list of squares
squares = [x ** 2 for x in range(10)]
print(squares)
# Filtering even numbers
evens = [x for x in range(10) if x % 2 == 0]
print(evens)
```
**Exercises:**
1. Write a program that prints all prime numbers between 1 and 100.
2. Write a program that calculates the factorial of a number using a while loop.
---
### **Chapter 4: Functions and Modules**
**4.1 Defining Functions**
Functions are reusable pieces of code that perform a specific task.
```python
# Defining a function
def greet(name):
print(f"Hello, {name}!")
# Calling a function
greet("Alice")
```
**Parameters and Return Values:**
```python
# Function with parameters and return value
def add(a, b):
return a + b
result = add(3, 4)
print(result)
```
**Default and Keyword Arguments:**
```python
# Default arguments
def greet(name, message="Hello"):
print(f"{message}, {name}!")
greet("Alice")
greet("Bob", "Good morning")
# Keyword arguments
def describe_pet(pet_name, animal_type="dog"):
print(f"I have a {animal_type} named {pet_name}.")
describe_pet(pet_name="Willie")
describe_pet(pet_name="Harry", animal_type="hamster")
```
**4.2 Modules and Packages**
Modules allow you to organize your code into separate files. A package is a
collection of modules.
**Importing Modules:**
```python
# Importing a module
import math
print(math.sqrt(16))
# Importing specific functions
from math import sqrt, pi
print(sqrt(25))
print(pi)
```
**Creating a Module:**
Create a file named `mymodule.py`:
```python
# mymodule.py
def greet(name):
print(f"Hello, {name}!")
```
In another script, you can import and use this module:
```python
import mymodule
mymodule.greet("Alice")
```
**Exercises:**
1. Write a module with a function that calculates the area of a circle.
2. Write a program that imports your module and uses the function to calculate the
area of a circle with a given radius.
---
### **Chapter 5: Data Structures**
**5.1 Lists**
Lists are ordered collections of items.
```python
# Creating a list
fruits = ["apple", "banana", "cherry"]
# Accessing elements
print(fruits[0]) # Output: apple
# Modifying elements
fruits[1] = "blueberry"
print(fruits)
# Adding elements
fruits.append("orange")
print(fruits)
# Removing elements
fruits.remove("cherry")
print(fruits)
# List methods
print(len(fruits))
print(fruits.index("apple"))
print(fruits.count("apple"))
```
**5.2 Tuples**
Tu
ples are immutable, ordered collections of items.
```python
# Creating a tuple
coordinates = (10, 20)
# Accessing elements
print(coordinates[0])
# Tuples are immutable
# coordinates[0] = 15 # This will raise an error
```
**5.3 Sets**
Sets are unordered collections of unique items.
```python
# Creating a set
fruits = {"apple", "banana", "cherry"}
# Adding elements
fruits.add("orange")
print(fruits)
# Removing elements
fruits.remove("banana")
print(fruits)
# Set operations
another_set = {"cherry", "kiwi", "apple"}
print(fruits.union(another_set))
print(fruits.intersection(another_set))
print(fruits.difference(another_set))
```
**5.4 Dictionaries**
Dictionaries are collections of key-value pairs.
```python
# Creating a dictionary
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
# Accessing values
print(person["name"])
# Modifying values
person["age"] = 26
print(person)
# Adding key-value pairs
person["job"] = "Engineer"
print(person)
# Removing key-value pairs
del person["city"]
print(person)
# Dictionary methods
print(person.keys())
print(person.values())
print(person.items())
```
**Exercises:**
1. Create a list of your favorite movies and print them one by one.
2. Write a program that counts the number of each vowel in a given string using a
dictionary.
---
### **Chapter 6: File I/O and Exception Handling**
**6.1 Reading and Writing Files**
Python makes it easy to work with files.
**Reading a File:**
```python
# Reading a file
with open("example.txt", "r") as file:
content = file.read()
print(content)
# Reading line by line
with open("example.txt", "r") as file:
for line in file:
print(line.strip())
```
**Writing to a File:**
```python
# Writing to a file
with open("example.txt", "w") as file:
file.write("Hello, World!")
# Appending to a file
with open("example.txt", "a") as file:
file.write("\nThis is a new line.")
```
**Working with File Paths:**
```python
# Using the os module
import os
# Get current working directory
print(os.getcwd())
# Change working directory
os.chdir("/path/to/directory")
# Create a new directory
os.mkdir("new_directory")
# List files in a directory
print(os.listdir("."))
```
**6.2 Exception Handling**
Handling errors gracefully is crucial for robust programs.
**Basic Exception Handling:**
```python
try:
result = 10 / 0
except ZeroDivisionError:
print("Error: Division by zero is not allowed.")
```
**Multiple Exceptions:**
```python
try:
with open("example.txt", "r") as file:
content = file.read()
result = 10 / int(content)
except FileNotFoundError:
print("Error: File not found.")
except ValueError:
print("Error: Cannot convert content to integer.")
except ZeroDivisionError:
print("Error: Division by zero.")
```
**Finally Clause:**
```python
try:
file = open("example.txt", "r")
content = file.read()
finally:
file.close()
print("File closed.")
```
**Exercises:**
1. Write a program that reads a file and prints its content line by line.
2. Write a program that prompts the user for a file name, reads the file, and
handles possible exceptions (file not found, read error).
---
### **Chapter 7: Object-Oriented Programming (OOP)**
**7.1 Classes and Objects**
Classes define the blueprint for objects.
```python
# Defining a class
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
print(f"{self.name} is barking.")
# Creating objects
dog1 = Dog("Buddy", 3)
dog2 = Dog("Lucy", 5)
# Accessing attributes and methods
print(dog1.name)
dog1.bark()
```
**7.2 Attributes and Methods**
Attributes store data about the object, and methods define its behavior.
**Instance and Class Variables:**
```python
class Dog:
species = "Canis familiaris" # Class variable
def __init__(self, name, age):
self.name = name # Instance variable
self.age = age
def bark(self):
print(f"{self.name} is barking.")
print(Dog.species)
dog1 = Dog("Buddy", 3)
print(dog1.species)
```
**Method Definitions:**
```python
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
def bark(self):
print(f"{self.name} is barking.")
def birthday(self):
self.age += 1
def get_age(self):
return self.age
dog1 = Dog("Buddy", 3)
dog1.birthday()
print(dog1.get_age())
```
**7.3 Inheritance and Polymorphism**
Inheritance allows one class to inherit attributes and methods from another class.
**Inheritance:**
```python
class Animal:
def __init__(self, name):
self.name = name
def make_sound(self):
pass
class Dog(Animal):
def make_sound(self):
print(f"{self.name} says Woof!")
class Cat(Animal):
def make_sound(self):
print(f"{self.name} says Meow!")
dog = Dog("Buddy")
cat = Cat("Whiskers")
dog.make_sound()
cat.make_sound()
```
**Polymorphism:**
```python
class Bird:
def __init__(self, name):
self.name = name
def make_sound(self):
print(f"{self.name} says Tweet!")
animals = [Dog("Buddy"), Cat("Whiskers"), Bird("Tweety")]
for animal in animals:
animal.make_sound()
```
**Exercises:**
1. Create a class `Car` with attributes like brand, model, and year, and methods
like `start` and `stop`.
2. Create a class `ElectricCar` that inherits from `Car` and adds a new method
`charge`.
---
### **Chapter 8: Practice Projects**
**8.1 Simple Calculator**
Create a simple calculator that performs basic arithmetic operations.
```python
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(a, b):
if b != 0:
return a / b
else:
return "Error: Division by zero"
print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = input("Enter choice(1/2/3/4): ")
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(f"{num1} + {num2} = {add(num1, num2)}")
elif choice == '2':
print(f"{num1} - {num2} = {subtract(num1, num2)}")
elif choice == '3':
print(f"{num1} * {num2} = {multiply(num1, num2)}")
elif choice == '4':
print(f"{num1} / {num2} = {divide(num1, num2)}")
else:
print("Invalid input")
```
**8.2 To-Do List Application**
Create a to-do list application where users can add, view, and remove tasks.
```python
tasks = []
def add_task(task):
tasks.append(task)
print(f"Added task: {task}")
def view_tasks():
print("Your tasks:")
for i, task in enumerate(tasks, 1):
print(f"{i}. {task}")
def remove_task(index):
if 0 <= index < len(tasks):
removed = tasks.pop(index)
print(f"Removed task: {removed}")
else:
print("Invalid task number")
while True:
print("\nMenu:")
print("1. Add Task")
print("2. View Tasks")
print("3. Remove Task")
print("4. Exit")
choice = input("Enter choice: ")
if choice == '1':
task = input("Enter task: ")
add_task(task)
elif choice == '2':
view_tasks()
elif choice == '3':
view_tasks()
index = int(input("Enter task number to remove: ")) - 1
remove_task(index)
elif choice == '4':
break
else:
print("Invalid choice")
```
**8.3 Basic Text-Based Game**
Create a simple text-based adventure game where the player can navigate through
different rooms.
```python
rooms = {
'hall': {'south': 'kitchen', 'east': 'dining room'},
'kitchen': {'north': 'hall'},
'dining room': {'west': 'hall', 'south': 'garden'},
'garden': {'north': 'dining room'}
}
current_room = 'hall'
while True:
print(f"\nYou are in the
{current_room}")
print("Available exits: ", ", ".join(rooms[current_room].keys()))
move = input("Enter move: ")
if move in rooms[current_room]:
current_room = rooms[current_room][move]
else:
print("Invalid move. Try again.")
```
---
### **Week 2: Intermediate Python**
---
### **Chapter 9: More on Functions**
**9.1 Lambda Functions**
Lambda functions are small anonymous functions defined with the `lambda` keyword.
```python
# Lambda function for addition
add = lambda x, y: x + y
print(add(2, 3))
# Using lambda with map
numbers = [1, 2, 3, 4]
squares = list(map(lambda x: x ** 2, numbers))
print(squares)
```
**9.2 Decorators**
Decorators are functions that modify the behavior of another function.
```python
# Defining a decorator
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
# Using a decorator
@my_decorator
def say_hello():
print("Hello!")
say_hello()
```
**9.3 Generators**
Generators are functions that return an iterator that yields a sequence of values.
```python
def count_up_to(max):
count = 1
while count <= max:
yield count
count += 1
counter = count_up_to(5)
for number in counter:
print(number)
```
**Exercises:**
1. Write a lambda function that checks if a number is even.
2. Create a generator that yields the Fibonacci sequence up to a given number of
terms.
### **Chapter 10: File Handling with JSON and CSV**
**10.1 Working with JSON**
JSON (JavaScript Object Notation) is a lightweight data-interchange format that is
easy for humans to read and write and easy for machines to parse and generate.
**Reading JSON Data:**
```python
import json
# JSON string
json_data = '{"name": "Alice", "age": 30, "city": "New York"}'
# Parsing JSON
data = json.loads(json_data)
print(data)
print(data['name'])
```
**Writing JSON Data:**
```python
import json
# Python dictionary
data = {
"name": "Alice",
"age": 30,
"city": "New York"
}
# Converting to JSON string
json_data = json.dumps(data)
print(json_data)
# Writing JSON to a file
with open("data.json", "w") as file:
json.dump(data, file)
```
**Reading JSON from a File:**
```python
import json
# Reading JSON from a file
with open("data.json", "r") as file:
data = json.load(file)
print(data)
print(data['city'])
```
**10.2 Working with CSV**
CSV (Comma Separated Values) is a simple file format used to store tabular data,
such as a spreadsheet or database.
**Reading CSV Data:**
```python
import csv
# Reading CSV file
with open('data.csv', mode='r') as file:
csv_reader = csv.reader(file)
for row in csv_reader:
print(row)
```
**Writing CSV Data:**
```python
import csv
# Data to write
data = [
["name", "age", "city"],
["Alice", 30, "New York"],
["Bob", 25, "Los Angeles"]
]
# Writing to CSV file
with open('data.csv', mode='w', newline='') as file:
csv_writer = csv.writer(file)
csv_writer.writerows(data)
```
**Using DictReader and DictWriter:**
```python
import csv
# Reading CSV as dictionary
with open('data.csv', mode='r') as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
print(row)
# Writing CSV as dictionary
with open('data.csv', mode='w', newline='') as file:
fieldnames = ['name', 'age', 'city']
csv_writer = csv.DictWriter(file, fieldnames=fieldnames)
csv_writer.writeheader()
csv_writer.writerow({'name': 'Alice', 'age': 30, 'city': 'New York'})
csv_writer.writerow({'name': 'Bob', 'age': 25, 'city': 'Los Angeles'})
```
**Exercises:**
1. Create a program that reads a list of dictionaries from a JSON file and writes
it to a CSV file.
2. Write a program that reads data from a CSV file, processes it, and writes the
result back to another CSV file.
---
### **Chapter 11: Web Scraping with BeautifulSoup**
**11.1 Introduction to Web Scraping**
Web scraping is the process of extracting data from websites. Python provides
libraries like BeautifulSoup and requests to perform web scraping easily.
**Installing Required Libraries:**
```bash
pip install requests beautifulsoup4
```
**11.2 Fetching Web Pages:**
```python
import requests
# Fetching a web page
url = 'http://example.com'
response = requests.get(url)
print(response.text)
```
**11.3 Parsing HTML with BeautifulSoup:**
```python
from bs4 import BeautifulSoup
# Parsing HTML
html_content = '<html><head><title>Example</title></head><body><h1>Hello,
World!</h1></body></html>'
soup = BeautifulSoup(html_content, 'html.parser')
print(soup.title.text)
print(soup.h1.text)
```
**11.4 Navigating the HTML Tree:**
```python
from bs4 import BeautifulSoup
# Sample HTML
html_content = '''
<html>
<head>
<title>Example</title>
</head>
<body>
<h1>Hello, World!</h1>
<p class="description">This is an example page.</p>
<p class="content">BeautifulSoup is useful for web scraping.</p>
</body>
</html>
'''
soup = BeautifulSoup(html_content, 'html.parser')
# Accessing elements
print(soup.title.text)
print(soup.body.h1.text)
# Finding elements by class
descriptions = soup.find_all('p', class_='description')
for description in descriptions:
print(description.text)
```
**11.5 Extracting Data from Web Pages:**
```python
import requests
from bs4 import BeautifulSoup
# Fetching and parsing a web page
url = 'http://example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Extracting all links
links = soup.find_all('a')
for link in links:
print(link.get('href'))
```
**Exercises:**
1. Write a program that fetches a web page and extracts all the headings (h1, h2,
etc.).
2. Create a web scraper that extracts product names and prices from an e-commerce
website.
---
### **Chapter 12: Working with APIs**
**12.1 Introduction to APIs**
APIs (Application Programming Interfaces) allow applications to communicate with
each other. Many web services provide APIs to interact with their data.
**12.2 Making API Requests:**
```python
import requests
# Making a GET request
url = 'https://api.example.com/data'
response = requests.get(url)
print(response.json())
```
**12.3 Handling API Responses:**
```python
import requests
# Making a GET request
url = 'https://api.example.com/data'
response = requests.get(url)
data = response.json()
# Accessing data
print(data['key1'])
print(data['key2'])
```
**12.4 Sending Data to an API:**
```python
import requests
# Data to send
payload = {'key1': 'value1', 'key2': 'value2'}
# Making a POST request
url = 'https://api.example.com/data'
response = requests.post(url, json=payload)
print(response.json())
```
**12.5 Handling Errors and Exceptions:**
```python
import requests
url = 'https://api.example.com/data'
try:
response = requests.get(url)
response.raise_for_status() # Raise an exception for HTTP errors
data = response.json()
print(data)
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except Exception as err:
print(f"Other error occurred: {err}")
```
**Exercises:**
1. Write a program that fetches data from a weather API and prints the current
temperature and weather conditions for a given city.
2. Create a program that posts data to an API and handles possible errors and
exceptions.
---
### **Chapter 13: Database Interaction with SQLite**
**13.1 Introduction to SQLite**
SQLite is a C-language library that provides a lightweight, disk-based database. It
is a self-contained, serverless, and zero-configuration database engine.
**13.2 Setting Up SQLite:**
```python
import sqlite3
# Connecting to a database (creates the database if it doesn't exist)
conn = sqlite3.connect('example.db')
# Creating a cursor object
c = conn.cursor()
# Creating a table
c.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT,
age INTEGER)''')
# Committing the changes
conn.commit()
# Closing the connection
conn.close()
```
**13.3 Inserting Data:**
```python
import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
# Inserting data
c.execute("INSERT INTO users (name, age) VALUES ('Alice', 30)")
c.execute("INSERT INTO users (name, age) VALUES ('Bob', 25)")
# Committing the changes
conn.commit()
conn.close()
```
**13.4 Querying Data:**
```python
import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
# Querying data
c.execute("SELECT * FROM users")
rows = c.fetchall()
for row in rows:
print(row)
conn.close()
```
**13.5 Updating and Deleting Data:**
```python
import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
# Updating data
c.execute("UPDATE users SET age = 31 WHERE name = 'Alice'")
# Deleting data
c.execute("DELETE FROM users WHERE name = 'Bob'")
# Committing the changes
conn.commit()
conn.close()
```
**Exercises:**
1. Write a program that creates a database for storing book information (title,
author, year) and allows users to add, view, update, and delete book records.
2. Create a program that queries a database and prints records that match certain
criteria (e.g., users older than 25).
---
### **Week 3: Advanced Python Topics**
---
### **Chapter 14: Working with Dates and Times**
**14.1 Introduction to the datetime Module**
The `datetime` module supplies classes for manipulating dates and times.
**14.2 Getting the Current Date and Time:**
```python
from datetime import datetime
# Current date and time
now = datetime.now()
print(now)
# Formatting date and time
formatted = now.strftime('%Y-%m-%d %H:%M:%
S')
print(formatted)
```
**14.3 Creating Date and Time Objects:**
```python
from datetime import datetime, date, time
# Creating date object
d = date(2023, 5, 21)
print(d)
# Creating time object
t = time(14, 30)
print(t)
# Creating datetime object
dt = datetime(2023, 5, 21, 14, 30)
print(dt)
```
**14.4 Date and Time Arithmetic:**
```python
from datetime import datetime, timedelta
# Current date and time
now = datetime.now()
# Adding 10 days to the current date
future_date = now + timedelta(days=10)
print(future_date)
# Subtracting 5 hours from the current time
past_time = now - timedelta(hours=5)
print(past_time)
```
**14.5 Parsing and Formatting Dates:**
```python
from datetime import datetime
# Parsing a date string
date_string = '2023-05-21 14:30:00'
parsed_date = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S')
print(parsed_date)
# Formatting a datetime object
formatted_date = parsed_date.strftime('%A, %B %d, %Y at %I:%M %p')
print(formatted_date)
```
**Exercises:**
1. Write a program that calculates the number of days between two dates.
2. Create a program that takes a date string as input and prints the day of the
week for that date.
---
### **Chapter 15: Multithreading and Multiprocessing**
**15.1 Introduction to Concurrency**
Concurrency allows multiple tasks to run in overlapping time periods. Python
provides several modules to handle concurrency, including `threading` and
`multiprocessing`.
**15.2 Multithreading:**
```python
import threading
# Function to be executed in a new thread
def print_numbers():
for i in range(1, 6):
print(i)
# Creating a thread
thread = threading.Thread(target=print_numbers)
thread.start()
# Main thread continues to run
print("Main thread continues to run")
```
**15.3 Synchronizing Threads:**
```python
import threading
# Shared resource
counter = 0
# Lock object
lock = threading.Lock()
# Function to increment the counter
def increment_counter():
global counter
with lock:
for _ in range(1000):
counter += 1
# Creating threads
threads = []
for _ in range(10):
thread = threading.Thread(target=increment_counter)
threads.append(thread)
thread.start()
# Waiting for all threads to complete
for thread in threads:
thread.join()
print(f"Final counter value: {counter}")
```
**15.4 Multiprocessing:**
```python
import multiprocessing
# Function to be executed in a new process
def print_numbers():
for i in range(1, 6):
print(i)
# Creating a process
process = multiprocessing.Process(target=print_numbers)
process.start()
# Main process continues to run
print("Main process continues to run")
```
**15.5 Sharing Data Between Processes:**
```python
import multiprocessing
# Function to increment the counter
def increment_counter(counter):
for _ in range(1000):
counter.value += 1
# Shared value
counter = multiprocessing.Value('i', 0)
# Creating processes
processes = []
for _ in range(10):
process = multiprocessing.Process(target=increment_counter, args=(counter,))
processes.append(process)
process.start()
# Waiting for all processes to complete
for process in processes:
process.join()
print(f"Final counter value: {counter.value}")
```
**Exercises:**
1. Write a program that creates multiple threads to perform a simple task
concurrently and synchronizes access to a shared resource.
2. Create a program that uses multiprocessing to perform a CPU-intensive task in
parallel and compares the performance with a single-threaded version.
---
### **Chapter 16: Working with Regular Expressions**
**16.1 Introduction to Regular Expressions**
Regular expressions (regex) are a powerful tool for pattern matching in strings.
Python provides the `re` module for working with regular expressions.
**16.2 Basic Pattern Matching:**
```python
import re
# Matching a pattern
pattern = r'\d{3}-\d{2}-\d{4}'
text = 'My number is 123-45-6789.'
match = re.search(pattern, text)
if match:
print("Match found:", match.group())
else:
print("No match found")
```
**16.3 Finding All Matches:**
```python
import re
# Finding all matches
pattern = r'\b\w{5}\b'
text = 'Hello world, welcome to Python programming.'
matches = re.findall(pattern, text)
print(matches)
```
**16.4 Replacing Patterns:**
```python
import re
# Replacing a pattern
pattern = r'\d{3}-\d{2}-\d{4}'
text = 'My number is 123-45-6789.'
new_text = re.sub(pattern, 'XXX-XX-XXXX', text)
print(new_text)
```
**16.5 Using Groups and Capturing:**
```python
import re
# Using groups
pattern = r'(\d{3})-(\d{2})-(\d{4})'
text = 'My number is 123-45-6789.'
match = re.search(pattern, text)
if match:
print("Full match:", match.group())
print("Area code:", match.group(1))
print("Prefix:", match.group(2))
print("Line number:", match.group(3))
```
**Exercises:**
1. Write a program that uses regular expressions to validate email addresses.
2. Create a program that extracts all phone numbers from a given text and prints
them.
---
### **Chapter 17: Data Analysis with Pandas**
**17.1 Introduction to Pandas**
Pandas is a powerful data analysis and manipulation library for Python. It provides
data structures and functions needed to work on structured data seamlessly.
**17.2 Installing Pandas:**
```bash
pip install pandas
```
**17.3 Working with DataFrames:**
```python
import pandas as pd
# Creating a DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)
print(df)
```
**17.4 Reading and Writing Data:**
```python
import pandas as pd
# Reading CSV data
df = pd.read_csv('data.csv')
print(df)
# Writing DataFrame to CSV
df.to_csv('output.csv', index=False)
```
**17.5 DataFrame Operations:**
```python
import pandas as pd
# Creating a DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)
# Selecting columns
print(df['Name'])
# Selecting rows
print(df.loc[0])
print(df.iloc[1])
# Filtering data
filtered_df = df[df['Age'] > 30]
print(filtered_df)
```
**17.6 Data Aggregation and Grouping:**
```python
import pandas as pd
# Creating a DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'Alice', 'Bob'],
'Age': [25, 30, 35, 25, 30],
'Score': [85, 90, 95, 80, 85]
}
df = pd.DataFrame(data)
# Grouping and aggregating
grouped = df.groupby('Name').mean()
print(grouped)
```
**Exercises:**
1. Write a program that reads a CSV file into a DataFrame, performs some basic data
analysis (e.g., mean, sum, count), and writes the result to a new CSV file.
2. Create a DataFrame from a dictionary of lists and demonstrate various DataFrame
operations (selection, filtering, grouping).
---
### **Chapter 18: Data Visualization with Matplotlib**
**18.1 Introduction to Matplotlib**
Matplotlib is a plotting library for Python that provides an object-oriented API
for embedding plots into applications.
**18.2 Installing Matplotlib:**
```bash
pip install matplotlib
```
**18.3 Creating Simple Plots:**
```python
import matplotlib.pyplot as plt
# Creating a simple plot
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
plt.plot(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Simple Plot')
plt.show()
```
**18.4 Bar Charts:**
```python
import matplotlib.pyplot as plt
# Creating a bar chart
categories = ['A', 'B', 'C', 'D']
values = [4, 7, 1, 8]
plt.bar(categories, values)
plt.xlabel('Categories')
plt.ylabel('Values')
plt.title('Bar Chart')
plt.show()
```
**18.5 Histograms:**
```python
import matplotlib.pyplot as plt
# Creating a histogram
data = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5,
5, 5, 5, 5]
plt.hist(data, bins=5, edgecolor='black')
plt.xlabel('Bins')
plt.ylabel('Frequency')
plt.title('Histogram')
plt.show()
```
**18.6 Scatter Plots:**
```python
import matplotlib.pyplot as plt
# Creating a scatter plot
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
plt.scatter(x, y)
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Scatter Plot')
plt.show()
```
**Exercises:**
1. Create a line plot using Matplotlib that shows the trend of stock prices over
time.
2. Write a program that generates a bar chart from a dataset and customizes the
chart with labels and a title.
### **Chapter 19: Advanced Data Analysis with Pandas**
**19.1 Advanced DataFrame Operations**
```python
import pandas as pd
# Sample DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [24, 27, 22, 32],
'Score': [88, 92, 95, 85]
}
df = pd.DataFrame(data)
# Adding a new column
df['Passed'] = df['Score'] > 90
print(df)
# Applying a function to a column
df['Score'] = df['Score'].apply(lambda x: x + 5)
print(df)
```
**19.2 Handling Missing Data**
```python
import pandas as pd
import numpy as np
# Sample DataFrame with missing values
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [24, np.nan, 22, 32],
'Score': [88, 92, np.nan, 85]
}
df = pd.DataFrame(data)
# Filling missing values
df['Age'].fillna(df['Age'].mean(), inplace=True)
df['Score'].fillna(df['Score'].median(), inplace=True)
print(df)
# Dropping rows with missing values
df.dropna(inplace=True)
print(df)
```
**19.3 Merging and Joining DataFrames**
```python
import pandas as pd
# Sample DataFrames
data1 = {'ID': [1, 2, 3], 'Name': ['Alice', 'Bob', 'Charlie']}
data2 = {'ID': [3, 4, 5], 'Score': [95, 85, 90]}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Merging DataFrames
merged_df = pd.merge(df1, df2, on='ID', how='inner')
print(merged_df)
# Joining DataFrames
df1.set_index('ID', inplace=True)
df2.set_index('ID', inplace=True)
joined_df = df1.join(df2, how='outer')
print(joined_df)
```
**Exercises:**
1. Write a program that merges two DataFrames and handles missing values
appropriately.
2. Create a DataFrame and perform various operations like filtering, grouping, and
aggregating.
---
### **Chapter 20: Data Visualization with Seaborn**
**20.1 Introduction to Seaborn**
Seaborn is a powerful Python library for data visualization based on Matplotlib. It
provides a high-level interface for drawing attractive and informative statistical
graphics.
**20.2 Installing Seaborn:**
```bash
pip install seaborn
```
**20.3 Creating Statistical Plots:**
```python
import seaborn as sns
import matplotlib.pyplot as plt
# Sample DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie', 'David'],
'Age': [24, 27, 22, 32],
'Score': [88, 92, 95, 85]
}
df = pd.DataFrame(data)
# Creating a bar plot
sns.barplot(x='Name', y='Score', data=df)
plt.show()
```
**20.4 Customizing Plots:**
```python
import seaborn as sns
import matplotlib.pyplot as plt
# Sample DataFrame
data = sns.load_dataset('tips')
# Creating a scatter plot with a regression line
sns.lmplot(x='total_bill', y='tip', data=data, hue='smoker', markers=['o', 'x'])
plt.title('Total Bill vs Tip')
plt.show()
```
**20.5 Pair Plots and Heatmaps:**
```python
import seaborn as sns
import matplotlib.pyplot as plt
# Sample DataFrame
data = sns.load_dataset('iris')
# Pair plot
sns.pairplot(data, hue='species')
plt.show()
# Heatmap
corr = data.corr()
sns.heatmap(corr, annot=True, cmap='coolwarm')
plt.show()
```
**Exercises:**
1. Create various types of plots using Seaborn and customize them with titles,
labels, and legends.
2. Use a dataset to generate a pair plot and heatmap, and interpret the results.
---
### **Chapter 21: Machine Learning with Scikit-Learn**
**21.1 Introduction to Scikit-Learn**
Scikit-Learn is a powerful Python library for machine learning. It provides simple
and efficient tools for data mining and data analysis.
**21.2 Installing Scikit-Learn:**
```bash
pip install scikit-learn
```
**21.3 Basic Classification:**
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
# Loading the dataset
iris = load_iris()
X = iris.data
y = iris.target
# Splitting the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)
# Creating and training the model
model = KNeighborsClassifier(n_neighbors=3)
model.fit(X_train, y_train)
# Making predictions
y_pred = model.predict(X_test)
# Evaluating the model
accuracy = accuracy_score(y_test, y_pred)
print(f'Accuracy: {accuracy}')
```
**21.4 Basic Regression:**
```python
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
# Loading the dataset
boston = load_boston()
X = boston.data
y = boston.target
# Splitting the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)
# Creating and training the model
model = LinearRegression()
model.fit(X_train, y_train)
# Making predictions
y_pred = model.predict(X_test)
# Evaluating the model
mse = mean_squared_error(y_test, y_pred)
print(f'Mean Squared Error: {mse}')
```
**21.5 Model Evaluation:**
```python
from sklearn.metrics import classification_report, confusion_matrix
# Generating evaluation metrics
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
```
**Exercises:**
1. Build a classification model using a different dataset and evaluate its
performance.
2. Create a regression model, make predictions, and analyze the model’s accuracy
using different metrics.
---
### **Chapter 22: Deep Learning with TensorFlow**
**22.1 Introduction to TensorFlow**
TensorFlow is an open-source platform for machine learning and deep learning
developed by Google.
**22.2 Installing TensorFlow:**
```bash
pip install tensorflow
```
**22.3 Building a Simple Neural Network:**
```python
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
# Loading the dataset
mnist = tf.keras.datasets.mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train, X_test = X_train / 255.0, X_test / 255.0
# Building the model
model = Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')
])
# Compiling the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Training the model
model.fit(X_train, y_train, epochs=5)
# Evaluating the model
model.evaluate(X_test, y_test)
```
**22.4 Convolutional Neural Networks (CNNs):**
```python
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# Loading the dataset
mnist = tf.keras.datasets.mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(-1, 28, 28, 1) / 255.0
X_test = X_test.reshape(-1, 28, 28, 1) / 255.0
# Building the CNN model
model = Sequential([
Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
# Compiling the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Training the model
model.fit(X_train, y_train, epochs=5)
# Evaluating the model
model.evaluate(X_test, y_test)
```
**Exercises:**
1. Create and train a neural network on a different dataset.
2. Build a CNN for image classification and evaluate its performance.
---
### **Chapter 23: Natural Language Processing with NLTK**
**23.1 Introduction to NLTK**
NLTK (Natural Language Toolkit) is a comprehensive library for working with human
language data.
**23.2 Installing NLTK:**
```bash
pip install nltk
```
**23.3 Tokenization:**
```python
import nltk
nltk.download('punkt
')
from nltk.tokenize import word_tokenize, sent_tokenize
text = "Hello, world! This is a test."
words = word_tokenize(text)
sentences = sent_tokenize(text)
print(words)
print(sentences)
```
**23.4 Stemming and Lemmatization:**
```python
from nltk.stem import PorterStemmer, WordNetLemmatizer
nltk.download('wordnet')
# Stemming
stemmer = PorterStemmer()
print(stemmer.stem('running'))
# Lemmatization
lemmatizer = WordNetLemmatizer()
print(lemmatizer.lemmatize('running', pos='v'))
```
**23.5 Part-of-Speech Tagging:**
```python
from nltk import pos_tag
nltk.download('averaged_perceptron_tagger')
text = "Hello, world! This is a test."
words = word_tokenize(text)
pos_tags = pos_tag(words)
print(pos_tags)
```
**Exercises:**
1. Write a program that tokenizes a given text into words and sentences.
2. Perform stemming and lemmatization on a set of words and compare the results.
---
### **Chapter 24: Web Scraping with BeautifulSoup**
**24.1 Introduction to Web Scraping**
Web scraping is the process of extracting data from websites. BeautifulSoup is a
Python library for parsing HTML and XML documents.
**24.2 Installing BeautifulSoup and Requests:**
```bash
pip install beautifulsoup4 requests
```
**24.3 Scraping a Simple Web Page:**
```python
import requests
from bs4 import BeautifulSoup
# Sending a request to the website
url = 'https://example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Extracting and printing the title
title = soup.title.text
print(title)
```
**24.4 Extracting Data from HTML Elements:**
```python
import requests
from bs4 import BeautifulSoup
# Sending a request to the website
url = 'https://example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Extracting and printing all paragraphs
paragraphs = soup.find_all('p')
for p in paragraphs:
print(p.text)
```
**24.5 Handling Pagination:**
```python
import requests
from bs4 import BeautifulSoup
# Function to scrape data from a single page
def scrape_page(url):
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
data = soup.find_all('p')
for item in data:
print(item.text)
# Looping through multiple pages
base_url = 'https://example.com/page/'
for page_num in range(1, 6):
url = f'{base_url}{page_num}'
scrape_page(url)
```
**Exercises:**
1. Write a web scraper that extracts all the headings from a given website.
2. Create a scraper that follows pagination to extract data from multiple pages.
---
### **Chapter 25: Working with APIs**
**25.1 Introduction to APIs**
APIs (Application Programming Interfaces) allow different software applications to
communicate with each other.
**25.2 Sending Requests with Requests:**
```bash
pip install requests
```
**25.3 Making API Calls:**
```python
import requests
# Sending a GET request
url = 'https://api.example.com/data'
response = requests.get(url)
data = response.json()
print(data)
```
**25.4 Handling Authentication:**
```python
import requests
# Sending a GET request with headers for authentication
url = 'https://api.example.com/secure-data'
headers = {'Authorization': 'Bearer YOUR_ACCESS_TOKEN'}
response = requests.get(url, headers=headers)
data = response.json()
print(data)
```
**25.5 Posting Data to an API:**
```python
import requests
# Sending a POST request
url = 'https://api.example.com/data'
payload = {'key1': 'value1', 'key2': 'value2'}
response = requests.post(url, json=payload)
data = response.json()
print(data)
```
**Exercises:**
1. Write a program that makes a GET request to a public API and processes the
response.
2. Create a script that sends a POST request with a payload to an API and handles
the response.
---
### **Chapter 26: Introduction to Flask**
**26.1 Introduction to Flask**
Flask is a lightweight web framework for Python. It’s designed to be simple and
easy to use.
**26.2 Installing Flask:**
```bash
pip install Flask
```
**26.3 Creating a Simple Web Application:**
```python
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return "Hello, Flask!"
if __name__ == '__main__':
app.run(debug=True)
```
**26.4 Handling Routes and Templates:**
```python
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
if __name__ == '__main__':
app.run(debug=True)
```
**26.5 Working with Forms:**
```python
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def home():
return render_template('form.html')
@app.route('/submit', methods=['POST'])
def submit():
name = request.form['name']
return f"Hello, {name}!"
if __name__ == '__main__':
app.run(debug=True)
```
**Exercises:**
1. Create a Flask application that has multiple routes and displays different
pages.
2. Build a simple web form using Flask and handle form submissions.
---
### **Chapter 27: Advanced Flask Concepts**
**27.1 Database Integration with SQLAlchemy**
SQLAlchemy is a SQL toolkit and Object-Relational Mapping (ORM) library for Python.
**27.2 Installing SQLAlchemy:**
```bash
pip install Flask-SQLAlchemy
```
**27.3 Setting Up a Database:**
```python
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
def __repr__(self):
return f'<User {self.username}>'
if __name__ == '__main__':
app.run(debug=True)
```
**27.4 Performing Database Operations:**
```python
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
@app.route('/add/<username>')
def add_user(username):
new_user = User(username=username)
db.session.add(new_user)
db.session.commit()
return f"Added user {username}"
@app.route('/users')
def get_users():
users = User.query.all()
return f"Users: {users}"
if __name__ == '__main__':
app.run(debug=True)
```
**Exercises:**
1. Extend the Flask application to include more database models and relationships.
2. Create a web interface for adding, viewing, and deleting records in the
database.
---
### **Chapter 28: Introduction to Django**
**28.1 Introduction to Django**
Django is a high-level Python web framework that encourages rapid development and
clean, pragmatic design.
**28.2 Installing Django:**
```bash
pip install django
```
**28.3 Creating a Django Project:**
```bash
django-admin startproject myproject
cd myproject
python manage.py runserver
```
**28.4 Creating a Django App:**
```bash
python manage.py startapp myapp
```
**28.5 Defining Models and Views:**
```python
# myapp/models.py
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
# myapp/views.py
from django.shortcuts import render
from .models import Post
def home(request):
posts = Post.objects.all()
return render(request, 'home.html', {'posts': posts})
```
**28.6 Configuring URLs:**
```python
# myapp/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
# myproject/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
```
**Exercises:**
1. Create a Django application with multiple models and views.
2. Implement forms and handle form submissions in a Django project.
---
### **Chapter 29: Advanced Django Concepts**
**29.1 Django Rest Framework (DRF)**
DRF is a powerful and flexible toolkit for building Web APIs in Django.
**29.2 Installing DRF:**
```bash
pip install djangorestframework
```
**29.3 Creating API Endpoints:**
```python
# myapp/serializers.py
from rest_framework import serializers
from .models import Post
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = '__all__'
# myapp/views.py
from rest_framework import viewsets
from
.models import Post
from .serializers import PostSerializer
class PostViewSet(viewsets.ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer
# myapp/urls.py
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import PostViewSet
router = DefaultRouter()
router.register(r'posts', PostViewSet)
urlpatterns = [
path('', include(router.urls)),
]
```
**29.4 Authentication and Permissions:**
```python
# settings.py
INSTALLED_APPS = [
...
'rest_framework',
]
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
],
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
],
}
# myapp/views.py
from rest_framework.permissions import IsAuthenticated
class PostViewSet(viewsets.ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer
permission_classes = [IsAuthenticated]
```
**Exercises:**
1. Create a REST API for a Django application and test it using a tool like
Postman.
2. Implement user authentication and authorization for the API endpoints.
---
### **Chapter 30: Deployment and Best Practices**
**30.1 Deploying Flask Applications**
Deploying a Flask application involves preparing your application to run on a web
server.
**30.2 Using Gunicorn and Nginx:**
```bash
pip install gunicorn
gunicorn -w 4 myapp:app
```
**30.3 Configuring Nginx:**
```nginx
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
```
**30.4 Deploying Django Applications:**
**30.5 Using Gunicorn and Nginx:**
```bash
pip install gunicorn
gunicorn myproject.wsgi:application
```
**30.6 Configuring Nginx:**
```nginx
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
```
**30.7 Best Practices:**
- **Code Organization:** Keep your codebase well-organized with clear separation of
concerns.
- **Version Control:** Use version control systems like Git to manage your code.
- **Testing:** Write tests for your code to ensure reliability and stability.
- **Documentation:** Document your code and applications thoroughly.
- **Security:** Follow best security practices to protect your applications and
data.
**Exercises:**
1. Deploy a Flask application using Gunicorn and Nginx.
2. Deploy a Django application and configure it with Nginx and Gunicorn.