Python coding
Basic_Python_Sets.pdf
Basic Python Course
Data Structures: Sets
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
1. Understanding Sets
A Set is a collection used to store multiple unique items in a single variable.
Sets are written using curly brackets
Example:
---
Characteristics of Sets
- Unordered → Items do not keep a fixed order
- Unindexed → No index numbers
- No Duplicates → Repeated values are automatically removed
- Mutable → You can add or remove items
---
Duplicate Values are Ignored
Output:
Even though
---
2. Modifying a Set
Adding One Item
Use
---
Adding Multiple Items
Use
---
Removing Items
Using
Important:
If the item does not exist, Python raises an error.
---
Using
Difference:
---
3. Set Operations (Logic)
Sets support mathematical operations.
These operations are extremely useful in programming and data analysis.
---
Union
Combines all unique values from both sets.
Method:
Operator:
Example:
---
Intersection
Returns only common values.
Method:
Operator:
Example:
Output:
---
Difference
Returns values in the first set but not in the second set.
Method:
Operator:
---
Symmetric Difference
Returns values that are NOT shared between both sets.
Method:
Operator:
---
Boolean Note
In Python sets:
So Python treats them as duplicate values.
Example:
Output:
---
4. Why Use Sets?
Removing duplicates instantly.
Example:
Output:
---
Real-World Uses of Sets
- Removing duplicate usernames
- Finding common interests between users
- Comparing datasets
- Membership testing
---
Practice Exercises
1. Disjoint Check
Write a program to check if two sets have zero common items without using built-in functions.
---
2. Unique Counter
Given a list of student names, count how many unique names exist.
---
3. Symmetric Difference
Use the
Data Structures: Sets
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
1. Understanding Sets
A Set is a collection used to store multiple unique items in a single variable.
Sets are written using curly brackets
{}.Example:
thisset = {"apple", "banana", "cherry"}
print(thisset)---
Characteristics of Sets
- Unordered → Items do not keep a fixed order
- Unindexed → No index numbers
- No Duplicates → Repeated values are automatically removed
- Mutable → You can add or remove items
---
Duplicate Values are Ignored
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset)Output:
{'apple', 'banana', 'cherry'}Even though
"apple" appears twice, it is stored only once.---
2. Modifying a Set
Adding One Item
Use
.add():thisset = {"apple", "banana"}
thisset.add("orange")
print(thisset)---
Adding Multiple Items
Use
.update():thisset.update(["mango", "kiwi"])
print(thisset)
---
Removing Items
Using
remove():thisset.remove("banana")Important:
If the item does not exist, Python raises an error.
---
Using
discard()thisset.discard("banana")Difference:
discard() does not raise an error if the item is missing.---
3. Set Operations (Logic)
Sets support mathematical operations.
These operations are extremely useful in programming and data analysis.
---
Union
Combines all unique values from both sets.
Method:
set1.union(set2)
Operator:
set1 | set2
Example:
set1 = {"a", "b"}
set2 = {1, 2}
print(set1 | set2)---
Intersection
Returns only common values.
Method:
set1.intersection(set2)
Operator:
set1 & set2
Example:
set1 = {"apple", "banana"}
set2 = {"banana", "orange"}
print(set1 & set2)Output:
{'banana'}---
Difference
Returns values in the first set but not in the second set.
Method:
set1.difference(set2)
Operator:
set1 - set2
---
Symmetric Difference
Returns values that are NOT shared between both sets.
Method:
set1.symmetric_difference(set2)
Operator:
set1 ^ set2
---
Boolean Note
In Python sets:
True == 1
False == 0
So Python treats them as duplicate values.
Example:
myset = {True, 1, False, 0}
print(myset)Output:
{False, True}---
4. Why Use Sets?
Removing duplicates instantly.
Example:
names = ["Ali", "Sara", "Ali", "Hana", "Sara"]
unique_names = set(names)
print(unique_names)
Output:
{'Ali', 'Sara', 'Hana'}---
Real-World Uses of Sets
- Removing duplicate usernames
- Finding common interests between users
- Comparing datasets
- Membership testing
---
Practice Exercises
1. Disjoint Check
Write a program to check if two sets have zero common items without using built-in functions.
---
2. Unique Counter
Given a list of student names, count how many unique names exist.
---
3. Symmetric Difference
Use the
^ operator to find items that exist in either Set A or Set B, but not both.
Python coding
Basic_Python_Dictionaries.pdf
Basic Python Course
Data Structures: Dictionaries
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
1. Understanding Dictionaries
Dictionaries are used to store data in key:value pairs.
Think of a real dictionary:
- Word → Key
- Meaning → Value
Example:
Output:
---
Characteristics of Dictionaries
- Ordered → Python keeps the insertion order
- Changeable → Items can be updated or removed
- No Duplicate Keys → Duplicate keys overwrite old values
Example:
Output:
---
2. Managing Data
Accessing Items
Using square brackets:
Using `.get()
print(thisdict.get("year"))
python
thisdict["year"] = 2020
python
thisdict["color"] = "red"
python
print(thisdict)
python
for x in thisdict:
print(x)
python
for x in thisdict.values():
print(x)
python
for x, y in thisdict.items():
print(x, y)
python
myfamily = {
"child1": {
"name": "Emil",
"year": 2004
},
"child2": {
"name": "Tobias",
"year": 2007
}
}
python
print(myfamily["child2"]["name"])
python
Tobias
python
x = thisdict.keys()
thisdict["color"] = "white"
print(x)
python
thisdict.pop("model")
python
thisdict.clear()
python
thisdict.update({"color": "blue"})
python
if "model" in thisdict:
print("Yes, model exists")
Data Structures: Dictionaries
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
1. Understanding Dictionaries
Dictionaries are used to store data in key:value pairs.
Think of a real dictionary:
- Word → Key
- Meaning → Value
Example:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])Output:
Ford
---
Characteristics of Dictionaries
- Ordered → Python keeps the insertion order
- Changeable → Items can be updated or removed
- No Duplicate Keys → Duplicate keys overwrite old values
Example:
thisdict = {
"brand": "Ford",
"brand": "BMW"
}
print(thisdict)Output:
{'brand': 'BMW'}---
2. Managing Data
Accessing Items
Using square brackets:
print(thisdict["model"])
Using `.get()
:
``pythonprint(thisdict.get("year"))
Difference:
`.get()` avoids errors if the key does not exist.
---
Changing and Adding Items
Update an existing value:
python
thisdict["year"] = 2020
Add a new key:value pair:
python
thisdict["color"] = "red"
Example:
python
print(thisdict)
---
3. Looping Through Dictionaries
Loop Through Keys
python
for x in thisdict:
print(x)
---
Loop Through Values
python
for x in thisdict.values():
print(x)
---
Loop Through Both Keys and Values
python
for x, y in thisdict.items():
print(x, y)
---
4. Nested Dictionaries
A dictionary can contain other dictionaries.
This is useful for:
- User profiles
- Databases
- Structured data
Example:
python
myfamily = {
"child1": {
"name": "Emil",
"year": 2004
},
"child2": {
"name": "Tobias",
"year": 2007
}
}
Access nested data:
python
print(myfamily["child2"]["name"])
Output:
python
Tobias
---
5. View Objects (Added Value)
The `.keys()` and `.values()` methods return dynamic view objects.
This means if the dictionary changes, the views update automatically.
Example:
python
x = thisdict.keys()
thisdict["color"] = "white"
print(x)
---
Dictionary Methods Summary
---
.pop(key)
Removes a specific item.
python
thisdict.pop("model")
---
.clear()
Removes all items.
python
thisdict.clear()
---
.update()
Updates the dictionary with another dictionary.
python
thisdict.update({"color": "blue"})
---
Real-World Uses of Dictionaries
- User account information
- Product databases
- API responses
- Student records
- Configuration settings
---
Practice Exercises
1. Inventory Update
Create a dictionary of store items and prices. Increase the price of one item by 10%.
---
2. Nested Access
From the `myfamily` dictionary, print only the name of `"child2"`.
---
3. Key Checker
Check if `"model"` exists inside `thisdict`.
Example:
python
if "model" in thisdict:
print("Yes, model exists")
`
Python coding
Basic_Python_NumPy_Libraries.pdf
Basic Python Course
Introduction to NumPy
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
1. What is NumPy?
NumPy stands for Numerical Python.
It is a powerful Python library used for:
- Numerical Computing
- Arrays and Matrices
- Scientific Calculations
- Data Analysis
- Artificial Intelligence and Machine Learning
NumPy is one of the most important libraries in the Python ecosystem.
---
Why NumPy is Important
Speed:
NumPy arrays are much faster than normal Python lists.
Memory Efficiency:
Arrays use less memory compared to standard Python lists.
Advanced Mathematics:
Supports matrix operations, statistics, linear algebra, and more.
Foundation of AI:
Libraries like Pandas, TensorFlow, and Scikit-learn rely heavily on NumPy.
---
2. Importing NumPy
Before using NumPy, it must be imported.
`np` is simply a shorter alias for NumPy.
---
3. Creating Arrays
Arrays are the core structure of NumPy.
Output:
---
4. Array Dimensions
NumPy supports multiple dimensions.
| Dimension | Description |
|-----------|-------------|
| 0-D | Single value |
| 1-D | Simple array |
| 2-D | Matrix/Table |
| 3-D | Collection of matrices |
---
2-D Array Example
---
Checking Dimensions
Use
Output:
---
5. Array Shape
The
Output:
Meaning:
- 2 rows
- 3 columns
---
6. Slicing Arrays
Slicing works similarly to Python lists.
Example:
Explanation:
- Row index → 0
- Column indexes → 1 to 2
Output:
---
7. Reshaping Arrays
Reshaping changes the structure without changing the data.
Output:
---
8. Searching Arrays
Use
Output:
Meaning:
The value
---
9. Vectorized Operations
NumPy can perform operations on entire arrays without loops.
Output:
Every element increases by 1 instantly.
---
10. Why NumPy is Powerful
Without NumPy:
With NumPy:
Cleaner, faster, and more efficient.
---
Real-World Uses of NumPy
- Artificial Intelligence
- Machine Learning
- Data Science
- Image Processing
- Scientific Research
- Game Development
---
Practice Exercises
1. Create a 5-D array using
2. Find indexes of all even numbers:
3. Convert a 1-D array with 8 elements into a 2x4 matrix using
Introduction to NumPy
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
1. What is NumPy?
NumPy stands for Numerical Python.
It is a powerful Python library used for:
- Numerical Computing
- Arrays and Matrices
- Scientific Calculations
- Data Analysis
- Artificial Intelligence and Machine Learning
NumPy is one of the most important libraries in the Python ecosystem.
---
Why NumPy is Important
Speed:
NumPy arrays are much faster than normal Python lists.
Memory Efficiency:
Arrays use less memory compared to standard Python lists.
Advanced Mathematics:
Supports matrix operations, statistics, linear algebra, and more.
Foundation of AI:
Libraries like Pandas, TensorFlow, and Scikit-learn rely heavily on NumPy.
---
2. Importing NumPy
Before using NumPy, it must be imported.
import numpy as np
`np` is simply a shorter alias for NumPy.
---
3. Creating Arrays
Arrays are the core structure of NumPy.
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
Output:
[1 2 3 4 5]
---
4. Array Dimensions
NumPy supports multiple dimensions.
| Dimension | Description |
|-----------|-------------|
| 0-D | Single value |
| 1-D | Simple array |
| 2-D | Matrix/Table |
| 3-D | Collection of matrices |
---
2-D Array Example
arr2 = np.array([
[1, 2, 3],
[4, 5, 6]
])
print(arr2)
---
Checking Dimensions
Use
.ndim:print(arr2.ndim)
Output:
2
---
5. Array Shape
The
.shape attribute returns the size of each dimension.print(arr2.shape)
Output:
(2, 3)
Meaning:
- 2 rows
- 3 columns
---
6. Slicing Arrays
Slicing works similarly to Python lists.
Example:
print(arr2[0, 1:3])
Explanation:
- Row index → 0
- Column indexes → 1 to 2
Output:
[2 3]
---
7. Reshaping Arrays
Reshaping changes the structure without changing the data.
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
newarr = arr.reshape(2, 4)
print(newarr)
Output:
[[1 2 3 4]
[5 6 7 8]]
---
8. Searching Arrays
Use
np.where() to find indexes.arr = np.array([1, 2, 3, 4, 5, 6])
x = np.where(arr == 4)
print(x)
Output:
(array([3]),)
Meaning:
The value
4 exists at index 3.---
9. Vectorized Operations
NumPy can perform operations on entire arrays without loops.
arr = np.array([1, 2, 3])
arr = arr + 1
print(arr)
Output:
[2 3 4]
Every element increases by 1 instantly.
---
10. Why NumPy is Powerful
Without NumPy:
numbers = [1, 2, 3]
new_numbers = []
for x in numbers:
new_numbers.append(x + 1)
print(new_numbers)
With NumPy:
arr = np.array([1, 2, 3])
print(arr + 1)
Cleaner, faster, and more efficient.
---
Real-World Uses of NumPy
- Artificial Intelligence
- Machine Learning
- Data Science
- Image Processing
- Scientific Research
- Game Development
---
Practice Exercises
1. Create a 5-D array using
ndmin.2. Find indexes of all even numbers:
arr = np.array([1, 2, 3, 4, 5, 6])
3. Convert a 1-D array with 8 elements into a 2x4 matrix using
.reshape().
Python coding
Basic_Python_NumPy_Part2.pdf
Basic Python Course
NumPy Part 2: Randomness & Math Functions
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
1. Random Numbers in NumPy
In programming, random numbers are usually pseudo-random numbers.
This means:
- They are generated using algorithms
- They appear random
- But they are not truly random like natural randomness
NumPy provides powerful tools for generating random data.
Import random module:
---
Random Integers
Use
Output example:
This generates a random integer from:
- 0 → 99
---
Random Floats
Use
Example output:
---
Random Choice
Use
Possible output:
---
2. NumPy Arithmetic (Universal Functions)
NumPy uses Universal Functions (
These operations are:
- Faster than loops
- Cleaner to write
- Optimized internally
---
Example Arrays
---
Addition
Output:
---
Subtraction
---
Multiplication
---
Division
---
3. Rounding Decimal Numbers
NumPy provides multiple functions for handling decimals.
---
trunc() / fix()
Removes decimal values toward zero.
Output:
---
around()
Rounds to the nearest decimal place.
Output:
---
floor()
Rounds downward.
Output:
---
ceil()
Rounds upward.
Output:
---
4. Trigonometry and Logarithms
NumPy can perform advanced mathematics easily.
---
Sine Function
Output:
---
Logarithm Base 2
Output:
Explanation:
- log₂(1) = 0
- log₂(2) = 1
- log₂(4) = 2
- log₂(8) = 3
---
5. Data Distribution
In Machine Learning and AI, we often need data that follows a distribution.
One of the most common is the Normal Distribution (Bell Curve).
---
Normal Distribution Example
This generates:
- Random numbers
- Following a Gaussian distribution
- Inside a 2x3 matrix
---
Why Distributions Matter
Distributions are heavily used in:
- Artificial Intelligence
- Machine Learning
- Statistics
- Simulations
- Data Science
---
Practice Exercises
1. Random Choice
Create an array of 5 names and use
---
2. Ceiling & Floor
Create this array:
Print:
- Floor values
- Ceiling values
---
3. Random Matrix
Generate a 3x5 matrix of random integers between 0 and 50.
NumPy Part 2: Randomness & Math Functions
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
1. Random Numbers in NumPy
In programming, random numbers are usually pseudo-random numbers.
This means:
- They are generated using algorithms
- They appear random
- But they are not truly random like natural randomness
NumPy provides powerful tools for generating random data.
Import random module:
from numpy import random
---
Random Integers
Use
randint() to generate random integers.from numpy import random
x = random.randint(100)
print(x)
Output example:
57
This generates a random integer from:
- 0 → 99
---
Random Floats
Use
rand() to generate random decimal values between 0 and 1.y = random.rand(5)
print(y)
Example output:
[0.24 0.81 0.55 0.91 0.13]
---
Random Choice
Use
choice() to randomly select values from an array.arr = [3, 5, 7, 9]
x = random.choice(arr)
print(x)
Possible output:
7
---
2. NumPy Arithmetic (Universal Functions)
NumPy uses Universal Functions (
ufuncs) to perform fast mathematical operations on arrays.These operations are:
- Faster than loops
- Cleaner to write
- Optimized internally
---
Example Arrays
import numpy as np
arr1 = np.array([10, 20, 30])
arr2 = np.array([3, 5, 7])
---
Addition
add_arr = np.add(arr1, arr2)
print(add_arr)
Output:
[13 25 37]
---
Subtraction
sub_arr = np.subtract(arr1, arr2)
print(sub_arr)
---
Multiplication
mult_arr = np.multiply(arr1, arr2)
print(mult_arr)
---
Division
div_arr = np.divide(arr1, arr2)
print(div_arr)
---
3. Rounding Decimal Numbers
NumPy provides multiple functions for handling decimals.
---
trunc() / fix()
Removes decimal values toward zero.
arr = np.trunc([-3.7, 3.7])
print(arr)
Output:
[-3. 3.]
---
around()
Rounds to the nearest decimal place.
arr = np.around(3.1666, 2)
print(arr)
Output:
3.17
---
floor()
Rounds downward.
arr = np.floor([3.7])
print(arr)
Output:
[3.]
---
ceil()
Rounds upward.
arr = np.ceil([3.2])
print(arr)
Output:
[4.]
---
4. Trigonometry and Logarithms
NumPy can perform advanced mathematics easily.
---
Sine Function
import numpy as np
val = np.sin(np.pi / 2)
print(val)
Output:
1.0
---
Logarithm Base 2
arr = np.array([1, 2, 4, 8])
print(np.log2(arr))
Output:
[0. 1. 2. 3.]
Explanation:
- log₂(1) = 0
- log₂(2) = 1
- log₂(4) = 2
- log₂(8) = 3
---
5. Data Distribution
In Machine Learning and AI, we often need data that follows a distribution.
One of the most common is the Normal Distribution (Bell Curve).
---
Normal Distribution Example
from numpy import random
x = random.normal(size=(2, 3))
print(x)
This generates:
- Random numbers
- Following a Gaussian distribution
- Inside a 2x3 matrix
---
Why Distributions Matter
Distributions are heavily used in:
- Artificial Intelligence
- Machine Learning
- Statistics
- Simulations
- Data Science
---
Practice Exercises
1. Random Choice
Create an array of 5 names and use
random.choice() to select a winner.---
2. Ceiling & Floor
Create this array:
arr = np.array([1.2, 2.8, 3.5])
Print:
- Floor values
- Ceiling values
---
3. Random Matrix
Generate a 3x5 matrix of random integers between 0 and 50.
Python coding
Basic_Python_CSV_JSON_Handling.pdf
Basic Python Course
Data Analysis with Pandas
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
1. Introduction to Pandas
Pandas is one of the most powerful and popular Python libraries for data analysis.
It helps programmers:
- Analyze data
- Clean messy datasets
- Organize information
- Discover patterns
Pandas is built on top of NumPy, making it fast and efficient.
---
Why Use Pandas?
Data Cleaning:
Fix missing or incorrect values.
Data Exploration:
Understand what exists inside your dataset.
Speed:
Handles large files efficiently.
Real-World Usage:
Used heavily in:
- Data Science
- Artificial Intelligence
- Business Analytics
- Machine Learning
---
2. Importing Pandas
Before using Pandas:
`pd` is simply a short alias for Pandas.
---
3. Creating a DataFrame
A DataFrame is like a spreadsheet or table.
Example:
Output:
---
4. Series vs DataFrame
Pandas mainly uses two structures.
---
Series
A one-dimensional structure.
Example:
---
DataFrame
A multi-dimensional table.
Example:
Think of:
- Series → One column
- DataFrame → Entire spreadsheet
---
5. Reading CSV Files
CSV stands for Comma Separated Values.
Most real-world datasets are stored as CSV files.
Example:
---
Useful Tip
By default, Pandas only shows part of large datasets.
Increase visible rows:
---
6. Inspecting Data
---
head()
Shows the first rows.
---
tail()
Shows the last rows.
---
info()
Displays important dataset information.
This shows:
- Column names
- Data types
- Non-null values
- Memory usage
---
What Does "Non-Null" Mean?
Non-null means:
A cell contains actual data and is NOT empty.
Example:
Only 3 rows are non-null.
---
7. Introduction to Data Cleaning
Real-world datasets are often messy.
Pandas provides tools to clean them.
---
Removing Empty Rows
This removes rows with missing values.
---
Replacing Missing Values
This replaces empty cells with
---
Checking Duplicates
Returns:
-
-
---
8. Why Pandas is Important
Pandas is heavily used in:
- AI
- Machine Learning
- Financial Analysis
- Data Visualization
- Scientific Research
Almost every data scientist uses Pandas daily.
---
Practice Exercises
1. Load and Inspect
Load
---
2. Data Summary
Use
---
3. Dictionary to DataFrame
Create a dictionary of 5 students and their grades, then convert it into a DataFrame.
Data Analysis with Pandas
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
1. Introduction to Pandas
Pandas is one of the most powerful and popular Python libraries for data analysis.
It helps programmers:
- Analyze data
- Clean messy datasets
- Organize information
- Discover patterns
Pandas is built on top of NumPy, making it fast and efficient.
---
Why Use Pandas?
Data Cleaning:
Fix missing or incorrect values.
Data Exploration:
Understand what exists inside your dataset.
Speed:
Handles large files efficiently.
Real-World Usage:
Used heavily in:
- Data Science
- Artificial Intelligence
- Business Analytics
- Machine Learning
---
2. Importing Pandas
Before using Pandas:
import pandas as pd
`pd` is simply a short alias for Pandas.
---
3. Creating a DataFrame
A DataFrame is like a spreadsheet or table.
Example:
import pandas as pd
data = {
"calories": [420, 380, 390],
"duration": [50, 40, 45]
}
df = pd.DataFrame(data)
print(df)
Output:
calories duration
0 420 50
1 380 40
2 390 45
---
4. Series vs DataFrame
Pandas mainly uses two structures.
---
Series
A one-dimensional structure.
Example:
series = pd.Series([1, 2, 3])
print(series)
---
DataFrame
A multi-dimensional table.
Example:
df = pd.DataFrame(data)
Think of:
- Series → One column
- DataFrame → Entire spreadsheet
---
5. Reading CSV Files
CSV stands for Comma Separated Values.
Most real-world datasets are stored as CSV files.
Example:
df = pd.read_csv("data.csv")
print(df.to_string())---
Useful Tip
By default, Pandas only shows part of large datasets.
Increase visible rows:
pd.options.display.max_rows = 9999
---
6. Inspecting Data
---
head()
Shows the first rows.
print(df.head())
---
tail()
Shows the last rows.
print(df.tail())
---
info()
Displays important dataset information.
print(df.info())
This shows:
- Column names
- Data types
- Non-null values
- Memory usage
---
What Does "Non-Null" Mean?
Non-null means:
A cell contains actual data and is NOT empty.
Example:
Age
21
18
NULL
25
Only 3 rows are non-null.
---
7. Introduction to Data Cleaning
Real-world datasets are often messy.
Pandas provides tools to clean them.
---
Removing Empty Rows
new_df = df.dropna()
This removes rows with missing values.
---
Replacing Missing Values
df.fillna(130, inplace=True)
This replaces empty cells with
130.---
Checking Duplicates
print(df.duplicated())
Returns:
-
True → Duplicate row -
False → Unique row ---
8. Why Pandas is Important
Pandas is heavily used in:
- AI
- Machine Learning
- Financial Analysis
- Data Visualization
- Scientific Research
Almost every data scientist uses Pandas daily.
---
Practice Exercises
1. Load and Inspect
Load
data.csv and print the last 10 rows.---
2. Data Summary
Use
.info() and explain what "Non-Null" means.---
3. Dictionary to DataFrame
Create a dictionary of 5 students and their grades, then convert it into a DataFrame.
Python coding
Basic_Python_Matplotlib_Visualization.pdf
Basic Python Course
Data Visualization with Matplotlib
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
1. Introduction to Matplotlib
Matplotlib is one of the most popular Python libraries for data visualization.
It helps transform raw numbers into:
- Charts
- Graphs
- Visual insights
Most plotting features are available inside the
---
Importing Matplotlib
---
2. Basic Line Plot
A line plot connects data points using lines.
Example:
---
What
Without it, the graph may not appear.
---
3. Markers and Line Styles
Markers highlight data points.
Common markers:
| Marker | Meaning |
|--------|----------|
|
|
|
---
Example with Marker and Style
---
Understanding the Parameters
-
-
-
---
4. Labels and Titles
Graphs become meaningful when labels are added.
---
Example
---
Why Grids Matter
Grids make charts easier to read and compare.
---
5. Plotting Multiple Lines
Matplotlib allows multiple datasets on one graph.
Example:
---
What
Useful when comparing datasets.
---
6. Visualizing CSV Data
Matplotlib works perfectly with Pandas DataFrames.
Example:
---
What is a Scatter Plot?
A scatter plot displays relationships between two variables.
Useful for:
- Trend analysis
- Data patterns
- Correlation discovery
---
7. Why Visualization Matters
Humans understand visuals faster than raw numbers.
Visualization helps:
- Discover patterns
- Detect mistakes
- Compare data
- Explain results clearly
---
Real-World Uses of Matplotlib
- Data Science
- AI & Machine Learning
- Business Analytics
- Financial Charts
- Scientific Research
---
Practice Exercises
1. The Star Plot
Create a line plot for:
Use:
- Red star markers
- Dashed line
---
2. Grid Challenge
Recreate the "Sports Watch" graph and add a grid only on the Y-axis.
Hint:
---
3. Data Insight
Load
Data Visualization with Matplotlib
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
1. Introduction to Matplotlib
Matplotlib is one of the most popular Python libraries for data visualization.
It helps transform raw numbers into:
- Charts
- Graphs
- Visual insights
Most plotting features are available inside the
pyplot module.---
Importing Matplotlib
import matplotlib.pyplot as plt
import numpy as np
---
2. Basic Line Plot
A line plot connects data points using lines.
Example:
import matplotlib.pyplot as plt
import numpy as np
xpoints = np.array([0, 6])
ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints)
plt.show()
---
What
plt.show() Doesplt.show() displays the graph window.Without it, the graph may not appear.
---
3. Markers and Line Styles
Markers highlight data points.
Common markers:
| Marker | Meaning |
|--------|----------|
|
'o' | Circle ||
'*' | Star ||
'.' | Point |---
Example with Marker and Style
ypoints = np.array([3, 8, 1, 10])
plt.plot(
ypoints,
marker='*',
linestyle='dotted',
color='r'
)
plt.show()
---
Understanding the Parameters
-
marker='*' → Star marker -
linestyle='dotted' → Dotted line -
color='r' → Red color ---
4. Labels and Titles
Graphs become meaningful when labels are added.
---
Example
x = np.array([80, 85, 90, 95])
y = np.array([240, 250, 260, 270])
plt.plot(x, y)
plt.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.grid()
plt.show()
---
Why Grids Matter
Grids make charts easier to read and compare.
---
5. Plotting Multiple Lines
Matplotlib allows multiple datasets on one graph.
Example:
y1 = np.array([3, 8, 1, 10])
y2 = np.array([6, 2, 7, 11])
plt.plot(y1, label='Dataset A')
plt.plot(y2, label='Dataset B')
plt.legend()
plt.show()
---
What
legend() Doesplt.legend() shows labels for each line.Useful when comparing datasets.
---
6. Visualizing CSV Data
Matplotlib works perfectly with Pandas DataFrames.
Example:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data4.csv')
df.plot(
kind='scatter',
x='Pulse',
y='Calories'
)
plt.title("Relationship between Pulse and Calories")
plt.show()
---
What is a Scatter Plot?
A scatter plot displays relationships between two variables.
Useful for:
- Trend analysis
- Data patterns
- Correlation discovery
---
7. Why Visualization Matters
Humans understand visuals faster than raw numbers.
Visualization helps:
- Discover patterns
- Detect mistakes
- Compare data
- Explain results clearly
---
Real-World Uses of Matplotlib
- Data Science
- AI & Machine Learning
- Business Analytics
- Financial Charts
- Scientific Research
---
Practice Exercises
1. The Star Plot
Create a line plot for:
[2, 10, 4, 8]
Use:
- Red star markers
- Dashed line
---
2. Grid Challenge
Recreate the "Sports Watch" graph and add a grid only on the Y-axis.
Hint:
plt.grid(axis='y')
---
3. Data Insight
Load
data4.csv and create a line plot showing how "Maxpulse" changes over time.Hello everyone! 👋
You might have noticed a temporary pause in our regular posts recently. I wanted to let you all know that I have paused posting in celebration of Eid.
Regular educational content and updates will resume right after the holiday! Enjoy the break, take some time to review the previous material, and get ready for more exciting programming and development content coming your way very soon.
Thank you for your understanding, and see you all after Eid! ✨
Eid Mubarek ✨✨
You might have noticed a temporary pause in our regular posts recently. I wanted to let you all know that I have paused posting in celebration of Eid.
Regular educational content and updates will resume right after the holiday! Enjoy the break, take some time to review the previous material, and get ready for more exciting programming and development content coming your way very soon.
Thank you for your understanding, and see you all after Eid! ✨
Eid Mubarek ✨✨
👍1
Python coding
sales.csv
Basic Python Course
Matplotlib Visualization Lab
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
Introduction
Visualization helps transform raw numbers into meaningful insights.
In this lab, we will create charts using Matplotlib and NumPy to understand patterns, trends, and comparisons.
---
Exercise 1: Plotting a Linear Function
A linear function has the general form:
Where:
-
-
For this exercise:
---
Code
---
Explanation
Creates 100 evenly spaced numbers between -10 and 10.
---
Calculates the corresponding Y values.
---
Draws the graph.
---
Displays grid lines for easier reading.
---
Displays the graph label.
---
Expected Result
You will see:
- A straight green line
- Crossing the Y-axis at 3
- Increasing continuously because the slope is positive
---
Exercise 2: Comparing Multiple Data Series
Businesses often compare sales between different stores or years.
In this example, we compare product sales between Store A and Store B.
---
Code
---
Explanation
Products:
Categories displayed on the X-axis.
---
Sales Data
Represent sales numbers for two stores.
---
Transparency
Makes bars semi-transparent so overlapping bars remain visible.
---
Legend
Helps identify which color belongs to which store.
---
Expected Result
A bar chart showing:
- Blue bars → Store A
- Red bars → Store B
This makes comparison quick and easy.
---
Why These Visualizations Matter
Line Charts:
- Show trends
- Show growth or decline
- Useful for predictions
Bar Charts:
- Compare categories
- Compare performance
- Compare products, teams, or departments
---
Real-World Applications
- Business dashboards
- AI model performance analysis
- Student grade comparison
- Financial reports
- Scientific experiments
---
Practice Exercises
1. Modify the line graph and plot:
---
2. Create a bar chart comparing the marks of five students.
---
3. Add:
- X-axis label
- Y-axis label
- Grid
- Legend
to all charts.
---
4. Create a chart comparing:
- Python learners
- Java learners
across four different schools.
Matplotlib Visualization Lab
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
Introduction
Visualization helps transform raw numbers into meaningful insights.
In this lab, we will create charts using Matplotlib and NumPy to understand patterns, trends, and comparisons.
---
Exercise 1: Plotting a Linear Function
A linear function has the general form:
y = mx + b
Where:
-
m = slope-
b = y-interceptFor this exercise:
y = 2x + 3
---
Code
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-10, 10, 100)
y = 2 * x + 3
plt.plot(
x,
y,
color='green',
label='y = 2x + 3'
)
plt.xlabel("X Values")
plt.ylabel("Y Values")
plt.title("Simple Line Plot")
plt.grid(True)
plt.legend()
plt.show()
---
Explanation
np.linspace(-10, 10, 100)
Creates 100 evenly spaced numbers between -10 and 10.
---
y = 2 * x + 3
Calculates the corresponding Y values.
---
plt.plot()
Draws the graph.
---
plt.grid(True)
Displays grid lines for easier reading.
---
plt.legend()
Displays the graph label.
---
Expected Result
You will see:
- A straight green line
- Crossing the Y-axis at 3
- Increasing continuously because the slope is positive
---
Exercise 2: Comparing Multiple Data Series
Businesses often compare sales between different stores or years.
In this example, we compare product sales between Store A and Store B.
---
Code
import matplotlib.pyplot as plt
products = [
'Laptop',
'Mouse',
'Keyboard',
'Monitor'
]
sales_v1 = [5, 15, 10, 8]
sales_v2 = [7, 12, 11, 9]
plt.bar(
products,
sales_v1,
color='blue',
alpha=0.5,
label='Store A'
)
plt.bar(
products,
sales_v2,
color='red',
alpha=0.5,
label='Store B'
)
plt.title("Sales Comparison by Product")
plt.legend()
plt.show()
---
Explanation
Products:
products = [
'Laptop',
'Mouse',
'Keyboard',
'Monitor'
]
Categories displayed on the X-axis.
---
Sales Data
sales_v1 = [5, 15, 10, 8]
sales_v2 = [7, 12, 11, 9]
Represent sales numbers for two stores.
---
Transparency
alpha = 0.5
Makes bars semi-transparent so overlapping bars remain visible.
---
Legend
plt.legend()
Helps identify which color belongs to which store.
---
Expected Result
A bar chart showing:
- Blue bars → Store A
- Red bars → Store B
This makes comparison quick and easy.
---
Why These Visualizations Matter
Line Charts:
- Show trends
- Show growth or decline
- Useful for predictions
Bar Charts:
- Compare categories
- Compare performance
- Compare products, teams, or departments
---
Real-World Applications
- Business dashboards
- AI model performance analysis
- Student grade comparison
- Financial reports
- Scientific experiments
---
Practice Exercises
1. Modify the line graph and plot:
y = 3x - 5
---
2. Create a bar chart comparing the marks of five students.
---
3. Add:
- X-axis label
- Y-axis label
- Grid
- Legend
to all charts.
---
4. Create a chart comparing:
- Python learners
- Java learners
across four different schools.
Python coding
Statistical_Visualization_Seaborn_Guide.pdf
Basic Python Course
Statistical Visualization with Seaborn
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
1. Introduction to Seaborn
Seaborn is a powerful Python library built on top of Matplotlib.
It is specifically designed for:
- Statistical Visualization
- Data Exploration
- Pattern Discovery
- Correlation Analysis
Seaborn works extremely well with Pandas DataFrames and requires much less code than Matplotlib for advanced visualizations.
---
Why Use Seaborn?
Advantages:
- Beautiful default chart styles
- Easier syntax
- Built-in statistical plots
- Excellent integration with Pandas
- Commonly used in Data Science and AI
---
Importing Seaborn
Example dataset:
The `tips
sns.lineplot(
data=df,
x="Year",
y="Sales",
hue="Region"
)
plt.show()
python
hue = "Region"
python
sns.scatterplot(
x="Age",
y="Weight",
data=my_data
)
plt.title("Age vs Weight Correlation")
plt.show()
text
Age ↑ → Weight ↑
python
sns.boxplot(
x="day",
y="total_bill",
data=tips
)
plt.show()
python
correlation_matrix = df.corr()
sns.heatmap(
correlation_matrix,
annot=True,
cmap='coolwarm'
)
plt.show()
python
annot=True
python
cmap='coolwarm'
text
total_bill vs tip
Color points based on whether the customer is a smoker.
---
3. Clean and Plot
Using your cleaned sales dataset:
Statistical Visualization with Seaborn
By Yehya Oumer
(Certificate from Mizan Institute of Technology)
Issued: 2026
---
1. Introduction to Seaborn
Seaborn is a powerful Python library built on top of Matplotlib.
It is specifically designed for:
- Statistical Visualization
- Data Exploration
- Pattern Discovery
- Correlation Analysis
Seaborn works extremely well with Pandas DataFrames and requires much less code than Matplotlib for advanced visualizations.
---
Why Use Seaborn?
Advantages:
- Beautiful default chart styles
- Easier syntax
- Built-in statistical plots
- Excellent integration with Pandas
- Commonly used in Data Science and AI
---
Importing Seaborn
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
Example dataset:
tips = sns.load_dataset("tips")The `tips
dataset is included with Seaborn and is commonly used for learning and practice.
---
2. Line Plots and Semantic Grouping
Line plots help visualize trends over time.
Seaborn can automatically separate groups using colors with the hue parameter.
---
Example
``pythonsns.lineplot(
data=df,
x="Year",
y="Sales",
hue="Region"
)
plt.show()
---
Understanding `hue`
The `hue` parameter automatically assigns different colors to categories.
Example:
python
hue = "Region"
Different regions will appear as separate colored lines.
---
Applications
- Sales growth analysis
- Population trends
- Weather tracking
- Business dashboards
---
3. Scatter Plots and Correlation
Scatter plots are used to discover relationships between numerical variables.
---
Example
python
sns.scatterplot(
x="Age",
y="Weight",
data=my_data
)
plt.title("Age vs Weight Correlation")
plt.show()
---
Why Scatter Plots Matter
They help identify:
- Positive correlation
- Negative correlation
- No correlation
- Outliers
---
Example Interpretation
If points move upward from left to right:
text
Age ↑ → Weight ↑
This indicates a positive correlation.
---
4. Box Plots (Five Number Summary)
A box plot summarizes the distribution of data.
It highlights:
- Minimum value
- First Quartile (Q1)
- Median
- Third Quartile (Q3)
- Maximum value
It also helps detect outliers.
---
Example
python
sns.boxplot(
x="day",
y="total_bill",
data=tips
)
plt.show()
---
Why Box Plots Are Useful
They quickly show:
- Data spread
- Skewness
- Median values
- Unusual observations
---
5. Heatmaps and Correlation Matrices
Heatmaps visually represent relationships between variables.
They are extremely important in:
- Machine Learning
- AI Engineering
- Data Analysis
---
Example
python
correlation_matrix = df.corr()
sns.heatmap(
correlation_matrix,
annot=True,
cmap='coolwarm'
)
plt.show()
---
Understanding the Parameters
python
annot=True
Displays the correlation values inside each cell.
---
python
cmap='coolwarm'
Uses a color gradient:
- Blue → Weak or Negative Relationship
- Red → Strong Positive Relationship
---
Why Heatmaps Matter
They help answer questions like:
- Which variables affect sales?
- Which features influence predictions?
- Which columns are strongly related?
---
Real-World Applications of Seaborn
- Data Science
- Artificial Intelligence
- Business Intelligence
- Finance
- Healthcare Analytics
- Scientific Research
---
Comparison: Matplotlib vs Seaborn
| Feature | Matplotlib | Seaborn |
|----------|----------|----------|
| Ease of Use | Moderate | Easy |
| Statistical Charts | Limited | Excellent |
| Default Appearance | Basic | Modern |
| DataFrame Support | Good | Excellent |
---
Statistical Practice Exercises
1. Tips Analysis
Load the `tips` dataset and create a boxplot showing how `total_bill` changes between Lunch and Dinner.
---
2. Hue Challenge
Create a scatter plot showing:
text
total_bill vs tip
`Color points based on whether the customer is a smoker.
---
3. Clean and Plot
Using your cleaned sales dataset:
Python coding
Statistical_Visualization_Seaborn_Guide.pdf
Create a Seaborn line plot showing:
Price over Date