Master Python Programming Step by Step
Complete interactive assignments from basic syntax to advanced concepts with hands-on practice.
Strings & String Methods
Master text manipulation with Python's comprehensive string methods
Assignment Description
Create a program that takes a user's name, age, and country, then prints a formatted message using three different string formatting methods:
- Using f-strings (Python 3.6+)
- Using the str.format() method
- Using the old % formatting
Example Solution
# String Formatting Assignment Solution
name = "Alice"
age = 30
country = "USA"
# 1. Using f-string
print(f"{name} is {age} years old and lives in {country}.")
# 2. Using format()
print("{} is {} years old and lives in {}.".format(name, age, country))
# 3. Using % formatting
print("%s is %d years old and lives in %s." % (name, age, country))
# Expected Output:
# Alice is 30 years old and lives in USA.
# Alice is 30 years old and lives in USA.
# Alice is 30 years old and lives in USA.
Assignment Description
Write a program that processes a paragraph of text and performs the following operations:
- Convert the entire text to uppercase and lowercase
- Split the text into words and count them
- Replace all occurrences of a specific word
- Find the position of a specific word
- Join a list of words back into a sentence
Lists & List Methods
Master Python's most versatile data structure with comprehensive methods
Assignment Description
Create a program that demonstrates various list operations:
- Create a list of 5 numbers and a list of 5 strings
- Append new items to both lists
- Insert an item at a specific position
- Remove items by value and by index
- Use slicing to get sublists
- Concatenate two lists
Assignment Description
Write a program that uses list comprehensions to:
- Create a list of squares from 1 to 10
- Filter even numbers from a list
- Convert a list of strings to uppercase
- Create a matrix using nested comprehensions
- Flatten a 2D list into a 1D list
Example Solution Snippet
# List Comprehensions Examples
# Squares of numbers 1-10
squares = [x**2 for x in range(1, 11)]
# Filter even numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evens = [x for x in numbers if x % 2 == 0]
# Convert strings to uppercase
words = ["hello", "world", "python"]
upper_words = [word.upper() for word in words]
# Create a 3x3 matrix
matrix = [[i+j for j in range(3)] for i in range(3)]
# Flatten a 2D list
flat_list = [item for sublist in matrix for item in sublist]
Classes & Objects
Learn Object-Oriented Programming with Python classes and objects
Assignment Description
Create a BankAccount class with the following specifications:
- Attributes: account_number, account_holder, balance
- Methods: deposit(amount), withdraw(amount), display_balance()
- Constructor to initialize the account
- Validation for withdrawal (balance shouldn't go negative)
- Create multiple objects and test all methods
Example Solution Structure
class BankAccount:
def __init__(self, account_number, account_holder, initial_balance=0):
self.account_number = account_number
self.account_holder = account_holder
self.balance = initial_balance
def deposit(self, amount):
if amount > 0:
self.balance += amount
print(f"Deposited ${amount}. New balance: ${self.balance}")
else:
print("Deposit amount must be positive")
def withdraw(self, amount):
if 0 < amount <= self.balance:
self.balance -= amount
print(f"Withdrew ${amount}. New balance: ${self.balance}")
else:
print("Insufficient funds or invalid amount")
def display_balance(self):
print(f"Account Holder: {self.account_holder}")
print(f"Account Number: {self.account_number}")
print(f"Current Balance: ${self.balance}")
# Create and test account
account1 = BankAccount("123456", "Alice Smith", 1000)
account1.deposit(500)
account1.withdraw(200)
account1.display_balance()
Modules & Packages
Organize your code with Python modules and create reusable packages
Assignment Description
Create a Python package called math_utils with the following structure:
__init__.py
basic_operations.py
geometry.py
statistics.py
tests/
__init__.py
test_operations.py
Each module should contain related functions:
- basic_operations.py: add, subtract, multiply, divide
- geometry.py: circle_area, rectangle_area, triangle_area
- statistics.py: mean, median, mode
- __init__.py: Should expose key functions for easy import