Python Handout Real
Python Handout Real
Python is a high-level, interpreted programming language known for its simplicity and
versatility. It is used for web development, data analysis, machine learning, automation,
scripting, and more.
1. Interpreted Language – Python code is executed line by line, making it easier to debug
and fix errors.
2. Object-Oriented – Python supports object-oriented programming (OOP), allowing
developers to create reusable and modular code.
3. Dynamic Typing – Variables in Python do not need explicit data type declarations.
Python automatically assigns the data type based on the value assigned.
4. Extensive Libraries – Python provides a wide range of built-in libraries such as NumPy,
Pandas, Matplotlib, which make development faster.
5. Platform-Independent – Python code can run on different operating systems (Windows,
macOS, Linux) without modification.
1. Running Python Scripts – PowerShell allows you to execute Python scripts directly
from the terminal.
2. Creating Virtual Environments – You can create and manage virtual environments in
Python using PowerShell to avoid conflicts between project dependencies.
1|Page
3. Managing Python Packages – PowerShell can be used to install, update, and manage
packages using pip (Python’s package manager).
2|Page
CHAPTER TWO
Understanding data types is crucial in Python programming, as they define the type of value a
variable can hold.
Variables are used to store values in a program. In Python, variables are created by assigning
values to them.
3|Page
Constants are variables whose values should not change during program execution. By
convention, constants are written in uppercase.
List
4|Page
Tuple
5|Page
6|Page
CHAPTER THREE
Control structures are used to control the flow of execution in a Python program. They include
conditional statements and loops.
Conditional statements allow the program to execute different code blocks based on specific
conditions.
1. If Statement
Executes a block of code if the condition is True.
2. if...else Statement
Executes one block of code if the condition is True, and another block if the condition is
False.
7|Page
3. if...elif...else Statement
Checks multiple conditions.
1. The for loop is used to iterate over a sequence (such as a list, tuple, string, or range).
8|Page
Using the range() function:
While Loop
9|Page
continue – Skips the current iteration and moves to the next.
Nested Loops
You can have loops inside loops. The inner loop will be executed once for each iteration of the
outer loop.
10 | P a g e
CHAPTER FOUR
Functions, libraries, and modules are essential for writing efficient, reusable, and well-
organized Python programs.
A function is a block of reusable code that performs a specific task. Functions help to make
programs more organized, modular, and efficient.
Types of Functions:
11 | P a g e
When creating functions in Python, follow these rules:
12 | P a g e
A recursive function is a function that calls itself. It is used to solve problems that can be
broken down into smaller, similar problems (e.g., calculating factorials, traversing trees).
A module is a Python file that contains functions, classes, and variables. It allows you to
organize your code into separate files and reuse them in other programs.
Built-In Modules:
Python provides many built-in modules that you can use, such as math, random, datetime, etc.
13 | P a g e
14 | P a g e
CHAPTER FIVE
1️⃣ Encapsulation
Bundling data and methods that operate on the data into a single unit (class).
Example: A class Car with attributes speed and color, and methods like start().
2️⃣ Abstraction
3️⃣ Inheritance
Enabling a class (child) to inherit attributes and methods from another class (parent).
Example: A class ElectricCar inheriting from the Car class.
4️⃣ Polymorphism
Class: A blueprint for creating objects. It defines the attributes (data) and methods
(functions) that objects of the class can use.
Object: An instance of a class.
15 | P a g e
Define Methods
Methods are functions defined inside a class. They perform actions using the data
(attributes) of the object.
The first parameter of a method is always self, which refers to the instance of the class.
16 | P a g e
Inheritance allows a class (child) to inherit attributes and methods from another class (parent).
The child class can also override methods of the parent class.
Use the super() function to call the methods of the parent class.
Example: Inheritance
Additional Concepts:
17 | P a g e
CHAPTER SIX
18 | P a g e
DATABASE IN PYTHON
Python provides robust tools to interact with databases, enabling developers to store, retrieve,
and manage data programmatically.
Python supports a wide range of databases using APIs and libraries. Common databases include:
Python libraries like mysql-connector, psycopg2, sqlite3, and pymongo are commonly used to
interact with these databases.
To interact with MySQL databases, Python uses the MySQL Connector library, which allows
you to execute SQL commands directly from Python scripts.
19 | P a g e
20 | P a g e
Python Code to Interact with MySQL Database
21 | P a g e
22 | P a g e
Using SQLite as an Alternative
SQLite is a lightweight, file-based database included with Python. It is ideal for small projects
and local storage.
CHAPTER SEVEN
23 | P a g e
Python is one of the most popular languages for data analysis due to its simplicity and powerful
libraries like NumPy, Pandas, and Matplotlib.
Big Data refers to datasets that are so large and complex that traditional data processing tools
cannot handle them efficiently.
24 | P a g e
25 | P a g e
Dataset and Explain How It Differs from a Database
1️⃣ Dataset:
A dataset is a collection of related data, typically stored in a file (e.g., CSV, Excel, JSON). It is
often used in data analysis.
2️⃣ Database:
A database is a structured collection of data managed by a Database Management System
(DBMS), allowing for querying and modifications.
26 | P a g e
Cleaning Data in Preparation for Analysis
Data cleaning ensures that data is accurate, consistent, and ready for analysis. Common tasks
include:
1️⃣ Removing missing or duplicate values.
2️⃣ Correcting inconsistent data formats.
3️⃣ Handling outliers.
Correlation measures the relationship between two variables. It is used to determine how one
variable changes in response to another.
27 | P a g e
Types of Correlation:
1️⃣ Positive Correlation: Variables increase together.
2️⃣ Negative Correlation: One variable increases while the other decreases.
3️⃣ No Correlation: No relationship between variables.
28 | P a g e