Complete Python Notes
Complete Python Notes
Maurice Otieno is the Founder, CEO, and Director of Tech Savvy Institute. He began his
journey in statistical programming during his Bachelor's degree in Applied Statistics.
Maurice is an expert in Python, Julia, and R Programming. He is also passionate about Artificial
Intelligence.
Welcome! This lesson is your first step into the world of coding. We will learn how computers think
and how we can talk to them using Python.
What is a Program?
A program is just a list of instructions (like a recipe) that tells the computer exactly what to
do.
Computers are powerful, but they are not smart on their own. They need you to tell them exactly
what to do.
🍽️ REAL LIFE EXAMPLE: THE KITCHEN RECIPE
Imagine you want to bake a cake. You cannot just tell a kitchen: "Make cake." You need a
recipe with steps:
A computer program is just a recipe. The computer is the cook that follows your steps
blindly.
In a computer context, if you want to calculate speed, you must tell the computer:
Accept the number for distance.
Accept the number for time.
Divide distance by time.
Show the result on the screen.
2. Human Language vs. Computer Language
We speak "Natural Languages" (English, Swahili, Spanish). Computers speak "Machine Language"
(Zeros and Ones). We need a way to bridge this gap.
Full of ambiguity. Example: "I saw a bat." (Did you see a flying animal or a baseball
bat?)
Humans use context to understand meaning.
Programming Languages
Created by humans to talk to machines.
Every language consists of four main parts. Think of it like learning English grammar.
Alphabet Lexis
The Symbols The Vocabulary
English: A, B, C... English: "Apple", "Run"
Python: a, b, @, #, *, ... Python: "print", "if", "else"
Syntax Semantics
The Grammar The Meaning
Rules on how to arrange words. Does it make sense?
"I store go" is bad syntax. "I ate a shoe" is correct grammar, but
bad semantics (nonsense).
4. High-Level Languages
Computers only understand "Machine Code" (binary: 00101110). This is incredibly hard for humans
to read.
High-Level Languages (like Python) act as a bridge. They look like English, making them easy for
us to write.
Imagine you only speak English, and your friend only speaks Japanese.
You (Programmer): Write "High-Level" instructions (English).
The Computer: Needs "Machine Code" (Japanese).
Python: Is the translator in the middle that converts your English into Japanese so the
computer understands.
Compilation
Translates the whole program at once.
Creates a separate file (executable).
Runs very fast.
Interpretation (Python)
Translates one line at a time.
Runs immediately, no waiting for translation.
Easier to find errors (debugging).
1. Read Code
Python reads one line.
2. Check Grammar
Is the syntax correct?
↓
3. Run It!
Execute the instruction.
4. Next Line
Repeat until finished.
Python is one of the world's most popular programming languages. It is known for being easy to
read and very powerful.
🐍 Fun Fact
It is NOT named after the snake! It was named after the British comedy show "Monty Python's
Flying Circus."
8. Python's Philosophy
When Guido created Python, he had specific goals to make it different from other languages:
1. Easy to Read
Code should look clean. Less clutter, more meaning.
2. Open Source
It's free! Anyone can download it, use it, or even help improve it.
3. Plain English
It uses words like `and`, `not`, and `is` instead of confusing symbols.
Beginner Friendly
It has a simple learning curve. You can write your first program in minutes.
Versatile
You can build websites, analyze data, create AI, or automate boring tasks.
High Demand
Companies everywhere (Google, Netflix, NASA) use Python, meaning lots of job
opportunities.
Practice Questions
Congratulations! You've finished Lesson 1. You now understand that programming is just giving
clear recipes to a computer using a language it understands. Next, we will start writing actual code!
Lesson 2: Data Types
Introduction
In Python, every piece of data is considered an Object. A "variable" is just a name or label attached
to that data.
This concept asks: "Can I change this object after I make it?"
Immutable (Ink)
If you make a mistake, you cannot change it. You must throw the paper away and write a new
one.
Examples: Numbers, Strings, Tuples.
Mutable (Pencil)
You can erase items or add new ones without replacing the whole page.
Examples: Lists, Dictionaries, Sets.
2. The Numbers
<class 'int'>
120
y = 3.14
print(type(y))
print(10 / 3)
<class 'float'>
3.3333333333333335
3. Booleans (bool)
The simplest type. It represents truth values and can only be True or False.
is_valid = True
print(is_valid)
print(type(is_valid))
True
<class 'bool'>
4. Text (Strings)
Strings (str) are used for text. They are Immutable sequences of characters enclosed in quotes.
name = "Alice"
print(name)
print(type(name))
Alice
<class 'str'>
(10, 20)
5.3 Range
A range is a sequence of numbers used mainly in loops. It does not store all numbers in memory.
nums = range(5)
print(list(nums))
[0, 1, 2, 3, 4]
6. Dictionaries (dict)
phonebook = {
"Alice": "555-1234",
"Bob": "555-9876"
}
print(phonebook)
Practice Questions
1. Should you use a List or a Tuple for a collection that needs to be modified?
2. In the "Ink vs. Pencil" analogy, which one represents an Immutable object?
3. What is the main difference between an Integer and a Float?
4. Which data type uses Key-Value pairs?
5. Can you change a character inside a String directly?
Lesson 3: Arithmetic Operators
Introduction
Operators are symbols in Python that perform specific mathematical or logical computations. They
are the "verbs" of programming; they tell the code what action to perform on the data.
The Items: A loaf of bread (60 KES) and a packet of milk (50 KES). These are your
Operands (data).
The Action: You put them both on the counter to calculate the total.
The Operator: The addition symbol (+) is the action of combining the prices.
1. Basic Math
Python acts like a powerful calculator. It uses standard symbols for basic math.
1.1 Addition (+)
Adds two numbers together.
print(50 + 60)
110
55
print(5 * 20)
100
print(20 / 5)
4.0
1.5 Floor Division (//)
Floor division uses two slashes //. It divides numbers but chops off the decimal part, leaving only
the whole integer. It always rounds down to the nearest whole number.
print(105 // 50)
2. Modulus (%)
The Modulus operator is written with the percent symbol %. It does not calculate percentage. It
calculates the Remainder of a division.
Imagine you have a bag of 10 sweets and you want to share them equally among 3 kids.
3. Exponentiation (**)
This is used to raise a number to the power of another. It uses two asterisks **.
# This means 2 * 2 * 2
print(2 ** 3)
Instead of standard BODMAS, Python uses Precedence (priority) and Associativity (direction) to
determine which calculation happens first.
Precedence (Who wins?)
Some operators are "stronger" and grab the numbers near them before others do.
Associativity (Tie-breaker)
If two operators have the same strength, Associativity decides the order. Most operators are Left-
to-Right, but Exponentiation is Right-to-Left.
20
30
512
Practice Questions
1. Which symbol is used for Division in Python, and what type of number does it always return?
2. If you have 10 sweets and 3 children, which operator helps you find out how many sweets are
left over?
3. What is the result of 3 ** 2?
4. In the expression 5 + 2 * 3, which part is calculated first due to Precedence?
5. What is the difference between / and //?
Lesson 4: Strings
Introduction to Strings
A String in Python is a sequence of characters used to represent text. In Python, you can create
strings by enclosing text in single quotes ('...') or double quotes ("...").
# Single quotes
print('Karibu Kenya')
# Double quotes
print("Jambo")
Karibu Kenya
Jambo
This is a string
that spans across
multiple lines.
1. String Properties
Strings have two very important characteristics that dictate how we use them.
1.1 Immutability
Strings are Immutable. This means once you create a string, you cannot change a single character
inside it directly. You must create a whole new string if you want to make changes.
Just like a printed newspaper: if there is a typo, you cannot magically move the ink. You have to print
a new page.
1.2 Ordered Sequence
Strings are Ordered. Every character has a specific position. The character 'A' in "Apple" is always
at the beginning unless you create a new string.
2. Indexing and Slicing
Since strings are ordered, we can access specific characters using their position, known as an
Index.
2.1 Indexing
Python starts counting from 0.
You can also count backwards from the end using negative numbers (starting from -1).
word = "PYTHON"
P
Y
N
2.2 Slicing
Slicing allows you to grab a "chunk" or substring. The syntax is [start:stop].
Note: The stop index is exclusive (it is NOT included in the result).
text = "NAIROBI"
NAI
ROBI
Concatenation is like connecting two railway carriages. You hook "Carriage A" to "Carriage
B" to make one long train.
a = "Ngori"
b = "Sana"
print(a + " " + b)
Ngori Sana
3.2 Repetition (*)
Using the asterisk * repeats a string multiple times.
print("Goal! " * 3)
Often, you want to insert variables directly into a string. The best way to do this in modern Python is
using f-strings.
Simply put the letter f before the opening quote, and put your variables inside curly brackets {}.
When you receive an M-Pesa message, Safaricom doesn't type it manually. They have a
template (string) and they fill in your specific details (variables).
Template: "Confirmed. Ksh {amount} sent to {name}."
name = "Kamau"
balance = 500
# Using an f-string
print(f"Hello {name}, your balance is {balance}.")
Sometimes you need to use special characters that Python normally interprets as code (like
quotes) or invisible characters (like a new line). You use a backslash (\) to "escape" them.
Code Description Example Output
Line 1
\n New Line (Moves text to the next line)
Line 2
\t Tab (Adds a large space) Col1 Col2
\' or \" Quotes (Prints a quote symbol) It's Kenya
\\ Backslash (Prints a literal backslash) \
First Line
Second Line
It's a beautiful day
Practice Questions
Introduction to Variables
A Variable in Python is essentially a name or a label that refers to a value stored in the computer's
memory. It allows you to store data, retrieve it, and manipulate it later.
Unlike some other languages where a variable is a "box" that you put things into, in Python, a
variable is more like a sticky note that you attach to an object.
You can name a variable almost anything, but there are strict rules you must follow so Python
doesn't get confused.
1.1 The Must-Dos
Start with a letter (a-z, A-Z) or an underscore (_).
Contain only letters, numbers, or underscores.
Case Sensitive: Age, age, and AGE are three different variables.
1.2 The Don'ts
NO starting with a number (e.g., 1st_prize is invalid).
NO spaces (e.g., my name is invalid). Use underscores instead (my_name).
NO using Python keywords (words that mean something special to Python like print, if, for,
True).
# Good variable names
first_name = "Kamau"
age_2024 = 25
_secret_code = 1234
We use the equals sign = to create a variable and give it a value. This is called Assignment.
The variable name goes on the Left, and the value goes on the Right.
city = "Nairobi"
population = 4400000
print(city)
print(population)
Nairobi
4400000
3. Reassignment
Variables are called "variable" because their values can vary (change). You can change the value of
a variable simply by assigning a new value to the same name.
⚽ REAL LIFE EXAMPLE: FOOTBALL SCOREBOARD
score = 0
print("Start of match:", score)
score = 1
print("After first goal:", score)
score = score + 1
print("After second goal:", score)
Start of match: 0
After first goal: 1
After second goal: 2
4. Multiple Assignment
Python allows you to assign values to multiple variables in a single line. This keeps your code short
and clean.
# Assigning different values to different variables
x, y, z = 10, 20, 30
print(x)
print(y)
print(z)
10
20
30
100
100
5. Constants
Sometimes you have a value that should NEVER change during the program, like the value of Pi
(3.142) or the M-Pesa Transaction Fee.
Python doesn't strictly prevent you from changing these, but by convention, we use ALL CAPITAL
LETTERS to shout to other developers: "DO NOT CHANGE THIS!"
PI = 3.14159
MAX_LOGIN_ATTEMPTS = 3
MPESA_PAYBILL = 522522
Introduction
Data structures are simply containers that allow us to store, organize, and manage multiple items
together in different ways. Rather than having a separate variable for every single item, we group
them.
List: A Shopping Bag. You toss items in, take them out, and rearrange them anytime.
(Mutable, Ordered)
Tuple: A Sealed Delivery Box. The items inside are fixed; you can't change them
without breaking the box. (Immutable, Ordered)
Dictionary: A Phonebook. You look up a specific name (Key) to find their number
(Value). (Key-Value Pairs)
Set: A Sack of Potatoes. It doesn't matter which potato went in first (Unordered), and
you can't have the exact same physical potato twice (Unique).
1. Lists
A List is an ordered collection of items. It is Mutable, meaning you can add, remove, or change
items after creating it.
Syntax: Uses square brackets [].
1.1 Examples of Lists
Nairobi
Nakuru
['Mombasa', 'Kisumu']
Adding Items
Use .append() to add to the end, or .insert() to add at a specific spot.
print(fruits)
print(cart)
print("Popped item:", last_item)
['Milk']
Popped item: Sugar
A Tuple is like a list, but Immutable. Once created, it cannot be changed. It is faster and safer for
fixed data.
Syntax: Uses parentheses ().
2.1 Examples of Tuples
Mon
Wed
Unpacking
You can assign tuple values to variables in one line.
coords = (10, 20)
x, y = coords
print("X is:", x)
print("Y is:", y)
X is: 10
Y is: 20
Immutability Check
Trying to change a tuple creates an error.
fixed_data = (1, 2, 3)
# fixed_data[0] = 5 <-- TypeError: 'tuple' object does not support item assig
3. Dictionaries
A Dictionary stores data in Key-Value pairs. It allows you to look up values using a unique Key
rather than an index number.
Syntax: Uses curly brackets {} with key: value pairs.
3.1 Examples of Dictionaries
# 1. Student Profile
student = {"name": "Otieno", "age": 22, "course": "IT"}
# 2. Matatu Details
matatu = {
"route": "125",
"sacca": "Super Metro",
"driver": "Njoroge",
"fare": 100
}
# 3. M-Pesa Transaction
transaction = {
"code": "SDF342...",
"amount": 500,
"recipient": "KPLC Prepaid",
"balance": 2400
}
print(matatu["route"])
print([Link]("sacca"))
125
Super Metro
print(profile)
print(car)
{'brand': 'Toyota'}
dict_keys(['a', 'b'])
dict_values([1, 2])
4. Sets
A Set is an unordered collection of Unique items. It is great for removing duplicates and doing math
logic (like finding common items).
Syntax: Uses curly brackets {}.
4.1 Examples of Sets
# 4. Lotto Numbers
lotto = {4, 12, 33, 45, 1}
# Add an item
[Link]("Blue")
# Remove an item
[Link]("Red")
print(colors)
{'Blue', 'Green'}
{'Kamau', 'Wanjiku'}
{'Njoroge', 'Kamau', 'Wanjiku', 'Ochieng'}
{'Ochieng'}
Practice Questions
1. Which data structure would you use to store a list of students where you need to frequently add
and remove names?
2. How do you add the item "Sugar" to the end of a list named shopping_cart?
3. If you have a tuple coords = (10, 20), what happens if you try to run coords[0] = 50?
4. You have a list with duplicates: [1, 2, 2, 3]. What is the fastest way to remove the
duplicates?
5. In the dictionary car = {"model": "Toyota", "year": 2020}, which code retrieves the value
2020?
Lesson 7: Advanced Operators
Introduction
We've already looked at Arithmetic operators (+, -, *) which perform math. Now we will look at
operators that help us make decisions, compare values, and check relationships between data.
These operators typically answer questions with a Boolean result: True or False.
1. Comparison Operators
Comparison operators compare two values. They act like a question: "Is this equal to that?" or "Is
this bigger than that?"
# 1. Numeric Comparison
# Can I afford lunch? (Is balance greater or equal to cost?)
can_afford = balance >= cost_of_lunch
print(f"Can afford lunch: {can_afford}")
# 3. Chained Comparison
age = 20
# Is age between 18 and 35?
is_youth = 18 <= age <= 35
print(f"Is youth: {is_youth}")
2. Logical Operators
Logical operators allow us to combine multiple conditions. They are used when you need to check
more than one thing at a time.
👮 REAL LIFE EXAMPLE: CLUB ENTRY
AND: You need an ID AND the Entrance Fee. If you miss one, you don't enter.
OR: You can pay via Cash OR M-Pesa. If you have either one, you are good.
NOT: You must NOT be underage.
has_id = True
has_fee = False
False
True
True
3. Identity Operators
Identity operators (is, is not) check if two variables refer to the exact same object in memory,
not just if they have the same value.
Equality (==): They both wear the exact same shirt. Are the shirts equal in appearance?
Yes.
Identity (is): Is Kamau actually Njoroge? No. They are two different people who look
alike.
Identity (is): Does "President of Kenya" refer to the same person as "William Ruto"
(currently)? Yes. They are the same entity.
# Comparing Values
print(list_a == list_b) # True, they look the same
# Comparing Identity
print(list_a is list_b) # False, they are two different lists in memory
print(list_a is list_c) # True, they are the exact same list object
True
False
True
4. Membership Operators
Membership operators (in, not in) check if a specific item exists inside a sequence (like a list,
tuple, or string).
🥘 REAL LIFE EXAMPLE: RECIPE CHECK
True
True
True
Practice Questions
Introduction to Conditionals
Computer programs usually read code line-by-line, from top to bottom. But real life isn't a straight
line; we make decisions constantly. Conditional Statements allow our programs to make
decisions too.
They allow the code to "branch" and execute different blocks of code depending on whether a
specific condition is True or False.
Imagine you are at the bus station (stage) trying to get home.
This is the simplest form of decision making. It says: "If this condition is true, do this task. If not, just
ignore it and move on."
[Image of Python if statement flowchart]
Syntax
Notice the colon (:) at the end of the line and the Indentation (space) for the block of code
underneath.
⚠️ Important: Indentation
Python relies on whitespace. The code inside the if block MUST be indented (usually 4 spaces
or 1 tab). If you forget the indentation, you will get an error!
age = 20
Often, you have two choices. If the condition is true, do Option A. If it is false, do Option B. You
never do both.
Life is rarely just "Yes" or "No". Sometimes we have many options. We use elif (short for "else if")
to check multiple conditions in a sequence.
Python checks them from top to bottom. As soon as it finds ONE true condition, it executes that
block and ignores the rest.
Grade: B
4. Nested Conditionals
You can put an if statement inside another if statement. This is called "Nesting". It's like having a
decision within a decision.
if entered_pin == correct_pin:
print("PIN Accepted.")
# Inner Condition (Nested)
if balance >= withdraw_amount:
print("Dispensing Cash...")
else:
print("Insufficient Funds.")
else:
print("Wrong PIN.")
PIN Accepted.
Insufficient Funds.
You can combine multiple conditions in a single if statement using Logical Operators.
5.1 Using `and`
All conditions must be true.
# Payment Methods
has_cash = False
has_mpesa = True
if has_cash or has_mpesa:
print("Payment accepted.")
else:
print("You cannot pay.")
Practice Questions
1. Which keyword is used to check multiple conditions after the first if fails?
2. What happens if you forget to indent the code under an if statement?
3. In an if...elif...else chain, how many blocks of code will actually run?
4. Write a logic condition for: "If user is over 18 AND has a ticket".
5. What symbol must appear at the end of every if, elif, and else line?
Lesson 9: Loops
Introduction to Loops
In programming, we often need to repeat a specific task multiple times. Instead of copying and
pasting the same code 100 times, we use Loops.
Loops allow a block of code to execute repeatedly until a specific condition is met.
A while loop keeps running as long as a certain condition remains True. It checks the condition
before every iteration.
Warning: You must ensure the condition eventually becomes False, otherwise the loop will run
forever (Infinite Loop)!
"While the number of empty seats is greater than 0, keep calling for passengers."
Once seats == 0, the loop stops (and the matatu leaves).
Example 1: Basic Countdown
count = 5
print("Blast off!")
Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
Blast off!
Example 2: Saving Money
savings = 0
target = 500
A for loop is used to iterate over a sequence (like a list, tuple, string, or range). It runs once for
each item in the collection.
word = "KENYA"
K
E
N
Y
A
The range() function allows us to loop a specific number of times. It generates a sequence of
numbers.
range(5): 0, 1, 2, 3, 4 (Stops before 5)
range(2, 6): 2, 3, 4, 5 (Start at 2, stop before 6)
range(0, 10, 2): 0, 2, 4, 6, 8 (Step by 2)
# Print numbers 0 to 4
print("--- Standard Range ---")
for i in range(5):
print(i)
Sometimes you need to interrupt the normal flow of a loop. We use break and continue for this.
4.1 `break` (Stop Immediately)
The break statement terminates the loop entirely.
🛑 REAL LIFE EXAMPLE: SEARCHING FOR KEYS
You are looking through drawers for your keys. Loop: Open drawer, check inside.
Break: Once you find the keys in the 2nd drawer, you stop looking. You don't open the
remaining drawers.
Even number: 2
Even number: 4
5. Nested Loops
A nested loop is a loop inside another loop. The "inner loop" finishes all of its iterations for every
single iteration of the "outer loop".
Practice Questions
1. Which loop is best used when you know exactly how many times you want to iterate (e.g.,
iterating through a list)?
2. What happens if the condition in a while loop never becomes False?
3. Which keyword would you use to exit a loop completely when a specific condition is met?
4. If you have `range(1, 5)`, what is the last number that will be printed?
5. In a nested loop (Loop A inside Loop B), which loop completes its full cycle first?
Lesson 10: Introduction to NumPy
Introduction to NumPy
So far, we have used Python Lists to store groups of data. But when dealing with Data Science, AI,
or Engineering, lists can be too slow and use too much memory.
Enter NumPy (Numerical Python). It is a library (a toolbox of code written by others) that gives us a
new data structure called the Array.
Before we use it, we must install it and "import" it into our code.
# 2. Import in Python
import numpy as np
Why learn a new thing if we already have Lists? Because NumPy Arrays are faster and smarter for
numbers.
Key Differences
Speed: NumPy is up to 50x faster than Lists.
Data Type: Lists can hold anything (Int, String, Bool mixed). Arrays must hold ONE data type
(e.g., all Integers).
2. Creating Arrays
The most common way to create an array is by converting a regular Python list.
print(arr)
print(type(arr))
[1 2 3 4 5]
<class '[Link]'>
Automatic Arrays
NumPy can also create arrays for you automatically, which is useful for generating data.
# 1. Array of Zeros
print([Link](5))
# 2. Array of Ones
print([Link](3))
[0. 0. 0. 0. 0.]
[1. 1. 1.]
[0 2 4 6 8]
This is why we love NumPy. If you want to multiply every number in a list by 2, standard Python
requires a loop (slow). NumPy can do it all at once (fast).
Python Loop: Walking to Student 1 ("Stand up"), then Student 2 ("Stand up"), then
Student 3...
NumPy Vectorization: Shouting to the whole class "EVERYONE STAND UP!"
arr = [Link]([10, 20, 30])
# Multiply everyone by 2
print(arr * 2)
# Add 5 to everyone
print(arr + 5)
[20 40 60]
[15 25 35]
Accessing data in NumPy is similar to Lists, but with extra power for 2D arrays.
1-D Slicing
Works exactly like Python lists: [start:stop].
2-D Slicing
The syntax is [row, column].
matrix = [Link]([
[10, 20, 30], # Row 0
[40, 50, 60] # Row 1
])
6. Basic Statistics
1. What is the alias we typically use when importing numpy? (e.g. import numpy as ???)
2. Can a NumPy array hold both Strings and Integers at the same time?
3. If you have a 2D array (matrix), what does [Link] return?
4. In the analogy, is a NumPy array more like a "Shopping Bag" or a "Tray of Eggs"?
5. What NumPy function would you use to create an array of five zeros?
Lesson 11: Introduction to Pandas
Introduction to Pandas
Pandas is the most popular library in Python for Data Analysis. If you have ever worked with
Microsoft Excel or Google Sheets, then Pandas is essentially "Excel for Python".
It allows us to load data, clean it, manipulate it, and analyze it efficiently.
First, we need to install and import it.
# 2. Import in Python
import pandas as pd
Series (1D)
Think of a Single Column in Excel. For example, just a list of "Prices" for different items.
DataFrame (2D)
Think of the Whole Sheet. It has rows and multiple columns (e.g., "Item Name", "Price",
"Quantity"). A DataFrame is essentially a collection of Series stuck together.
# Convert to a Series
s = [Link](marks)
print(s)
0 80
1 90
2 75
dtype: int64
df = [Link](data)
print(df)
2. Reading Data
Usually, you don't type data manually. You load it from a file, like a CSV (Comma Separated Values)
or Excel file.
Imagine you download your M-Pesa statement as a file named [Link]. Pandas can
open this file instantly so you can calculate your total spending.
# Reading a CSV file
# df = pd.read_csv("mpesa_statement.csv")
3. Inspecting Data
When you load a huge file with 10,000 rows, you can't print the whole thing. You need tools to
"peek" at the data.
[Link](): Shows the first 5 rows (The header).
[Link](): Shows the last 5 rows (The footer).
[Link](): Summarizes the data types (Are they numbers? Text?) and checks for missing
values.
[Link]: Tells you the size (Rows, Columns).
print("--- HEAD (Top 2) ---")
print([Link](2))
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Student 3 non-null object
1 Age 3 non-null int64
2 Course 3 non-null object
dtypes: int64(1), object(2)
None
Sometimes you only want specific columns or rows that match a condition.
4.1 Selecting a Column
This works like a dictionary key.
# Select only the 'Student' column
print(df["Student"])
0 Kamau
1 Wanjiku
2 Otieno
Name: Student, dtype: object
5. Basic Statistics
Pandas makes math easy. You can calculate the average, sum, or count of a column instantly.
# Average Age
print("Mean Age:", df["Age"].mean())
Practice Questions
Introduction to Matplotlib
In Data Science, raw numbers in a table can be boring and hard to understand. Matplotlib is a
library that allows us to turn data into beautiful graphs and charts.
If Pandas is "Excel for Python," then Matplotlib is the tool for drawing charts on a whiteboard.
Imagine reading a list of every goal scored in the English Premier League. It's just a long list
of text.
Now imagine seeing a Bar Chart showing "Top Scorers." You can instantly see who is
leading without reading every line. That is the power of visualization.
# 2. Import in Python
# We mostly use the 'pyplot' module
import [Link] as plt
The simplest plot is a Line Plot. It is perfect for showing trends over time (like temperature or stock
prices).
You essentially tell Python: "Here are the X values (horizontal), and here are the Y values (vertical)."
[Image of simple line plot example]
# Sample Data: Days vs Temperature in Nairobi
days = ["Mon", "Tue", "Wed", "Thu", "Fri"]
temp = [22, 24, 19, 23, 25]
2. Decorating Plots
A graph without labels is confusing. Matplotlib allows us to add titles, labels, and legends to make
our chart professional.
# Adding details
[Link]("Weekly Temperature in Nairobi")
[Link]("Day of the Week")
[Link]("Temperature (Celsius)")
[Link]()
Bar charts are best for comparing categories. For example, comparing votes for different
candidates or sales of different fruits.
You have three candidates: A, B, and C. You want to show who got the most votes. A bar
chart makes the tallest bar (winner) obvious immediately.
[Link]()
4. Scatter Plots
Scatter plots use dots to show the relationship (correlation) between two numerical variables.
They don't connect the dots with lines.
Does studying more actually get you higher marks? If you plot "Hours Studied" on the X-axis
and "Exam Score" on the Y-axis, you might see a pattern (trend) going up.
hours_studied = [1, 2, 3, 4, 5, 6, 7]
exam_score = [50, 55, 65, 70, 85, 90, 95]
[Link]()
Practice Questions