Introduction
Data Science means studying data to find useful information and
patterns.
It helps people and companies make better decisions by turning raw
data (unorganized information) into meaningful results.
To do this, data science uses:
Statistics → to understand numbers and data.
Computer Science → to write code and use machines for
calculations.
Domain Knowledge → to understand the subject or area the data
belongs to (like business, health, education, etc.).
Python is one of the most important tools in data science because:
It is simple and easy to learn.
It has clear and readable code.
It includes many helpful libraries like:
o NumPy → for mathematical calculations.
o Pandas → for handling and analyzing data.
o Matplotlib → for drawing graphs and charts.
o Scikit-learn → for machine learning (making predictions).
So, before learning advanced data science topics, you must first
understand:
The Python environment,
Basic Python programming, and
How to use data libraries to handle and study data.
Python Programming
1. Python Environment
The Python environment means everything you need to write, run, and
test your Python programs.
A good setup helps you work smoothly and keeps your projects
organized.
You can download Python from the official Python Software
Foundation website:
[Link]
After installation, open your command prompt (CMD) and type:
python --version
This command will show which version of Python is installed.
Important: Python 2 is old and not used anymore. You should always
install Python 3.x (the latest version).
1.1 Virtual Environment
When you work on many Python projects, each project may need
different packages or different versions of the same package.
To avoid problems, we create a virtual environment — it acts like a
separate box for each project.
This way, every project has its own libraries and settings, and they don’t
disturb each other.
To create a virtual environment, type:
python -m venv myenv
To activate it:
On Windows:
myenv\Scripts\activate
On macOS/Linux:
source myenv/bin/activate
After activation, any package you install will stay inside that project’s
environment only.
1.2 Package Management
Python becomes powerful because of its packages — small pieces of
code made by other people that you can reuse.
For example, if you want to work with data, instead of writing your own
functions, you can install Pandas or NumPy using a tool called pip.
pip is Python’s built-in package manager. It helps you install, update,
and remove packages easily.
Example to install some useful packages:
pip install numpy pandas matplotlib
If you want to install all the packages listed in a file (for example, in
[Link]), use:
pip install -r [Link]
This helps when you want to share your project with others — they can
install the same libraries easily.
1.3 Integrated Development Environments (IDEs)
An IDE (Integrated Development Environment) is special software
made for programmers.
It gives a better place to write code because it includes:
Syntax highlighting (colors and formatting for easy reading),
Debugging tools (to find and fix errors),
Suggestions and auto-complete features, and
Visualization tools for data science.
Here are some popular Python IDEs:
1. VS Code – Lightweight and fast. You can add plugins for Python
and data science.
2. PyCharm – Very powerful, with many professional tools like
debugging and environment setup.
3. Jupyter Notebook – Mostly used for data science. It lets you write
code and see results step by step, along with text and graphs.
4. Spyder – Built mainly for scientists and researchers. It’s very good
for data analysis work.
All of these IDEs help make coding easier, clearer, and more productive
— especially when working on data science projects.
2. Python Programming Techniques
Python gives us many smart and short ways to write code.
These techniques make our programs faster, shorter, and easier to read
— especially when working with data.
Some of these useful techniques are:
Lambda Functions
Higher-Order Functions (map, filter, reduce)
List Comprehensions
Generators
Let’s understand them one by one
2.1 Lambda Functions
A lambda function is a small, one-line function in Python.
It has no name, so it is also called an anonymous function.
We use lambda functions when we need to do small or quick
calculations, instead of creating a full function using def.
Syntax:
lambda arguments: expression
Example:
square = lambda x: x * x
print(square(5))
# Output: 25
Here,
lambda x: means it takes one value x.
x * x is the operation (multiplying it by itself).
So when we call square(5), it returns 25.
✅ Lambda with Higher-Order Functions
We can also use lambda functions with other functions like map, filter,
and reduce.
Example:
nums = [1, 2, 3, 4]
squared = list(map(lambda x: x ** 2, nums))
print(squared)
# Output: [1, 4, 9, 16]
Here,
map() applies the lambda function to each item in the list.
It squares every number in the list.
💡 Why use lambda functions?
Because they make code short and clean, especially in data processing
and pipelines.
2.2 Map, Filter, and Reduce
These three are called functional programming tools.
They help us process data quickly and easily without using long loops.
🟢 Map()
The map() function applies a given function to each item in a list (or
other iterable).
Example:
nums = [1, 2, 3, 4]
result = list(map(lambda x: x * 2, nums))
print(result)
# Output: [2, 4, 6, 8]
It multiplies every number in the list by 2.
🔵 Filter()
The filter() function selects only those items from a list that meet a
certain condition.
Example:
nums = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, nums))
print(evens)
# Output: [2, 4, 6]
It filters out only the even numbers (those that divide by 2).
🟣 Reduce()
The reduce() function combines all elements of a list into one final
value.
It’s often used for adding, multiplying, or combining values.
To use it, we must import it first:
from functools import reduce
nums = [1, 2, 3, 4]
total = reduce(lambda a, b: a + b, nums)
print(total)
# Output: 10
Here, reduce adds all numbers together → 1 + 2 + 3 + 4 = 10
2.3 List Comprehension
List comprehension is a short and clear way to make new lists from
existing ones.
It replaces long loops with one simple line of code.
Example:
nums = [1, 2, 3, 4]
squares = [x * x for x in nums]
print(squares)
# Output: [1, 4, 9, 16]
Explanation:
x * x → the operation we want to do
for x in nums → repeats this operation for each number in the list
Why use it?
It’s faster, cleaner, and takes less code than using a normal loop.
2.4 Generators
Generators are like list comprehensions, but they don’t store all values
in memory at once.
Instead, they create values one by one when needed.
This helps save memory, especially when working with big data.
Example:
gen = (x * x for x in range(5))
for i in gen:
print(i)
Output:
0
1
4
9
16
Here, Python doesn’t create the full list [0,1,4,9,16] in memory.
It gives each value only when we ask for it — one at a time.
3. Reading and Manipulating CSV Files
In data science, most datasets are stored in a file format called CSV
(Comma-Separated Values).
A CSV file looks like a table but is saved as plain text — meaning you
can open it even in Notepad or Excel.
Each line in a CSV file represents one record (row),
and each value inside that line is separated by a comma (,).
Example of a small CSV file:
Name, Age, Marks
Ali, 20, 85
Sara, 21, 90
Bilal, 19, 75
Python gives us many ways to read, write, and change data from CSV
files.
We can use either built-in modules (like csv) or external libraries (like
NumPy or Pandas).
🧩 3.1 Using the csv Module
The csv module is already included in Python — you don’t need to
install it separately.
It helps you to read and write CSV files easily.
📘 Reading a CSV File
When you want to read data from a CSV file, you can do it like this:
import csv
with open('[Link]', mode='r') as file:
reader = [Link](file)
for row in reader:
print(row)
Explanation:
open('[Link]', mode='r') → opens the file in read mode.
[Link](file) → reads the file line by line.
for row in reader: → loops through each line (row) in the file.
print(row) → shows each row as a list of values.
Example Output:
['Name', 'Age']
['Alice', '25']
['Bob', '30']
📗 Reading a CSV File with Headers
If your CSV file has column names (headers), you can use DictReader
instead of reader.
with open('[Link]', mode='r') as file:
reader = [Link](file)
for row in reader:
print(row['Name'], row['Age'])
Explanation:
DictReader reads each row as a dictionary,
where the keys are the column names (like “Name”, “Age”).
You can access specific columns by using their header names.
Output Example:
Alice 25
Bob 30
📙 Writing Data to a CSV File
You can also create or write data into a CSV file using [Link].
import csv
with open('[Link]', mode='w', newline='') as file:
writer = [Link](file)
[Link](['Name', 'Age'])
[Link]([['Alice', 25], ['Bob', 30]])
Explanation:
mode='w' → opens the file in write mode (creates a new file).
writerow() → writes one line (row) at a time.
writerows() → writes many rows at once.
newline='' → prevents blank lines between rows.
After running this, a file named [Link] will be created with:
Name,Age
Alice,25
Bob,30
🔢 3.2 Using NumPy to Read CSV Files
If your CSV file contains only numbers, you can use NumPy to read it
directly into an array.
NumPy makes it easy to work with numerical data because it’s fast and
efficient.
import numpy as np
data = [Link]('[Link]', delimiter=',', skip_header=1)
print(data)
Explanation:
genfromtxt() → reads data from a text or CSV file.
delimiter=',' → tells NumPy that values are separated by commas.
skip_header=1 → skips the first line (the header row).
Output Example:
[[20. 85.]
[21. 90.]
[19. 75.]]
This shows all numeric values in the form of a NumPy array, which is
very useful for calculations.
3.3 Manipulating Data (Filtering and Saving)
Once you read the CSV file, you can easily filter, modify, or analyze the
data using Python.
Example:
Let’s say we have a file named [Link] that contains student
names and marks.
We want to find students who scored more than 80 and save them into
a new file.
import csv
rows = []
with open('[Link]', 'r') as file:
reader = [Link](file)
for row in reader:
if int(row['Marks']) > 80:
[Link](row)
with open('top_students.csv', 'w', newline='') as file:
writer = [Link](file, fieldnames=['Name', 'Marks'])
[Link]()
[Link](rows)
Explanation:
1. [Link]() → reads each row as a dictionary.
2. if int(row['Marks']) > 80: → checks if the marks are greater than
80.
3. [Link](row) → saves that student’s record to the list.
4. Finally, it writes all selected students into a new file called
top_students.csv.
Resulting file (top_students.csv):
Name,Marks
Ali,85
Sara,90
4. NumPy Library
NumPy stands for Numerical Python.
It is one of the most important and powerful libraries used for scientific
and mathematical computing in Python.
NumPy provides:
High-speed mathematical operations,
Multidimensional arrays (called ndarrays), and
Many built-in functions for calculations, statistics, and data
manipulation.
Unlike normal Python lists, NumPy arrays:
Store only one data type (all integers, or all floats, etc.) — this
makes them faster.
Allow vectorized operations, which means you can perform math
on whole arrays at once without writing loops.
That’s why NumPy is the foundation of data science and machine
learning in Python.
🧮 4.1 Creating Arrays
You can create arrays in NumPy using the array() function.
Example:
import numpy as np
a = [Link]([1, 2, 3, 4]) # 1D array
b = [Link]([[1, 2, 3], [4, 5, 6]]) # 2D array
Explanation:
a is a one-dimensional array (like a simple list).
b is a two-dimensional array (like rows and columns in a table).
📏 4.2 Array Properties
Every NumPy array has important properties that describe its structure.
Example:
print([Link]) # Number of dimensions (1 for 1D, 2 for 2D, etc.)
print([Link]) # Number of rows and columns
print([Link]) # Total number of elements
print([Link]) # Type of data (int, float, etc.)
Explanation:
.ndim tells how many dimensions the array has.
.shape shows the size of each dimension.
.size gives the total number of elements.
.dtype tells what type of numbers are stored.
🧰 4.3 Special Arrays
NumPy can easily create arrays with pre-filled values using built-in
functions.
Examples:
[Link]((2, 3)) # Creates a 2x3 array filled with 0s
[Link]((3, 3)) # Creates a 3x3 array filled with 1s
[Link]((2, 2), 7) # Creates a 2x2 array filled with 7
[Link](3) # Creates a 3x3 identity matrix (1s on diagonal)
[Link](0, 10, 2) # Creates array [0, 2, 4, 6, 8]
Explanation:
These arrays are very useful when you want to initialize data or
create matrices quickly.
➕ 4.4 Array Operations
One of the biggest advantages of NumPy is that it supports element-
wise operations.
This means you can perform calculations on entire arrays directly — no
need for loops.
Example:
x = [Link]([1, 2, 3])
y = [Link]([4, 5, 6])
print(x + y) # [5, 7, 9] → Addition
print(x * y) # [4, 10, 18] → Multiplication
print(x ** 2) # [1, 4, 9] → Power
Explanation:
Each element in the array is combined with the element at the same
position in the other array.
📊 4.5 Aggregate Functions
NumPy provides built-in functions to perform summary or statistical
calculations.
Example:
a = [Link]([1, 2, 3, 4, 5])
print([Link](a)) # Adds all numbers → 15
print([Link](a)) # Average → 3.0
print([Link](a)) # Largest value → 5
print([Link](a)) # Smallest value → 1
Explanation:
These functions help in quickly finding totals, averages, and other
statistics — very common in data analysis.
🔄 4.6 Reshaping and Transposing
NumPy allows you to reshape and transpose arrays easily.
Example:
a = [Link](6) # [0, 1, 2, 3, 4, 5]
b = [Link]((2, 3)) # Changes to 2 rows × 3 columns
print(b)
print(b.T) # Transpose (rows become columns)
Explanation:
reshape() → changes the shape (size) of an array.
T → gives the transpose of the array (flips rows and columns).
🎯 4.7 Filtering Using Conditions
You can also filter data in NumPy using conditions.
Example:
a = [Link]([10, 20, 30, 40, 50])
print(a[a > 25])
# Output: [30, 40, 50]
Explanation:
Here, a > 25 checks each element.
Only the numbers greater than 25 are selected and shown.
This is very helpful when you want to find specific data that meets
certain conditions.
💡 Example: CSV Integration (NumPy + Pandas + Lambda)
Now, let’s see how Python tools work together in a real data analysis
workflow.
Example:
import pandas as pd
import numpy as np
df = pd.read_csv('[Link]') # Read CSV file
sales = df['Revenue'].to_numpy() # Convert Revenue column to
NumPy array
mean_sales = [Link](sales) # Calculate average revenue
print("Average Revenue:", mean_sales)
df['Tax'] = df['Revenue'].apply(lambda x: x * 0.15) # Add Tax column
using lambda
df.to_csv('updated_sales.csv', index=False) # Save updated data
Explanation (Step by Step):
1. Pandas reads the data from a CSV file.
2. The “Revenue” column is changed into a NumPy array for fast
calculations.
3. NumPy calculates the average (mean) of all revenues.
4. A lambda function is used to calculate tax (15% of each revenue).
5. The updated file is saved again as a new CSV.
This shows how Pandas (for data files), NumPy (for math), and Lambda
(for logic) work together in a complete data science process.
🧩 Integration of Concepts: CSV, Lambda, and NumPy
Concept Description Example / Tool
Python Setup using IDEs, venv, VS python -m venv myenv, VS
Environment Code, Jupyter Code, Jupyter
Lambda Small, one-line
lambda x: x * 2
Functions anonymous functions
Concept Description Example / Tool
Map / Filter / Functional programming
map(lambda x: x*2, data)
Reduce tools
Handling structured data
Reading CSV csv, pandas
files
Efficient numerical
NumPy Arrays [Link], [Link]()
computations
✅ Summary
Python provides a strong base for data science because it combines:
Easy-to-read code
Powerful tools for math and data
Libraries like CSV, NumPy, Pandas, and Lambda functions