Python: The Complete Handbook
A Comprehensive, Structured Guide to Python Programming
100% Ad-Free Learning Edition
Compiled for clean reading and offline reference.
Covers concepts from absolute basics to advanced application structure.
Python: The Complete Handbook (Ad-Free Edition) 1
Chapter 1: Introduction to Python & Setup
Python is a popular, high-level, interpreted programming language created by Guido van Rossum and
released in 1991. It is designed with an emphasis on code readability, allowing programmers to express
concepts in fewer lines of code compared to languages like C++ or Java.
Why Choose Python?
Python works on multiple platforms (Windows, Mac, Linux, Raspberry Pi, etc.). It has a simple syntax similar
to the English language, which makes it perfect for beginners. Developers use Python for web development
(server-side), software development, mathematics, system scripting, and extensively in Data Science and
Machine Learning.
Syntax Comparison & Execution
In Python, commands are executed line by line. Unlike many other programming languages that use
semicolons to finish a statement, Python uses a new line to complete a command. Furthermore, Python relies
heavily on indentation (whitespace at the beginning of a line) to define scope, such as the scope of loops,
functions, and classes.
# This is a comment in Python
print("Hello, World!")
Chapter 2: Variables & Core Data Types
Variables are containers for storing data values. Python has no command for declaring a variable; a variable
is created the moment you first assign a value to it.
x = 5
y = "John"
print(x)
print(y)
Dynamic Typing
Python is dynamically typed, meaning variables do not need to be declared with any particular type, and they
can even change type after they have been set.
Python: The Complete Handbook (Ad-Free Edition) 2
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
Built-in Data Types
Text Type: str
Numeric Types: int , float , complex
Sequence Types: list , tuple , range
Mapping Type: dict
Set Types: set , frozenset
Boolean Type: bool
Binary Types: bytes , bytearray , memoryview
Type Checking
You can get the data type of any object by using the type() function.
print(type(5)) # Outputs: <class 'int'>
print(type("Hello")) # Outputs: <class 'str'>
Chapter 3: Control Flow Structures
Python supports the usual logical conditions from mathematics. These conditions can be used in several
ways, most commonly in 'if statements' and loops.
Conditional Statements (If-Else)
Python uses the if , elif , and else keywords to manage conditional flows. Remember that indentation is
mandatory.
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Python: The Complete Handbook (Ad-Free Edition) 3
Loops: While and For
Python has two primitive loop commands: while loops and for loops.
The While Loop
Executes a set of statements as long as a condition is true.
i = 1
while i < 6:
print(i)
i += 1
The For Loop
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Chapter 4: Data Collections
Python collections include Lists, Tuples, Sets, and Dictionaries. Each type has unique characteristics
regarding order, mutability, and indexing.
Collection Type Ordered Changeable (Mutable) Duplicates Allowed
List Yes Yes Yes
Tuple Yes No Yes
Set No No (but elements can be added) No
Dictionary Yes (as of 3.7) Yes No (Keys must be unique)
Python: The Complete Handbook (Ad-Free Edition) 4
Example Usage
# List Definition
my_list = ["apple", "banana", "cherry"]
# Tuple Definition
my_tuple = ("apple", "banana", "cherry")
# Set Definition
my_set = {"apple", "banana", "cherry"}
# Dictionary Definition
my_dict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
Chapter 5: Functions & Scope
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into
a function. A function can return data as a result.
Creating and Calling a Function
In Python a function is defined using the def keyword:
def my_function(fname):
return fname + " Refsnes"
print(my_function("Emil"))
print(my_function("Tobias"))
Lambda Functions
A lambda function is a small anonymous function. A lambda function can take any number of arguments, but
can only have one expression.
x = lambda a, b : a * b
print(x(5, 6)) # Outputs: 30
Python: The Complete Handbook (Ad-Free Edition) 5
Chapter 6: Object-Oriented Programming (OOP)
Python is an object-oriented programming language. Almost everything in Python is an object, with its
properties and methods. A Class is like an object constructor, or a "blueprint" for creating objects.
Creating a Class and Object
To create a class, use the keyword class . The __init__() function is built-in and is always executed when
the class is being initiated.
class Person:
def __init__(self, name, age):
[Link] = name
[Link] = age
def myfunc(self):
print("Hello my name is " + [Link])
p1 = Person("John", 36)
[Link]()
Inheritance
Inheritance allows us to define a class that inherits all the methods and properties from another class. Parent
class is the class being inherited from, also called base class. Child class is the class that inherits from
another class, also called derived class.
class Student(Person):
def __init__(self, name, age, year):
super().__init__(name, age)
[Link] = year
x = Student("Mike", 19, 2021)
print([Link])
Chapter 7: Modules, File Handling & Exceptions
Robust applications require dealing with external modules, reading/writing files, and smoothly handling
unexpected errors without crashing.
Python: The Complete Handbook (Ad-Free Edition) 6
File Handling
The key function for working with files in Python is the open() function. It takes two parameters: filename
and mode ('r' for read, 'w' for write, 'a' for append, 'x' for create).
# Writing to a file
with open("[Link]", "w") as f:
[Link]("Woops! I have deleted the content!")
# Reading from a file
with open("[Link]", "r") as f:
print([Link]())
Exception Handling (Try...Except)
When an error occurs, or exception as we call it, Python will normally stop and generate an error message.
These exceptions can be handled using the try statement:
try:
print(x)
except NameError:
print("Variable x is not defined")
except:
print("Something else went wrong")
finally:
print("The 'try except' is finished")
Python: The Complete Handbook (Ad-Free Edition) 7