Zero to Hero_ Comprehensive Python Programming Course
Zero to Hero_ Comprehensive Python Programming Course
Programming Course
Module 1: Introduction to Python
Python is a high-level, interpreted programming language known for its readability and versatility 1
2 . It was created by Guido van Rossum and first released in 1991. Python’s syntax is designed to be easy
to understand, which makes it a popular choice for beginners 1 . It supports multiple paradigms
(procedural, object-oriented, functional) and has automatic memory management. Real-world uses include
web and Internet development (e.g. Instagram, Netflix’s algorithms), data science and machine learning,
automation scripts, and more 3 4 . For example, Netflix’s recommendation system and self-driving
car software use Python, demonstrating its power in industry 3 .
Example: Open a Python interpreter and type print("Hello, Python!") . It will display Hello,
Python! on the screen. This simple example shows how print() outputs text to the console.
Why Learn Python? Python’s community is large and supportive, and its standard library and third-party
packages cover almost any domain. It runs on all major platforms (Windows, macOS, Linux) and is free and
open-source 5 . Learning Python gives you the foundation to build websites, applications, games, data
analysis, automation scripts, and more. Python’s motto is that “readability counts,” so writing clear,
clean code is emphasized 6 7 .
Practice:
- List three things you want to build with Python and why.
- Find three popular applications or websites built with Python (e.g. YouTube, Dropbox).
Next, set up a coding environment or editor. Beginners often use IDLE (Python’s basic IDE) or a text editor
like Visual Studio Code or PyCharm. These tools highlight syntax, auto-complete code, and help you
manage files. For data science or machine learning, many use the Anaconda distribution, which bundles
Python with useful packages and the Jupyter Notebook environment.
Example: After installing, open a terminal (or Command Prompt on Windows) and type:
1
python3
The >>> prompt means you’re in the interactive Python shell 10 . Try entering print("Welcome!") and
hit Enter. The output Welcome! should appear.
Use Case: Once set up, you can run Python scripts. For example, create a file hello.py with
print("Hello, World!") and run python3 hello.py . This modular approach (writing and running
.py files) is how real projects are built.
Exercise:
- Verify your Python installation by checking the version as described above.
- Open a Python shell and use print() to display your name.
- Create a new Python file in your editor and run it from the terminal.
# This is a comment
print("Hello, World!") # print text to output
Running this program prints Hello, World! . The print() function sends text to standard output 11 .
Key Points:
- Indentation matters: For example:
if True:
print("Yes")
else:
print("No")
2
The indented lines are part of the if or else blocks. - Case sensitivity: Python distinguishes
Variable from variable . - Line continuation: You can split long lines with a backslash \ or inside
parentheses () , brackets [] , or braces {} .
Use Case: Python’s clear syntax makes it great for quick scripting. Beginners often start by writing simple
programs (like a calculator or text manipulator) to practice syntax.
Practice:
1. Write a program that prints:
Python is fun!
This is a new line.
2. Explain why indentation is required in Python. Try removing the indent in an if block and observe the
error.
You can change a variable’s type by assigning a different type (dynamic typing). Use functions like int() ,
float() , str() to cast types explicitly (type conversion). For example, int("123") converts the
string "123" to the integer 123 .
Example:
age = 30 # an int
height = 1.75 # a float
name = "Alice" # a string
is_adult = True # a boolean
# Type casting:
age_str = str(age) # converts 30 to "30"
num = int("42") # converts "42" to 42
3
x = 10 # x is int
x = "ten" # now x is str
This flexibility is powerful but also means you must keep track of types in your logic.
Use Cases: Data types represent real-world values. For example, use numbers for calculations (like
summing costs), strings for names or messages, booleans for conditions (e.g. is user logged in), and None
for empty placeholders.
Practice:
- Create variables of different types (int, float, str, bool) and print them.
- Try concatenating a string and a number: what error do you get? Use type casting to fix it.
- Write code to convert a float to an integer (what happens to the decimal part?).
Example:
If the user types Bob , the program will output Hello, Bob .
All input from input() is a string. If you need a number, cast it: age = int(input("Age: ")) . Always
validate user input in real programs.
String Formatting: For complex output, Python supports formatted strings. A modern way is using f-
strings (available in Python 3.6+):
This will replace {name} and {score} with the variable values. Alternatively, str.format() or %
formatting exist but f-strings are clearer.
Use Cases: Console I/O is useful for scripts and quick tools. For example, writing a command-line quiz,
calculator, or data-entry form.
Exercises:
1. Write a program that asks the user for two numbers and prints their sum.
4
2. Ask the user for their birth year and print their age (current year minus birth year).
3. Try printing variables using f-strings and the older format() method.
Module 6: Operators
Python supports common operators:
Example Use:
a = 10
b = 3
print("Sum:", a + b, "Product:", a * b, "Power:", a**b)
print("a > b?", a > b)
Use Case: Operators let you perform calculations and make decisions. E.g., checking if a number is even:
if x % 2 == 0: ... . Or combining conditions: if age >= 18 and has_permission: .
Practice:
- Predict the result of 7 % 3 , 2**5 , and -3 // 2 .
- Write a condition using and / or : e.g., check if a number is between 10 and 20.
- Experiment with string concatenation ( + ) vs arithmetic: "foo" + "bar" vs 2 + 3 .
if condition1:
# code if condition1 is True
elif condition2:
# code if condition1 is False but condition2 is True
else:
# code if all above conditions are False
5
Example:
age = 20
if age >= 18:
print("Adult")
else:
print("Minor")
Chaining conditions:
score = 75
if score >= 90:
print("A grade")
elif score >= 80:
print("B grade")
elif score >= 70:
print("C grade")
else:
print("Below C")
You can nest ifs and use boolean operators. Remember that Python uses indentation to define the blocks
under each if / else . Missing or wrong indentation will cause errors.
Use Cases: Conditional logic is everywhere: validating input (e.g., password length), branching flows in
games (if player health is 0, game over), and more.
Exercises:
1. Write a program that asks a number and prints whether it’s positive, negative, or zero.
2. Given three numbers, print the largest one using if/elif.
3. Create a simple grade converter: input a percentage and print “Pass” if ≥60, else “Fail”.
Module 8: Loops
Loops repeat actions. Python has two loop types: for and while .
• for loops: Iterate over items of a sequence (list, string, tuple, etc.) or over a range of numbers.
Example:
for i in range(5):
print(i)
6
This prints 0 through 4. range(n) generates numbers 0 to n-1 . You can also specify start and
step, e.g. range(2, 10, 2) yields 2,4,6,8 .
You can loop through other sequences:
count = 0
while count < 3:
print("Count is", count)
count += 1
Control statements: - break : exits the loop immediately. - continue : skips to the next iteration. - else
on loops: an optional else: after a loop executes if the loop ends normally (not via break ).
Use Case: Iterate through data. For example, summing all numbers in a list, printing table rows, polling for
user input until valid, etc.
Exercises:
1. Use a for loop to print even numbers from 2 to 20.
2. Use a while loop to compute factorial of a given number.
3. Write a loop that prints the characters of a string on separate lines.
4. Implement a loop that sums all elements in a list (e.g. [4, 8, 15, 16, 23, 42] ).
Module 9: Functions
Functions encapsulate reusable code. Define a function using def :
def function_name(parameters):
"""Optional docstring describing the function."""
# code block
return value # optional
Example:
def greet(name):
"""Prints a greeting."""
print("Hello, " + name + "!")
7
If a function doesn’t explicitly return , it returns None by default.
Functions can take parameters (inputs) and return values. Use return to send back a result:
Functions help break a program into logical parts. They also have local scope: variables defined inside are
not visible outside. You can have default parameters, variable-length args ( *args , **kwargs ), and
keyword arguments.
Use Case: Functions are everywhere. E.g., you might write a calculate_tax(income) function or a
fetch_data(url) function in a bigger program. They improve readability and reusability.
Exercises:
1. Write a function square(x) that returns x*x. Test it with different inputs.
2. Write a function is_even(n) that returns True if n is even, False otherwise.
3. Create a function factorial(n) using recursion or loops. Include error check for negative input.
• Lists ( list ): Ordered, mutable collections of items. Items can be any type and lists can be nested.
Example: fruits = ["apple", "banana", "cherry"] . You can add ( append ), remove
( remove ), or modify items. Lists are like dynamic arrays.
Usage: Store a sequence of values, e.g. a to-do list.
Citation: Lists are “dynamic mutable arrays” holding ordered collections, supporting heterogeneous
items 13 .
• Tuples ( tuple ): Ordered, immutable collections. Once created, you cannot change them (no add/
remove). Good for fixed groups of values. Example: point = (10, 20) .
Usage: Return multiple values from a function, or use as dictionary keys if needed.
Note: Only tuples are immutable among Python’s core structures 14 , which means they can be
hashed.
• Sets ( set ): Unordered collections of unique items. Mutable (you can add/remove), but cannot
contain duplicates. Example: colors = {"red", "green", "blue"} .
Usage: Useful for membership tests (fast lookup) and set operations (union, intersection, difference).
For example, setA & setB gives items in both.
Citation: Sets are “mutable collections of unique, immutable elements” and are much faster than lists
for membership tests 15 16 .
• Dictionaries ( dict ): Key-value mappings. Mutable, keys are unique and typically immutable types,
values can be anything. Example: person = {"name": "Alice", "age": 30} .
Usage: Store lookup tables, like a phonebook (lookup name -> number). Retrieving a value by key is
very fast (hash lookup) 17 .
8
Citation: Dictionaries map unique keys to values (like a real-world dictionary maps words to
definitions) and allow fast access by key 18 17 .
Example Code:
# List example
nums = [1, 2, 3]
nums.append(4) # now [1,2,3,4]
nums[0] = 10 # now [10,2,3,4]
# Tuple example
coords = (0, 1, 2)
# coords[0] = 5 # error, tuples are immutable
# Set example
letters = set(["a", "b", "c"])
letters.add("d") # now {'a','b','c','d'}
# Dictionary example
scores = {"Alice": 95, "Bob": 80}
print(scores["Alice"]) # outputs 95
scores["Carol"] = 87 # add new entry
Use Cases:
- Use lists when you need an ordered, changeable collection (e.g., a list of tasks).
- Use tuples when you need an ordered collection that shouldn’t change (e.g., coordinates).
- Use sets for unique collections and membership tests (e.g., unique usernames, or removing duplicates
from a list).
- Use dicts for lookups (e.g., storing grades by student name, or configuration parameters).
Exercises:
- Create a list of five items and perform append, remove, and sort operations.
- Given a list with duplicates, convert it to a set to eliminate duplicates.
- Write a dictionary that maps country names to their capitals. Add, retrieve, and delete an entry.
- Explain why you might choose a tuple over a list, or a set over a list, in different scenarios.
9
Example:
Formatting Strings: We saw f-strings in I/O. You can also format using .format() :
Or with % : "%s scored %d" % (name, score) . F-strings ( f"{name} scored {score}" ) are most
Pythonic (clear and concise).
Use Case: Text processing is everywhere: parsing user input, generating reports, processing CSV/text files,
etc. For example, a log analyzer might use .split() to break lines or regex (see below) for complex
patterns.
Exercises:
1. Given a string, print it backwards (hint: slicing).
2. Use string methods to check if a string is a palindrome (reads the same forwards and backwards).
3. Format a multiline message using f-strings and variables (e.g. user input and computed results).
try:
# code that may raise an exception
value = int(input("Enter a number: "))
result = 100 / value
except ValueError:
print("That was not a valid number!")
except ZeroDivisionError:
print("Cannot divide by zero!")
else:
print("Result is", result)
finally:
print("Execution complete (this runs no matter what).")
10
- The else block runs if no exception occurred.
- The finally block always runs (e.g. for cleanup).
You can catch multiple specific exceptions or one general except Exception: . It’s best practice to catch
specific exceptions to avoid masking errors.
Use Case: For example, when parsing user input, use a try/except to handle non-numeric input. When
opening files, catch IOError if the file doesn’t exist. This makes programs robust.
Exercises:
- Write code that asks for a filename and prints its contents. Handle the case where the file does not exist
(catch FileNotFoundError ).
- Try dividing by zero inside a try block and catch the exception. In the finally clause, print “Done”.
- Read a number and raise a custom exception if it’s negative (use raise ValueError("Negative!") ).
Here, "r" means read mode. Use "w" for write (overwrites) or "a" to append. The with statement
(context manager) ensures the file is closed automatically 19 , even if errors occur. You can also read line-by-
line: for line in f: .
import csv
with open("data.csv", newline="") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
print(row) # each row is a list of strings
import json
data = {"name": "Alice", "age": 30}
with open("data.json", "w") as f:
json.dump(data, f) # write JSON to file
# To read:
with open("data.json", "r") as f:
data = json.load(f)
11
Use Cases: File I/O is essential for data persistence, logging, configuration. For example, you might save
user settings to a JSON file, or process a large CSV dataset. Web scraping often involves saving HTML/text to
files. Just remember to handle exceptions (e.g. file not found, permission errors).
Exercises:
1. Create a text file manually with some lines. Write a Python program to open it and print each line with
line numbers.
2. Write a list of dictionaries (e.g. person records) to a CSV file, then read it back in and print the data.
3. Save a Python dictionary to a JSON file and then load it back.
Or import specific names: from math_utils import add . The math_utils.py is a module. A
package is a directory containing modules and an __init__.py file (can be empty). It lets you organize
modules hierarchically.
The Python Standard Library is a collection of modules and packages. You also install third-party packages
using pip (Python’s package manager): e.g. pip install requests . Then you can import
requests .
Use Case: For example, Python’s random module is a standard library module for generating random
numbers. To use it: import random and call random.random() or random.choice() . Splitting code
into modules/packages makes large projects manageable and reusable.
Exercises:
- Create a module myutils.py with a function hello() that prints “Hello from a module”. In another
file, import and call it.
- Install a package (like numpy or requests ) and write a short script that uses it (e.g. generate a random
number or fetch a web page).
- Organize two modules into a package (make a folder, put an __init__.py file, and import across them).
Example:
12
class Dog:
def __init__(self, name):
self.name = name # attribute
def bark(self):
print(self.name + " says woof!")
dog1 = Dog("Fido")
dog1.bark() # outputs: Fido says woof!
Here, Dog is a class. When we call Dog("Fido") , __init__ initializes the new object with
name="Fido" . The object dog1 has its own data ( dog1.name ) and the method dog1.bark() .
class Animal:
def eat(self): print("Eating")
class Cat(Animal): # Cat inherits from Animal
def meow(self): print("Meow")
- Polymorphism: Different classes having methods with the same name (e.g., both Cat and Dog have eat
methods).
- Abstraction: Using simple interfaces while hiding complex implementation.
Citation: OOP in Python “bundles properties and behaviors into individual objects 21 ” and uses classes as
blueprints 20 .
Use Case: OOP is used in large applications (GUI frameworks, games, web backends). For instance, in GUI
toolkits, each window or button is an object. In a banking app, you might have classes Account and
Customer . OOP makes code reusable and models real-world entities.
Exercises:
1. Define a class Rectangle with attributes width and height , and a method area() that returns
the area.
2. Create a class Square that inherits from Rectangle but only takes one side length in its constructor.
3. Explain the difference between class attributes and instance attributes (maybe by example).
• Decorators: A decorator is a function that takes another function and extends its behavior
without explicitly modifying it 22 . In Python, decorators are used with the @decorator_name
syntax above a function.
13
def my_decorator(func):
def wrapper():
print("Before function call")
func()
print("After function call")
return wrapper
@my_decorator
def say_hello():
print("Hello!")
say_hello()
The say_hello function is “wrapped” by my_decorator 22 . Common uses include logging, access
control, or measuring execution time.
• Generators: A generator is a special kind of function that returns a lazy iterator. Use the yield
keyword instead of return . Each call to the generator’s __next__() (implicitly via a loop)
resumes where it left off. Generators produce items one at a time and only when needed, saving
memory 23 .
Example:
def count_up_to(n):
i = 1
while i <= n:
yield i
i += 1
Here, count_up_to is a generator. It yields numbers one by one. Internally, it creates a generator object
when called, and each yield pauses the function state. This is memory-efficient for large sequences.
Citation: Generator functions return lazy iterators that do not store contents in memory 23 , making them
ideal for large or infinite streams of data.
14
Use Cases: Decorators are used in frameworks (e.g. @app.route in Flask marks a URL route). Generators
are used for streaming data (e.g., reading a large file line by line) or in iterative algorithms. Both help write
cleaner, more Pythonic code.
Exercises:
- Write a decorator @timer that measures and prints a function’s execution time.
- Create a generator that yields the Fibonacci sequence (one number at a time). Use it to print the first 10
Fibonacci numbers.
Example: square = lambda x: x*x . Then square(5) returns 25 . This is equivalent to:
def square(x):
return x*x
But lambda is useful for short, throwaway functions. They’re often used with functions like map() ,
filter() , or sorting key functions.
Functional tools: - map(function, iterable) applies function to each item of iterable . Example:
list(map(lambda x: x*2, [1,2,3])) gives [2,4,6] . - filter(function, iterable) filters
items where function(item) is True. Example: list(filter(lambda x: x%2==0, [1,2,3,4]))
gives [2,4] . - reduce(function, iterable) (in functools ) accumulates results (e.g. summing a
list). - List comprehensions offer a functional style too: Example: [x*x for x in range(5)] creates
[0,1,4,9,16] .
Functional programming often emphasizes immutability and using functions as first-class objects. Python is
multi-paradigm, so it supports functional features but not as strictly as some languages.
Use Case: Lambdas and functional tools are handy for quick data transformations. For example, sorting a
list of tuples by the second element: pairs.sort(key=lambda p: p[1]) . Or processing data in
pipelines.
Exercises:
1. Use map with a lambda to convert a list of Celsius temperatures to Fahrenheit.
2. Given a list of words, use filter to select only the words longer than 3 characters.
3. Use a list comprehension (instead of map ) to compute squares of numbers from 1 to 10.
Module 18: Useful Python Standard Libraries (math, datetime, os, random, etc.)
Python comes with a “batteries included” standard library of modules for many common tasks.
15
• datetime : Works with dates and times. You can get the current date/time, compute differences,
format dates, etc 25 .
Example:
import datetime
now = datetime.datetime.now()
print(now.year, now.month, now.day)
Many standard libraries exist: json , re for regular expressions, itertools , functools ,
collections (Counter, deque), logging , and more. Familiarizing yourself with them lets you avoid
reinventing the wheel.
Use Cases:
- A finance app might use datetime to handle dates and math for calculations.
- A system script might use os and sys to move files or parse command-line options.
- random can create dummy data or shuffle items.
Exercises:
- Generate and print a random password of 10 characters (use random.choice ).
- Use datetime to calculate the age (in years) given a birthdate.
- List all files in the current directory using os.listdir() and filter those ending in .py .
import requests
response = requests.get("https://api.github.com/users/octocat")
data = response.json() # parse JSON response into a dict
print(data["name"], "has", data["public_repos"], "public repos")
16
This sends an HTTP GET request, and response.json() converts the JSON payload to Python data.
Requests simplifies handling HTTP: methods for GET, POST, headers, authentication, timeouts, etc 27 . You
can check status codes ( response.status_code ) to handle errors.
Use Case: Python + APIs is common for retrieving data from the web: weather APIs, Twitter data, online
databases, etc. For example, automating posting to Twitter via its API, or fetching and processing data from
an online service.
Exercises:
1. Use requests to fetch https://api.github.com and print the status code and content.
2. Call a free public API (e.g. a joke API or a currency converter) and display some part of its response.
3. Write a function that takes a city name, calls a weather API (like OpenWeatherMap), and returns the
current temperature.
Example:
import requests
from bs4 import BeautifulSoup
url = "https://en.wikipedia.org/wiki/Python_(programming_language)"
res = requests.get(url)
soup = BeautifulSoup(res.text, 'html.parser')
title = soup.find('h1').text # get the main title
print("Page title:", title)
Here, soup.find('h1') finds the first <h1> element. You can navigate the parse tree, find elements by
class, id, or tags.
Use Case: Scraping is used when no API is available. For example, gather product prices from e-commerce
sites, or pull data from public records sites. Always respect websites’ robots.txt and use scraping
responsibly (don’t overload servers).
Citation: BeautifulSoup “creates parse trees that make it straightforward to extract data from HTML” 28 .
Practice:
- Scrape your own Github profile page or a simple Wikipedia article: extract and print all the section
headings.
- Write a scraper to get news headlines from a news website (make sure the site allows scraping).
17
- If you encounter dynamic content (loaded by JavaScript), note that requests alone won’t fetch it; tools like
Selenium or Scrapy might be needed (advanced topic).
Example workflow:
import sqlite3
# Create table
cur.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name
TEXT, age INTEGER)")
# Insert data
cur.execute("INSERT INTO users (name, age) VALUES (?, ?)", ("Alice", 30))
cur.execute("INSERT INTO users (name, age) VALUES (?, ?)", ("Bob", 25))
# Query data
cur.execute("SELECT * FROM users")
rows = cur.fetchall()
for row in rows:
print(row)
conn.close()
Use ? placeholders to avoid SQL injection. After inserts or updates, call commit() . Always close the
connection.
Citation: SQLite “doesn’t require a separate server” and is good for prototyping or local storage 29 . Python’s
sqlite3 is DB-API compliant 30 .
Use Case: Applications use SQLite for embedded data storage (e.g., mobile apps, small web apps). If data
grows or requires multi-user access, you might move to larger databases like PostgreSQL. Python code
structure stays similar (often through higher-level libraries).
Exercises:
1. Create an SQLite database and a table for storing book titles and authors. Insert a few records, then
retrieve and print them.
18
2. Update a record (e.g. change a book’s author) and delete a record. Make sure to commit your changes.
3. (Advanced) Use Python’s sqlite3 row factory to return query results as dictionaries
( conn.row_factory = sqlite3.Row ).
import tkinter as tk
def on_click():
print("Button clicked!")
This creates a window with one button. mainloop() waits for user events (like clicks).
Use Case: Tkinter is good for quick desktop tools (dialogs, form entry, simple games). For example, a file
renamer or a data entry form. For more advanced GUIs, other libraries (PyQt, Kivy) exist, but Tkinter is
included with Python and easy for beginners.
Exercises:
- Modify the above example to add a label, an entry box, and a button. When the button is clicked, display
the text from the entry in the label.
- Create a small to-do list app: have an entry and an “Add” button that inserts the text into a listbox widget.
• Flask: A microframework – meaning it’s lightweight and easy to start with 32 . It doesn’t impose a
database layer or form handling; you add extensions as needed.
Example: A minimal Flask app:
@app.route("/")
19
def home():
return "Hello, Flask!"
if __name__ == "__main__":
app.run(debug=True)
Run this, and go to http://127.0.0.1:5000/ to see “Hello, Flask!”. Flask uses decorators
( @app.route ) to define URL routes. It’s great for small to medium apps (APIs, simple websites).
• Django: A “batteries included” framework for larger projects 33 . It has built-in ORM (database layer),
admin interface, and follows the MTV (Model-Template-View) pattern. The official slogan is “for
perfectionists with deadlines”. You can create a project with django-admin startproject ,
define models, and have a ready-admin site.
Citation: Flask is a micro web framework (no required tools/libraries) 32 ; Django is a high-level framework
that helps build web apps quickly and securely 33 .
Use Case: Use Flask when you need flexibility or are building a small service (like a personal API or
lightweight website). Use Django for bigger sites requiring user auth, admin UI, and robust structure (e.g.,
content sites, social networks).
Exercises:
- (Flask) Build a simple web app with one route /hello/<name> that greets the user by name.
- (Django) Install Django, create a project, and inspect the automatically generated admin interface. Create
a simple model (e.g. Post with title and content) and see it in the admin.
• NumPy: The fundamental package for numerical computing. It provides the ndarray (n-
dimensional array) – a fast array object 34 . NumPy arrays use less memory and are faster than
Python lists for large data 35 .
Example:
import numpy as np
a = np.array([1, 2, 3])
print(a.mean(), a.dtype, a.shape)
NumPy supports vectorized operations: e.g., a * 2 multiplies each element, no explicit loop
needed.
Use Case: Fast math on large arrays, linear algebra, Fourier transforms, random number generation
(NumPy’s random), etc. 34 .
20
• Pandas: Built on NumPy, it provides DataFrames and Series for tabular data. A DataFrame is like a
spreadsheet or SQL table 36 .
Example:
import pandas as pd
df = pd.DataFrame({"name": ["Alice", "Bob"], "age": [25, 30]})
print(df)
It has powerful data manipulation tools: filtering, grouping, merging, handling missing data, and
reading from CSV/Excel/SQL. Pandas is essential for data cleaning and analysis.
Citation: Pandas is “a Python library used for working with datasets” with functions for analyzing and
cleaning data 36 .
Use Cases: Analyzing datasets (sales, survey data, etc.), doing statistical analysis, preparing data for
machine learning. For example, loading a CSV of sales data into a DataFrame, computing summaries (mean
sales), filtering by date ranges, etc.
Exercises:
1. Using NumPy, create a 3×3 matrix of random numbers and compute its determinant.
2. Load a CSV file using Pandas (you can create a sample or find one online) and display summary statistics
(use df.describe() ).
3. Use Pandas to filter rows of a DataFrame (e.g., select all rows where a column “score” > 80).
• Matplotlib: A comprehensive plotting library 37 . It can create line plots, bar charts, histograms,
scatter plots, etc. It is very flexible (though code can be verbose).
Example:
This makes a simple line chart. Matplotlib is the foundation for many other libraries. Citation:
“Matplotlib makes easy things easy and hard things possible” 37 .
• Seaborn: Built on Matplotlib, it provides a high-level interface for statistical plots 38 . It has
attractive default styles and simplifies complex visualizations.
Example:
21
import seaborn as sns
sns.set_theme()
df = sns.load_dataset("iris")
sns.scatterplot(data=df, x="sepal_length", y="sepal_width", hue="species")
This plots a scatter of the Iris flower dataset colored by species. Seaborn integrates with Pandas and
handles styling. Citation: Seaborn is “for making statistical graphics” on top of Matplotlib 38 .
Use Cases: Data exploration and presentation: plotting trends over time, distributions of data, relationships
between variables. For instance, a business analyst might use Matplotlib/Seaborn to visualize monthly sales
or customer segments.
Exercises:
- Plot a histogram of random numbers (use NumPy’s random).
- Use Seaborn to plot a bar chart of counts for categories (e.g., species counts in
sns.load_dataset("iris") ).
- Create a line chart with Matplotlib showing a mathematical function (e.g. sine wave).
Examples of automation:
- File Management: Rename a batch of files, organize photos into folders by date, backup directories.
- Email/Text: Send automated emails or texts (with libraries like smtplib or Twilio API).
- Web Automation: Using requests / BeautifulSoup to fetch data periodically.
- Task Scheduling: Write a Python script to run daily (using cron or Task Scheduler) to check stock prices
and log them.
- GUI Automation: Libraries like pyautogui can automate mouse/keyboard (e.g., automatically fill
forms).
- Example Task: A script that logs into a website’s API, downloads data, processes it, and emails a report.
Citation: Python is often used to automate repetitive computer tasks – even beginners can write scripts to
rename files or download online content automatically 39 . This process of writing code to automate tasks
is sometimes called “scripting.”
Exercises:
- Write a script to automatically download the latest XKCD comic image every day.
- Automate zipping up a directory (use the shutil module’s make_archive ).
- (Advanced) Create a simple bot that monitors a website for changes (e.g., check if the content of a page
changes) and notifies you.
22
yields an iterator. Understanding iterators helps when creating custom iterable classes.
Citation: An iterator “contains a countable number of values” and can be traversed 40 .
• Context Managers: We saw with for files. Any object with __enter__ and __exit__ methods
is a context manager. This ensures resources are cleaned up. You can write your own by defining
these methods (or using @contextlib.contextmanager ). This is important for safely managing
resources (files, locks, database connections).
• Multithreading and Multiprocessing: Python’s threading module lets you run concurrent
threads, and multiprocessing spawns separate processes. However, due to the Global
Interpreter Lock (GIL), threads don’t execute Python bytecode truly in parallel for CPU-bound tasks.
They are useful for I/O-bound concurrency (e.g., making many network calls). For CPU-heavy
parallelism, multiprocessing or external solutions (like C extensions) are needed.
import asyncio
async def say():
print("Hello")
await asyncio.sleep(1)
print("World")
asyncio.run(say())
This runs say() coroutine. Under the hood, asyncio has an event loop and tasks. It’s widely used in
high-performance network servers.
Citation: asyncio is “a library to write concurrent code using the async/await syntax” and is suited for I/O-
bound tasks 41 .
Use Cases:
- Iterators/generators: processing streams of data lazily.
- Context managers: writing safe code (e.g., with lock: to acquire/release a lock).
- Threads/async: building servers or network clients that handle many connections (e.g., a chat server with
asyncio, or a multithreaded web scraper).
Exercises:
1. Create a class implementing the iterator protocol that yields even numbers indefinitely (hint: define
__iter__ and __next__ ).
2. Write an async function that fetches two web pages concurrently (use aiohttp or a dummy
asyncio.sleep ).
3. (Conceptual) Read about the GIL and explain its effect on multithreading.
23
(e.g., calculate_interest is better than ci ). The PEP 8 style guide recommends naming conventions,
indentation, and spacing 42 . For instance, use lowercase_with_underscores for functions and variables,
and CapitalWords for class names 43 .
- Structure: Keep functions small and focused. Use comments and docstrings to explain non-obvious logic.
Use modules to organize code. DRY principle (“Don’t Repeat Yourself”): reuse code instead of copying. -
Error Handling: Always check for possible errors (validate inputs, catch exceptions). Use logging ( import
logging ) instead of print for debug/info messages in production code. Logging allows setting levels
(DEBUG, INFO, ERROR) and outputs to files. - Testing: Write tests (using unittest or pytest ) to verify
code works as intended. Automated testing is a hallmark of robust code. - Version Control: Use tools like
Git to track changes and collaborate. - Debugging Tips:
- Use Python’s built-in pdb (e.g. insert import pdb; pdb.set_trace() to drop into a debugger).
- Many IDEs (VSCode, PyCharm) have graphical debuggers with breakpoints.
- Print statements can help trace values (though excessive prints clutter output; use logging or debugger
for complex cases).
- Read error tracebacks carefully to locate the cause of exceptions.
Citations: Following best practices is “writing readable code” and is crucial for maintenance and
collaboration 6 .
Exercises:
- Refactor a piece of messy code (yours or sample) to follow PEP 8 style and add meaningful names.
- Intentionally introduce a bug (e.g., divide by zero) and practice using traceback to find it. Then fix it.
- (Advanced) Install and try a linter tool (like flake8 ) on your code to see style warnings.
Final Project:
Combine what you’ve learned! Build a multi-feature Python application. For example, a “Movie Explorer”
program:
- Use requests to call a movie database API (e.g. The Open Movie Database) to search for movie info by
title.
- Store some results in an SQLite database.
- Create a simple GUI with Tkinter where the user enters a movie title and sees the fetched info.
- Optionally, add data visualization: show ratings over time for searched movies using Matplotlib.
- Implement error handling (API failures, no results found).
- Organize your code into modules (API handler, database, GUI, etc.) and use classes where appropriate.
- Follow clean code principles and document your code.
This project uses API calls, JSON parsing, a database, GUI, and optionally plotting – tying together many
concepts from web requests to data handling to user interface. It ensures you can structure a Python
program with multiple components working together.
By completing these modules and exercises, you will progress from knowing nothing about programming
to being comfortable with advanced Python features and ready to build professional applications. Happy
coding!
Sources: Concepts and definitions are supported by official Python documentation and reputable tutorials
1 2 5 18 23 27 29 31 44 33 34 36 37 38 19 41 6 .
24
1 What is Python? Executive Summary | Python.org
https://www.python.org/doc/essays/blurb/
29 30 sqlite3 — DB-API 2.0 interface for SQLite databases — Python 3.13.5 documentation
https://docs.python.org/3/library/sqlite3.html
34 35 Introduction to NumPy
https://www.w3schools.com/python/numpy/numpy_intro.asp
25
36 Pandas Introduction
https://www.w3schools.com/PYTHON/pandas/pandas_intro.asp
40 Python Iterators
https://www.w3schools.com/python/python_iterators.asp
26