Chapter 1: Introduction to Python
Welcome to the beginning of your Python journey! In this introductory chapter, we will uncover what Python is, delve into its history, understand the compelling reasons to learn it, and highlight its key features. By the end of this chapter, you will write your very first line of Python code.
1.1 What is Python?
Python is a high-level, interpreted, general-purpose programming language. Its design philosophy emphasizes code readability with its notable use of significant indentation. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.
Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented, and functional programming. Python is often described as a “batteries included” language due to its comprehensive standard library.
1.2 A Brief History of Python
Python was conceived in the late 1980s by Guido van Rossum at Centrum Wiskunde & Informatica (CWI) in the Netherlands as a successor to the ABC language. Its implementation began in December 1989. Van Rossum was the principal author, and his continuing central role in deciding the direction of Python was reflected in the title given to him by the Python community: “Benevolent Dictator for Life” (BDFL), a title from which he has since stepped down.
The name “Python” was inspired by the British comedy group Monty Python, as Guido van Rossum was a fan of their work.
Key Milestones:
- Python 1.0 was released in January 1994.
- Python 2.0 was released in October 2000, which included list comprehensions and a garbage collection system.
- Python 3.0 was released in December 2008. This was a major revision of the language that is not completely backward-compatible with Python 2.x. The end-of-life for Python 2.7 was January 1, 2020, and Python 3 is the current and future version of the language.
1.3 Why Learn Python?
Python consistently ranks as one of the most popular programming languages in the world. Here are a few reasons why:
- Easy to Learn and Use: Python has a simple, clean, and intuitive syntax that reads like plain English. This makes it an excellent language for beginners.
- Versatile and Platform-Independent: Python can be used for a wide variety of applications, including web development, data science, artificial intelligence, machine learning, automation, scripting, and more. It runs seamlessly on all major operating systems like Windows, macOS, and Linux.
- Extensive Libraries and Frameworks: Python has a vast ecosystem of third-party libraries and frameworks that make development faster and more efficient. Popular examples include Django and Flask for web development, and NumPy, Pandas, and TensorFlow for data science.
- Strong and Supportive Community: Python has a large and active global community. This means there is a wealth of documentation, tutorials, and forums available to help you learn and solve problems.
- High Demand in the Job Market: The versatility of Python has led to high demand for Python developers in various industries, often with competitive salaries.
1.4 Key Features of Python
- Interpreted: Python code is executed line by line, which simplifies debugging and makes the development process faster.
- Dynamically Typed: You don’t need to declare the data type of a variable. The type is assigned at runtime, offering more coding flexibility.
- Object-Oriented: Python supports object-oriented programming (OOP) principles, allowing for the creation of reusable and modular code through classes and objects.
- High-Level Language: Python abstracts away complex hardware details like memory management, allowing developers to focus on solving problems.
- Extensive Standard Library: Python comes “batteries included” with a large standard library that provides modules and functions for a wide range of tasks.
1.5 Your First Python Program: “Hello, World!”
It is a tradition in programming to start with a program that prints “Hello, World!” to the screen. In Python, this is a remarkably simple one-liner.
print(“Hello, World!”)
This single line of code calls the print() function, which is a built-in Python function that outputs text to the console. The text to be printed is enclosed in double quotes, which tells Python that it is a string (a sequence of characters).
Chapter 2: Python Installation and Setup
Before you can start writing more complex Python programs, you need to have the Python interpreter installed on your computer. This chapter will guide you through the installation process on different operating systems and introduce you to the essential tools that come with Python.
2.1 Downloading Python
The official and most reliable source for the Python interpreter is the Python Software Foundation’s website: python.org.
- Navigate to python.org in your web browser.
- Hover over the “Downloads” section in the menu.
- The website will automatically detect your operating system (Windows, macOS, or Linux) and suggest the latest stable release for it. Click the button to download the installer.
It is highly recommended to download the latest stable version of Python 3.
2.2 Installation on Windows
- Run the Installer: Once the download is complete, open the downloaded .exe file to run the installer.
- Customize Installation: You will see the main installation screen. Crucially, check the box at the bottom that says “Add Python 3.x to PATH”. This step is very important as it allows you to run Python from the command prompt from any directory.
- Install Now: Click on “Install Now”. This will install Python with the recommended settings.
- Complete Setup: After the installation is finished, you will see a “Setup was successful” screen. You can now close the installer.
2.3 Installation on macOS
Modern versions of macOS come with a pre-installed version of Python 2. However, this is now outdated and you should install Python 3.
- Run the Installer: Open the downloaded .pkg file. This will launch the Python installer.
- Follow the Steps: Click “Continue” through the welcome and license agreement screens. You will be prompted for your password to authorize the installation.
- Installation: The installer will place the Python 3 application in your /Applications folder. The installer also automatically adds the necessary links to run Python from the command line.
2.4 Installation on Linux
Most modern Linux distributions come with Python 3 pre-installed. You can check which version you have by opening the terminal and typing:
python3 –version
If Python 3 is not installed, or you need to upgrade, you can use your distribution’s package manager.
For Debian/Ubuntu-based distributions:
sudo apt update
sudo apt install python3
For Fedora/RHEL-based distributions:
sudo dnf install python3
2.5 Verifying the Installation
Once the installation is complete on any OS, you can verify that it was successful.
- Open your command line interface:
- Windows: Open the Command Prompt or PowerShell.
- macOS: Open the Terminal app.
- Linux: Open the Terminal.
Check the Python version: Type the following command and press Enter.
python –version
or, on some systems:
python3 –version
2. Enter the Python Interpreter: Now, type python or python3 and press Enter. You should see a >>> prompt. This is the Python interactive shell.
Python 3.12.3 (…) on …
Type “help”, “copyright”, “credits” or “license” for more information.
>>>
- To exit the interactive shell, type exit() and press Enter.
2.6 An Introduction to Pip
When you install Python, you also get pip, the package installer for Python. Pip allows you to install and manage additional libraries and packages that are not part of the standard library.
You can verify that pip is installed by running:
pip –version
Chapter 3: Python Integrated Development Environments (IDEs)
While you can write Python code in any plain text editor, using an Integrated Development Environment (IDE) or a dedicated code editor can make your life as a programmer much easier. An IDE bundles tools for software development into a single graphical user interface (GUI), typically including a source code editor, build automation tools, and a debugger.
This chapter introduces three of the most popular environments for Python development: Visual Studio Code, PyCharm, and Jupyter Notebook.
3.1 Visual Studio Code (VS Code)
VS Code is a free, lightweight, yet powerful source code editor developed by Microsoft. It becomes a full-featured Python development environment through its extensive library of extensions.
- Pros: Free, fast, lightweight, highly customizable, excellent Python support.
- Cons: Requires some initial setup and extension installation.
Setting up VS Code for Python:
- Install VS Code: Download it from code.visualstudio.com.
- Install the Python Extension: Open VS Code, go to the Extensions view, search for “Python” published by Microsoft, and click “Install”.
- Select a Python Interpreter: Open a .py file, click on the Python version in the bottom-right of the status bar, and select the Python interpreter you installed in Chapter 2.
3.2 PyCharm
PyCharm is a dedicated IDE for Python developed by JetBrains. It is specifically designed for Python development and comes with everything you need out of the box. It has a free Community Edition and a paid Professional Edition.
- Pros: All-in-one solution, intelligent code editor, great project management.
- Cons: Can be more resource-intensive (slower) than text editors.
Setting up a Project in PyCharm (Community Edition):
- Install PyCharm: Download the Community Edition from jetbrains.com/pycharm/.
- Create a New Project: Open PyCharm, click “New Project”, give it a name, and PyCharm will handle the setup of the interpreter.
3.3 Jupyter Notebook
Jupyter Notebook is a web-based interactive computing platform that allows you to create and share documents that contain live code, equations, visualizations, and narrative text. It’s incredibly popular in data science, scientific computing, and machine learning.
- Pros: Interactive and exploratory, combines code and documentation, great for data visualization.
- Cons: Not ideal for developing large, complex applications.
Getting Started with Jupyter:
- Open your terminal or command prompt.
Install Jupyter Notebook using pip:
pip install notebook
- Run Jupyter Notebook:
jupyter notebook
3.4 Which One Should You Choose?
- For General Purpose Programming & Beginners: VS Code is an excellent choice.
- For a Dedicated, Powerful Python Experience, PyCharm Community Edition is fantastic.
- For Data Analysis or Scientific Computing: Jupyter Notebook is the industry standard.
Chapter 4: Python Basic Syntax
Now that you have your development environment ready, it’s time to dive into the code. One of the most celebrated aspects of Python is its clean and readable syntax. This chapter will cover the fundamental rules of writing Python code, including indentation, creating variables, and adding comments.
4.1 The Importance of Indentation
Unlike many other programming languages that use braces {} to define blocks of code, Python uses indentation. A code block is a group of related statements, such as the body of a function or a loop. The standard convention is to use four spaces for each level of indentation.
temperature = 35
if temperature > 30:
print("It's a hot day!") # This line is indented, so it's inside the if-block.
print("Drink plenty of water.") # This line is also in the if-block.
If you fail to indent correctly, Python will raise an IndentationError.
4.2 Variables and Naming Conventions
A variable is a name given to a memory location. In Python, you can create a variable simply by assigning a value to it using the equals sign (=).
name = "Alice" # A variable 'name' of type string
age = 30 # A variable 'age' of type integer
Python is dynamically typed, which means you do not need to explicitly declare the type of a variable.
Variable Naming Rules:
- Must start with a letter (a-z, A-Z) or an underscore (_).
- The rest of the name can consist of letters, numbers, and underscores.
- Names are case-sensitive (age, Age, and AGE are different variables).
- The convention is to use snake_case for variable names (e.g., my_variable_name).
4.3 Comments: Explaining Your Code
Comments are lines in your code that are ignored by the Python interpreter.
Single-Line Comments: Use the hash symbol (#).
# This is a single-line comment.
x = 5 # This is an inline comment.
Multi-Line Comments (Docstrings): Use triple quotes (“””…””” or ”’…”’).
“””
This is a multi-line comment, often used as a docstring
to explain a function or module.
“””
Chapter 5: Python Data Types
In programming, a data type is a classification that specifies which type of value a variable can hold. You can find out the type of any variable using the type() function.
5.1 Text Type: String (str)
A string is a sequence of characters, enclosed in either single quotes (‘…’) or double quotes (“…”).
greeting = "Hello, World!"
user_name = 'python_programmer'
5.2 Numeric Types: int, float, complex
Integer (int): A whole number, positive or negative, without decimals.
age = 30
Float (float): A number, positive or negative, containing one or more decimals.
price = 19.99
Complex: Numbers with a real and imaginary part, written with a “j”.
complex_num = 2 + 5j
5.3 Boolean Type: bool
The boolean type has only two possible values: True or False. They are often the result of comparison operations.
is_learning = True
print(10 > 5) # Output: True
5.4 The None Type
The NoneType has a single value: None. It is used to represent the absence of a value or a null value.
winner = None
Chapter 6: Python Type Casting
Type casting (or type conversion) is the process of changing a variable’s data type from one to another. This is often necessary when handling data from different sources, such as user input, which is always a string.
6.1 Explicit Type Casting
You manually convert a data type using built-in functions like int(), float(), and str().
Casting to Integer (int()): Converts floats (by truncating) and compatible strings into integers.
price_float = 19.99
price_int = int(price_float) # Becomes 19
num_str = "100"
num_int = int(num_str) # Becomes 100
Casting to Float (float()): Converts integers and compatible strings into floating-point numbers.
age_int = 30
age_float = float(age_int) # Becomes 30.0
Casting to String (str()): Converts almost any other data type into a string.
age = 25
message = "I am " + str(age) + " years old."
6.2 Implicit Type Casting
Python automatically changes a data type, usually during arithmetic operations, to prevent data loss. For example, adding an int and a float will result in a float.
result = 10 + 5.5 # result is 15.5, a float
print(type(result)) # Output: <class 'float'>
Chapter 7: Python Operators
Operators are special symbols that carry out arithmetic or logical computation.
7.1 Arithmetic Operators
Used to perform mathematical operations. | Operator | Name | Example | Result | | :------- | :------------------- | :----------- | :----- | | + | Addition | 10 + 5 | 15 | | - | Subtraction | 10 - 5 | 5 | | * | Multiplication | 10 * 5 | 50 | | / | Division | 10 / 4 | 2.5 | | % | Modulus (remainder) | 10 % 3 | 1 | | ** | Exponentiation | 2 ** 3 | 8 | | // | Floor Division | 10 // 4 | 2 |
7.2 Comparison Operators
Used to compare two values, returning True or False. | Operator | Name | Example | | :------- | :------------------------- | :---------- | | == | Equal to | 5 == 5 | | != | Not equal to | 5 != 6 | | > | Greater than | 5 > 3 | | < | Less than | 5 < 3 | | >= | Greater than or equal to | 5 >= 5 | | <= | Less than or equal to | 5 <= 3 |
7.3 Logical Operators
Used to combine conditional statements. | Operator | Description | | :------- | :------------------------------------------------------------ | | and | Returns True if both statements are true | | or | Returns True if one of the statements is true | | not | Reverses the result, returns False if the result is true |
7.4 Assignment Operators
Used to assign values to variables. The simple assignment is =, but compound operators like += (x = x + 2) provide a shorthand.
Chapter 8: Python Conditional Statements (if…elif…else)
Conditional statements allow your program to execute different code based on certain conditions.
8.1 The if Statement
The if statement is used to execute a block of code only if a specified condition is true.
age = 20
if age >= 18:
print("You are eligible to vote.")
8.2 The else Statement
The else statement provides an alternative block of code to be executed if the if condition is False.
temperature = 15
if temperature > 25:
print("It's a warm day.")
else:
print("It's a bit chilly.")
8.3 The elif Statement
elif (else if) allows you to check multiple conditions in a chain.
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
else:
grade = "C"
print(f"Your grade is {grade}.")
Chapter 9: Python while Loops
A loop is a control flow structure that allows you to execute a block of code repeatedly. A while loop repeats as long as a certain condition is true.
9.1 The while Loop Syntax
count = 1
while count <= 5:
print(f"Current count is: {count}")
count += 1 # IMPORTANT: Update the counter to avoid an infinite loop
9.2 The break Statement
The break statement is used to exit a loop immediately.
while True:
user_input = input("Enter 'quit' to exit: ")
if user_input == 'quit':
break
9.3 The continue Statement
The continue statement is used to stop the current iteration and jump to the next one.
number = 0
while number < 10:
number += 1
if number % 2 == 0: # If number is even
continue # Skip the print statement for this iteration
print(number)
Chapter 10: Python for Loops
Python’s for loop is designed for iterating over a sequence (like a list, tuple, dictionary, set, or string).
10.1 The for Loop Syntax
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(f"I would like to eat a(n) {fruit}.")
10.2 The range() Function
The range() function generates a sequence of numbers, which is useful for looping a specific number of times.
# Loop 5 times (0 to 4)
for i in range(5):
print(f"Loop number {i}")
10.3 break and continue in for Loops
These statements work exactly the same way they do in while loops.
10.4 The else Block in a for Loop
An optional else block is executed only if the loop completes its entire sequence without being terminated by a break statement.
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num == 6:
print("Found 6!")
break
else:
print("The number 6 was not found.")
Chapter 11: Python Lists
Lists are one of the most versatile and frequently used data types in Python. A list is an ordered, mutable (changeable) collection of items. You create a list by placing items inside square brackets [].
11.1 Accessing and Modifying Items
List items are indexed starting from [0]. Negative indexing starts from the end [-1].
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
fruits[1] = "blueberry" # Change an item
print(fruits) # Output: ['apple', 'blueberry', 'cherry']
11.2 Common List Methods
- append(item): Adds an item to the end of the list.
- insert(index, item): Inserts an item at a specified index.
- pop(index): Removes and returns the item at the specified index (or the last item if no index is specified).
- remove(item): Removes the first occurrence of the specified item.
- sort(): Sorts the list in place.
- reverse(): Reverses the order of elements in the list in place.
numbers = [3, 1, 4]
numbers.append(2) # [3, 1, 4, 2]
numbers.sort() # [1, 2, 3, 4]
11.3 List Slicing
Slicing allows you to get a sub-list. The syntax is list[start:stop:step].
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers[2:5]) # Output: [2, 3, 4]
print(numbers[:3]) # Output: [0, 1, 2]
print(numbers[5:]) # Output: [5, 6, 7, 8, 9]
Chapter 12: Python Tuples
A tuple is an ordered, immutable collection of items. “Immutable” means that once a tuple is created, you cannot change, add, or remove its items. You create a tuple by placing items inside parentheses ().
12.1 Tuple Characteristics
- Immutable: The primary difference from lists. This makes them “read-only”.
coordinates = (10, 20)
# coordinates[0] = 15 # This will raise a TypeError
- Ordered: Items have a defined order.
- Allows Duplicates: Can contain items with the same value.
12.2 Accessing and Slicing
This works exactly the same as with lists.
days = ("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
print(days[0]) # Output: Mon
print(days[1:4]) # Output: ('Tue', 'Wed', 'Thu')
12.3 Unpacking a Tuple
A very elegant feature where you can assign tuple values to multiple variables at once.
point = (10, 20, 30)
x, y, z = point
print(f"x={x}, y={y}, z={z}") # Output: x=10, y=20, z=30
Chapter 13: Python Sets
A set is an unordered, mutable collection of unique items. This means a set cannot contain duplicate items.
13.1 Set Characteristics
- Unordered: Items have no defined order.
- Unindexed: You cannot access items by an index.
- Mutable: You can add and remove items.
- Unique: Duplicates are automatically removed.
numbers_with_duplicates = [1, 2, 3, 2, 4, 5, 1, 3]
unique_numbers = set(numbers_with_duplicates)
print(unique_numbers) # Output: {1, 2, 3, 4, 5}
Note: To create an empty set, you must use set(), not {} (which creates an empty dictionary).
13.2 Set Operations
Sets are powerful for performing mathematical operations.
- Union (|): All items from both sets.
- Intersection (&): Items that are present in both sets.
- Difference (–): Items in the first set but not in the second.
- Symmetric Difference (^): Items in one set or the other, but not both.
set_a = {1, 2, 3, 4}
set_b = {3, 4, 5, 6}
print(set_a | set_b) # Output: {1, 2, 3, 4, 5, 6}
print(set_a & set_b) # Output: {3, 4}
Chapter 14: Python Dictionaries
A dictionary is a mutable collection of key-value pairs. Each key must be unique and immutable (e.g., string, number, tuple). As of Python 3.7, dictionaries are ordered.
14.1 Creating and Accessing
You create dictionaries with curly braces {}. You access values using their keys in square brackets [].
student = {
"name": "Alice",
"age": 21,
"major": "Computer Science"
}
print(student["name"]) # Output: Alice
Using the .get(key) method is safer as it returns None if the key doesn’t exist, instead of raising an error.
14.2 Modifying Dictionaries
You can add new key-value pairs or change existing ones easily.
student["age"] = 22 # Modify an existing value
student["university"] = "State University" # Add a new pair
14.3 Looping Through a Dictionary
The most common way is to use the .items() method.
for key, value in student.items():
print(f"Key: {key}, Value: {value}")
Chapter 15: Python Arrays (via the array module)
An array from the built-in array module is similar to a list, but it is type-constrained, meaning all its elements must be of the same data type. This makes it more memory-efficient for large sequences of numbers.
15.1 Creating an array
You must import the array module and specify a typecode upon creation.
import array
# Create an array of signed integers ('i')
integer_array = array.array('i', [10, 20, 30, 40, 50])
Common typecodes include 'i' for integer and 'd' for double-precision float.
15.2 Working with Arrays
Once created, an array behaves very much like a list, supporting indexing, slicing, and methods like .append() and .pop(). Its main difference is the strict type constraint.
Chapter 16: Python Functions Basics
A function is a reusable, named block of code that performs a specific task.
16.1 Defining and Calling a Function
Use the def keyword.
# Define a function
def greet():
"""This function prints a simple greeting message."""
print("Hello! Welcome to Python functions.")
# Call the function
greet()
16.2 Parameters and the return Statement
Functions can take inputs (parameters) and send back outputs (return value).
def add_numbers(a, b):
"""Takes two numbers and returns their sum."""
return a + b
sum_result = add_numbers(5, 3) # a=5, b=3 are arguments
print(sum_result) # Output: 8
Chapter 17: Advanced Function Arguments (*args and **kwargs)
These tools allow you to create functions that accept a variable number of arguments.
17.1 *args (Arbitrary Positional Arguments)
Collects extra positional arguments into a tuple.
def sum_all(*args):
total = 0
for number in args:
total += number
return total
print(sum_all(1, 2, 3, 4, 5)) # Output: 15
17.2 **kwargs (Arbitrary Keyword Arguments)
Collects extra keyword arguments into a dictionary.
def display_user_info(**kwargs):
for key, value in kwargs.items():
print(f"{key.title()}: {value}")
display_user_info(name="Alice", age=30)
17.3 Order of Arguments
The correct order in a function definition is: standard_args, *args, **kwargs.
Chapter 18: Lambda, Map, Filter, and Reduce
These are tools that enable a more functional programming style.
18.1 Lambda Functions
A small, anonymous, one-line function defined with the lambda keyword. lambda arguments: expression
square = lambda x: x * x
print(square(5)) # Output: 25
18.2 map() and filter()
- map(function, iterable): Applies a function to every item of an iterable.
- filter(function, iterable): Filters items from an iterable based on a function that returns True or False.
numbers = [1, 2, 3, 4, 5]
# Map example
squared = list(map(lambda x: x*x, numbers)) # [1, 4, 9, 16, 25]
# Filter example
evens = list(filter(lambda x: x % 2 == 0, numbers)) # [2, 4]
18.3 reduce()
Found in the functools module, reduce() applies a rolling computation to a sequence, reducing it to a single value.
from functools import reduce
total = reduce(lambda x, y: x + y, numbers) # 15
Chapter 19: Python Recursion
Recursion is a technique where a function calls itself to solve a problem.
19.1 Pillars of Recursion
- Base Case: The condition that stops the recursion.
- Recursive Step: The part where the function calls itself, moving closer to the base case.
19.2 Factorial Example
The factorial n! is n * (n-1)!, with a base case of 0! = 1.
def factorial(n):
# Base Case
if n == 0:
return 1
# Recursive Step
else:
return n * factorial(n - 1)
print(factorial(5)) # Output: 120
Chapter 20: Python Decorators
A decorator is a function that takes another function as an argument, adds functionality, and returns another function, without altering the original function’s code.
20.1 Decorator Syntax (@)
The @ symbol is “syntactic sugar” for applying a decorator.
def log_decorator(func):
def wrapper(*args, **kwargs):
print(f"Calling {func.__name__}...")
result = func(*args, **kwargs)
print("Finished call.")
return result
return wrapper
@log_decorator
def add(a, b):
return a + b
add(5, 3)
Chapter 21: Python Generators and the yield Keyword
Generators are a simple way to create iterators. They generate values one at a time using the yield keyword, making them incredibly memory-efficient for large datasets.
21.1 yield vs return
- return exits a function completely.
- yield pauses the function, saves its state, and sends back a value. It resumes from the same point on the next call.
def countdown(start):
while start > 0:
yield start
start -= 1
for number in countdown(3):
print(number) # Prints 3, then 2, then 1
21.2 Generator Expressions
A concise, lambda-like way to create a generator using parentheses ().
# This generator object takes up almost no memory
gen_exp = (x*x for x in range(1_000_000))
# The sum is calculated one item at a time
total = sum(gen_exp)
Chapter 22: Introduction to Object-Oriented Programming (OOP)
OOP is a programming paradigm centered around creating “objects” that bundle together data (attributes) and functions (methods).
22.1 Classes and Objects
- Class: A blueprint for creating objects (e.g., Car).
- Object: A specific instance created from a class (e.g., my_honda).
22.2 The __init__() Method
A special “constructor” method that is called automatically when a new object is created. Its purpose is to initialize the object’s attributes. The first parameter of any instance method is self, which refers to the object itself.
class Dog:
def __init__(self, name, age):
self.name = name # Attribute
self.age = age # Attribute
def bark(self): # Method
print(f"{self.name} says Woof!")
my_dog = Dog("Buddy", 4)
print(my_dog.name) # Access attribute
my_dog.bark() # Call method
Chapter 23: Introduction to Python Modules
A module is a file containing Python code (.py file) that you can import into other scripts to reuse its functions, classes, and variables.
23.1 The import Statement
# Import the entire math module
import math
print(math.sqrt(16))
# Import a specific function
from random import randint
print(randint(1, 10))
# Import with an alias
import datetime as dt
print(dt.datetime.now())
23.2 The if __name__ == “__main__” Block
This block contains code that will only run when the file is executed directly as a script, not when it is imported as a module. It’s used for tests or demonstrations.
def my_func():
return "Hello"
# This code only runs when this file is executed directly.
if __name__ == "__main__":
print("Running tests...")
assert my_func() == "Hello"
print("Tests passed!")
Chapter 24: Creating Python Packages
A package is a way to structure Python’s module namespace by grouping related modules into a directory hierarchy. A directory is considered a package if it contains a special __init__.py file.
24.1 Package Structure
my_project/
├── main.py
└── my_utils/
├── __init__.py
├── math_ops.py
└── string_ops.py
24.2 The Role of __init__.py
- Marks the directory as a package.
- Code inside it runs upon first import (for setup).
- Can be used to create a simpler API by importing functions from its modules, making them accessible at the package level.
# In my_utils/__init__.py
from .math_ops import add
# In main.py, you can now do:
from my_utils import add
print(add(5, 5))
Chapter 25: Using PIP – The Python Package Installer
pip is the command-line tool for installing and managing third-party packages from the Python Package Index (PyPI).
25.1 Basic pip Commands
- pip install <package_name>: Installs a package.
- pip list: Lists all installed packages.
- pip show <package_name>: Shows details about a package.
- pip install –upgrade <package_name>: Upgrades a package.
- pip uninstall <package_name>: Removes a package.
25.2 requirements.txt and Virtual Environments
- Virtual Environment: An isolated Python environment for a project. Create one with python -m venv venv_name. This is a crucial best practice.
- requirements.txt: A file that lists a project’s dependencies. Create it with pip freeze > requirements.txt. Install from it with pip install -r requirements.txt.
Chapter 26: OOP – Inheritance
Inheritance allows a new class (child class) to inherit attributes and methods from an existing class (parent class).
26.1 super() and Method Overriding
- Overriding: A child class can provide its own implementation of a method from the parent.
- super(): A function that allows a child class to call methods from its parent, especially useful for extending the parent’s __init__ method.
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # Call parent's init
self.breed = breed # Add new attribute
Chapter 27: OOP – Encapsulation and Polymorphism
27.1 Encapsulation
Bundling data and methods together and restricting direct access to an object’s internal state. In Python, we use a double underscore (__) prefix to create a “private” attribute that should not be changed from outside the class. Access is provided via public methods (getters and setters).
27.2 Polymorphism
“Many forms.” The ability of different objects to respond to the same method call in their own unique ways.
class Dog:
def speak(self): return "Woof!"
class Cat:
def speak(self): return "Meow!"
# This loop works on different object types
for animal in [Dog(), Cat()]:
print(animal.speak())
Chapter 28: File I/O – Reading From and Writing to Files
File Input/Output allows for data persistence.
28.1 The with Statement
The recommended way to work with files. It automatically handles closing the file. with open(“file.txt”, “mode”) as file:
28.2 Modes and Methods
- Modes: ‘r’ (read), ‘w’ (write, erases), ‘a’ (append).
- Reading: file.read() (entire file), or iterate line by line: for line in file:.
- Writing: file.write(“some string\n”). Remember to add \n for newlines.
Chapter 29: Error and Exception Handling
Gracefully manage runtime errors to prevent your program from crashing.
29.1 The try…except Block
- try: Code that might cause an error.
- except: Code that runs if an error occurs. You should catch specific exceptions.
- else: Code that runs only if no error occurred.
- finally: Code that runs no matter what, for cleanup.
try:
result = 10 / 0
except ZeroDivisionError as e:
print(f"Error caught: {e}")
else:
print("Division was successful.")
finally:
print("This block always runs.")
29.2 The raise Keyword
You can trigger exceptions intentionally in your own code with raise.
def set_age(age):
if age < 0:
raise ValueError("Age cannot be negative.")
Chapter 30: Working with CSV Files
The csv module is used to handle CSV (Comma-Separated Values) data.
30.1 Reading and Writing CSVs
- csv.reader / csv.writer: Work with rows as lists of strings.
- csv.DictReader / csv.DictWriter: Work with rows as dictionaries, using the header row for keys. This is often more readable.
import csv
with open('users.csv', 'r') as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
print(f"Name: {row['name']}, City: {row['city']}")
Note: When writing, always open the file with newline=''.
Chapter 31: Working with JSON Data
JSON (JavaScript Object Notation) is a standard format for data exchange, common in web APIs. The json module helps convert between Python objects and JSON.
31.1 Serialization (Python to JSON)
- json.dumps(obj): Converts a Python object to a JSON formatted string.
- json.dump(obj, file): Writes a Python object to a file in JSON format.
import json
person_dict = {"name": "Bob", "age": 42}
# Use indent=4 for pretty-printing
with open("person.json", "w") as f:
json.dump(person_dict, f, indent=4)
31.2 Deserialization (JSON to Python)
- json.loads(str): Parses a JSON string into a Python object.
- json.load(file): Reads a JSON file and parses it into a Python object.
import json
with open("person.json", "r") as f:
data = json.load(f)
print(data["name"]) # Output: Bob