Introduction to Python
for Data Science

A detailed, humanized walkthrough of every concept you need — from setting up your very first environment to building real-world projects with pandas, NumPy, and Matplotlib.

Machine Learning Generative AI AI Ethics
Scroll to explore
Contents

We generate staggering amounts of data every single day — every search, every swipe, every purchase leaves a trail. Python is the language that lets you make sense of it all. This guide will hold your hand through every step, with real code you can run right now.

Section 01

Why Python for Data Science?

Before jumping into any code, it's worth asking a very reasonable question: why Python? The world is full of programming languages — Java, R, Julia, Scala, C++ — so why has the data science community rallied around Python so enthusiastically?

The honest answer is a combination of simplicity, power, and community. Python was designed from the ground up to be readable. Its creator, Guido van Rossum, wanted a language that felt almost like pseudocode — something that a non-programmer could glance at and roughly understand. That philosophy has paid off enormously in data science, where analysts, statisticians, and researchers who aren't professional software engineers need to write and read code fluidly.

Consider this contrast. To print "Hello, World!" in Java, you need to set up a class, a main method, and deal with semicolons. In Python, you write exactly one line:

Python
print("Hello, World!")

That's it. No boilerplate, no ceremony. This simplicity lowers the barrier to entry dramatically and lets you spend your mental energy on the problem you're trying to solve, not the language you're using to solve it.

But Python isn't just easy — it's incredibly capable. Its ecosystem of third-party libraries means that for almost any data task you can imagine, someone has already written a robust, well-tested tool for it. You don't need to implement a matrix multiplication algorithm from scratch; NumPy has done that for you, and it's faster than anything you'd write by hand because it's backed by C code under the hood.

🧩

Simple Syntax

Reads like English. Focus on logic, not language quirks.

📦

Rich Ecosystem

Thousands of libraries for every data task imaginable.

🌍

Huge Community

Every error you'll ever make has been answered on Stack Overflow.

🏭

Industry Standard

Used at Google, NASA, Netflix, and virtually every tech company.

Section 02

Setting Up Your Environment

Think of your environment as your workshop. A carpenter doesn't start building furniture in an empty field — they set up a space with the right tools, proper lighting, and organized storage. Your Python environment works the same way: it's the collection of software, interpreters, and editors that let you write and run code efficiently.

The good news is you have several excellent options, and choosing one is more about personal preference than technical correctness. Let's walk through each.

Option A — Anaconda (Recommended for Beginners)

Anaconda is a distribution — think of it as a big, pre-packed toolbox that installs Python plus over 250 commonly used data science libraries in one shot. You download one installer and suddenly you have NumPy, Pandas, Jupyter, Matplotlib, Scikit-learn, and dozens more, all ready to use.

Anaconda also includes conda, a package and environment manager that makes it trivially easy to create isolated project environments. This prevents the infamous "but it works on my machine" problem where different versions of libraries conflict with each other.

💡 Pro Tip

Download Anaconda from https://www.anaconda.com/download . During installation, check the box that says "Add Anaconda to my PATH" to make things much easier later.

Option B — Jupyter Notebook

Jupyter Notebook is the favourite tool of data scientists worldwide, and for very good reason. It's an interactive document where you write code in small, runnable "cells", see the output immediately below, add formatted explanatory text with markdown, and embed graphs right inline. It's equal parts coding environment and living documentation.

If you install Anaconda, Jupyter comes bundled. You can also install it standalone:

Terminal / Command Prompt
pip install notebook
jupyter notebook

Running jupyter notebook opens a browser tab at localhost:8888 where you can create, manage, and run notebooks.

Option C — Google Colab (Zero Setup)

Google Colab is possibly the most convenient option for students. It's entirely browser-based, meaning you don't install a single thing on your computer. You open https://colab.research.google.com/ , sign in with your Google account, and you're writing Python immediately. It even provides free GPU access for computationally heavy tasks like training machine learning models.

⚠ Heads Up

Colab sessions time out after a period of inactivity, and your local files don't persist between sessions. Always save your notebooks to Google Drive before closing your browser.

Section 03

The Core Python Basics

Now we get to the fun part — actually writing code. Python's fundamentals aren't just abstract programming concepts; they're the building blocks you'll use every single day when working with data. Let's explore each one with clear, practical examples.

Variables — Your Data's Labelled Containers

A variable is simply a name that points to a value stored in memory. Imagine you're doing a science experiment and you write measurements on sticky notes — each note has a label (the variable name) and a value (the measurement). Python makes this incredibly natural.

Python — Variables & Data Types
# --- String: text data ---
student_name = "Priya Sharma"
city = "Kolkata"

# --- Integer: whole numbers ---
age = 21
total_students = 1250

# --- Float: decimal numbers ---
gpa = 3.87
temperature_celsius = 36.6

# --- Boolean: True or False ---
is_enrolled = True
has_paid_fees = False

# Display them all
print(f"Name: {student_name}, Age: {age}, GPA: {gpa}")
print(f"Type of gpa: {type(gpa)}")
▶ Output
Name: Priya Sharma, Age: 21, GPA: 3.87
Type of gpa: <class 'float'>

Notice the f"..." syntax — these are called f-strings (formatted string literals), introduced in Python 3.6. They let you embed variable values directly inside a string by wrapping them in curly braces. They're clean, readable, and you'll use them constantly.

Lists — Storing Collections of Data

Real-world data almost never comes as a single value. You have a list of temperatures, a series of stock prices, an array of student scores. Python's list is the fundamental structure for holding multiple values in order. Lists are mutable (you can change them after creation), ordered (the order matters), and can hold any mix of data types.

Python — Lists
# Creating lists
exam_scores = [78, 92, 85, 67, 95, 88, 73]
subjects = ["Mathematics", "Statistics", "Python", "Machine Learning"]

# Accessing elements (indexing starts at 0)
print(exam_scores[0])    # First score → 78
print(exam_scores[-1])   # Last score  → 73

# Slicing: grab a sub-list
print(exam_scores[1:4])  # [92, 85, 67]

# Useful list operations
print(f"Highest score: {max(exam_scores)}")
print(f"Lowest score:  {min(exam_scores)}")
print(f"Average score: {sum(exam_scores) / len(exam_scores):.2f}")

# Add a new score
exam_scores.append(90)
print(f"Updated list: {exam_scores}")
▶ Output
78
73
[92, 85, 67]
Highest score: 95
Lowest score: 67
Average score: 82.57
Updated list: [78, 92, 85, 67, 95, 88, 73, 90]

Loops — Automating Repetitive Tasks

One of the most powerful things a computer can do is repeat the same operation thousands of times without complaint. In data science, you'll often need to apply the same calculation to every row in a dataset, every file in a folder, or every value in a list. Loops are your tool for automation.

Python — For Loops & While Loops
# --- For Loop: iterate over a sequence ---
temperatures_c = [22.0, 25.5, 19.8, 30.1, 27.3]

print("Converting Celsius to Fahrenheit:")
for temp in temperatures_c:
    fahrenheit = (temp * 9/5) + 32
    print(f"  {temp}°C  →  {fahrenheit:.1f}°F")

# --- enumerate() gives you index + value ---
print("\nSubjects with their index:")
for i, subject in enumerate(subjects, start=1):
    print(f"  {i}. {subject}")

# --- List Comprehension: a compact, Pythonic loop ---
squared = [x**2 for x in range(1, 6)]
print(f"\nSquares 1–5: {squared}")
▶ Output
Converting Celsius to Fahrenheit:
  22.0°C → 71.6°F
  25.5°C → 77.9°F
  19.8°C → 67.6°F
  30.1°C → 86.2°F
  27.3°C → 81.1°F

Subjects with their index:
  1. Mathematics
  2. Statistics
  3. Python
  4. Machine Learning

Squares 1–5: [1, 4, 9, 16, 25]

Functions — Reusable Recipes of Logic

Imagine having to rewrite the recipe for chocolate cake every time someone asks you for it. That's what coding without functions looks like — endless repetition. A function packages a block of logic under a name so you can call it as many times as you need, with different inputs, without repeating yourself.

Functions are the backbone of clean, maintainable code. In data science, you'll write functions to clean data, apply transformations, calculate statistics, and generate reports.

Python — Functions
def calculate_statistics(data):
    """
    Calculate basic descriptive statistics for a list of numbers.
    Returns a dictionary with mean, median, min, and max.
    """
    n = len(data)
    mean = sum(data) / n

    # Compute median manually
    sorted_data = sorted(data)
    mid = n // 2
    median = (sorted_data[mid] + sorted_data[mid - 1]) / 2 if n % 2 == 0 else sorted_data[mid]

    return {
        "count": n,
        "mean":  round(mean, 2),
        "median": median,
        "min":  min(data),
        "max":  max(data)
    }

# Use the function on any dataset
scores = [78, 92, 85, 67, 95, 88, 73]
stats = calculate_statistics(scores)

for key, value in stats.items():
    print(f"{key:10s}: {value}")
▶ Output
count : 7
mean : 82.57
median : 85
min : 67
max : 95
Section 04

The "Holy Trinity" of Data Science Libraries

Pure Python is impressive, but the real reason data scientists love it so much comes down to three libraries that transform it into an incredibly powerful data analysis platform. These three are so fundamental, so universally used, and so deeply interconnected that many people refer to them as the "Holy Trinity" of Python data science.

Library 1

NumPy — The Mathematical Backbone

NumPy (Numerical Python) is the foundation upon which the entire scientific Python ecosystem is built. At its heart, NumPy provides the ndarray — an N-dimensional array object that is blazingly fast because it stores elements of a single, contiguous type in memory and delegates heavy computation to optimized C and Fortran code.

Why does this matter? A standard Python list with 1,000,000 numbers takes roughly 8MB of memory and processes operations slowly because each element is a full Python object with overhead. A NumPy array with the same numbers takes about 4MB and runs operations orders of magnitude faster, especially with broadcasting — the ability to apply an operation to an entire array at once without explicit loops.

Python — NumPy Essentials
import numpy as np

# --- Creating arrays ---
arr = np.array([10, 20, 30, 40, 50])
matrix = np.array([[1, 2, 3],
                   [4, 5, 6],
                   [7, 8, 9]])

# --- Broadcasting: no loop needed! ---
print("Array doubled:", arr * 2)         # [20, 40, 60, 80, 100]
print("Array + 100:",  arr + 100)       # [110, 120, ...]

# --- Statistical functions ---
data = np.array([23, 45, 12, 67, 34, 89, 56, 78])
print(f"\nMean:    {np.mean(data):.2f}")
print(f"Std Dev: {np.std(data):.2f}")
print(f"Median:  {np.median(data):.2f}")

# --- Generating data ---
zeros   = np.zeros(5)
ones    = np.ones(5)
range_a = np.arange(0, 1, 0.2)  # [0.0, 0.2, 0.4, 0.6, 0.8]
random_data = np.random.normal(loc=50, scale=10, size=1000)

print(f"\nShape of matrix: {matrix.shape}")     # (3, 3)
print(f"Matrix transposed:\n{matrix.T}")
Library 2

Pandas — Excel on Steroids

If NumPy is a calculator, Pandas is Excel reborn as a programming library. Named after "Panel Data" (a term from econometrics), Pandas introduces two revolutionary data structures: the Series (a labelled 1D array) and the DataFrame (a labelled 2D table with rows and columns). It's in the DataFrame where the magic really happens.

With Pandas, you can load a CSV file with a single line of code, filter rows based on conditions, group data and calculate aggregates, handle missing values, merge datasets, reshape tables, and export results — all with clean, readable syntax. It turns what used to be hours of spreadsheet work into a few lines of Python.

Python — Pandas Deep Dive
import pandas as pd
import numpy as np

# --- Create a DataFrame from scratch ---
df = pd.DataFrame({
    "name":       ["Arjun", "Meera", "Rohan", "Diya", "Karan"],
    "age":        [22, 24, 21, 23, 25],
    "city":       ["Mumbai", "Delhi", "Bangalore", "Chennai", "Kolkata"],
    "score":      [88, 92, 75, 96, 83],
    "passed":     [True, True, True, True, True]
})

# --- Load from CSV (the most common real-world pattern) ---
# df = pd.read_csv("students.csv")

# --- Explore the DataFrame ---
print(df.head())            # First 5 rows
print(df.shape)            # (5, 5) — rows, columns
print(df.info())            # Column types & null counts
print(df.describe())       # Statistical summary

# --- Filtering rows ---
top_students = df[df["score"] >= 90]
print("\nTop students (score ≥ 90):")
print(top_students[["name", "score"]])

# --- Adding a new column ---
df["grade"] = df["score"].apply(lambda x: "A" if x >= 90 else "B" if x >= 80 else "C")

# --- GroupBy: aggregate by category ---
avg_by_grade = df.groupby("grade")["score"].mean()
print("\nAverage score by grade:")
print(avg_by_grade)

# --- Handling missing values ---
df_with_nulls = df.copy()
df_with_nulls.loc[1, "score"] = np.nan
df_with_nulls["score"].fillna(df_with_nulls["score"].mean(), inplace=True)
print("\nNull values filled with mean score.")
Library 3

Matplotlib & Seaborn — The Storytellers

Numbers in a table are hard for human brains to process quickly. A well-designed chart communicates the same information in seconds. Matplotlib is Python's foundational plotting library — highly customizable and capable of creating virtually any type of chart, but requiring relatively verbose code for complex visuals. Seaborn is built on top of Matplotlib and provides a high-level interface for creating statistically informative and beautiful plots with far less code.

Think of Matplotlib as the oil paints and canvas, and Seaborn as having a skilled assistant who prepares the palette and suggests compositions for you. Together, they give you both complete control and convenient shortcuts.

Python — Matplotlib & Seaborn Visualizations
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# ===== LINE CHART: Monthly Sales =====
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
sales  = [12000, 15400, 14200, 18900, 21300, 19700]

plt.figure(figsize=(10, 5))
plt.plot(months, sales, color="#c8410a", linewidth=2.5, marker="o", markersize=8)
plt.fill_between(months, sales, alpha=0.15, color="#c8410a")
plt.title("Monthly Revenue (H1 2026)", fontsize=16, fontweight="bold")
plt.xlabel("Month")
plt.ylabel("Sales (₹)")
plt.grid(alpha=0.3)
plt.tight_layout()
plt.show()

# ===== SEABORN: Distribution Plot =====
np.random.seed(42)
scores = np.random.normal(75, 10, 200)

sns.set_theme(style="whitegrid")
plt.figure(figsize=(9, 5))
sns.histplot(scores, kde=True, color="steelblue", bins=25)
plt.axvline(scores.mean(), color="red", linestyle="--", label=f"Mean = {scores.mean():.1f}")
plt.title("Distribution of Student Scores", fontsize=15)
plt.legend()
plt.show()
📊 Common Chart Types & When to Use Them

Line chart — trends over time. Bar chart — comparing categories. Scatter plot — relationships between two variables. Histogram — distribution of a single variable. Heatmap — correlations between many variables at once.

Section 05

Real-World Applications

Data science is not an abstract academic exercise. It is actively transforming every industry you can think of. Understanding the real-world applications of the tools you're learning makes the hard work feel far more meaningful — you're not just printing arrays, you're learning the skills that power systems affecting millions of people.

🏥

Healthcare

Detecting cancerous cells in MRI scans, predicting patient readmission risk, and analyzing drug trial outcomes.

💰

Finance

Real-time fraud detection, algorithmic trading systems, credit risk assessment, and portfolio optimization.

🛒

E-Commerce

Amazon's "Customers also bought" and Netflix's recommendation engine run on collaborative filtering models built in Python.

Sports Analytics

Player performance tracking, injury prediction, optimal team selection, and match outcome forecasting.

🌿

Environment

Climate modelling, deforestation detection via satellite imagery, and air quality prediction systems.

🚗

Transportation

Route optimization for delivery services, autonomous vehicle perception systems, and traffic flow prediction.

Let's build a miniature real-world example — a recommendation-style function that, given a user's history of exam scores, identifies their weakest subject and suggests it for more study time:

Python — Mini Real-World Example
import pandas as pd

student_scores = pd.DataFrame({
    "student": ["Arjun", "Meera", "Rohan"],
    "Math":    [88, 72, 95],
    "Science": [76, 91, 63],
    "History": [65, 84, 70],
    "English": [92, 60, 78]
})

def recommend_study_focus(df):
    """For each student, find the subject they need to work on most."""
    subjects = ["Math", "Science", "History", "English"]
    for _, row in df.iterrows():
        scores = {sub: row[sub] for sub in subjects}
        weakest = min(scores, key=scores.get)
        print(f"📚 {row['student']:8s} → Focus on {weakest} (score: {scores[weakest]})")

recommend_study_focus(student_scores)
▶ Output
📚 Arjun → Focus on History (score: 65)
📚 Meera → Focus on English (score: 60)
📚 Rohan → Focus on Science (score: 63)
Section 06

Your Learning Roadmap

Learning data science can feel overwhelming when you look at the full picture — machine learning, deep learning, neural networks, NLP, computer vision... it goes on and on. The key is to not look at the full mountain from base camp. Instead, focus on the next 500 metres. Here's a structured, honest roadmap that will take you from zero to job-ready.

1

Master Python Basics (2–4 weeks)

Variables, data types, lists, dictionaries, loops, conditionals, and functions. Don't move on until you can write a function from scratch without looking anything up. Use platforms like freeCodeCamp or the official Python tutorial at docs.python.org.

2

Learn NumPy & Pandas (3–5 weeks)

Focus relentlessly on data manipulation. Load a CSV, explore it with .info() and .describe(), filter and sort rows, handle nulls, and group data. The official Pandas documentation and "10 Minutes to Pandas" guide are excellent starting points.

3

Data Visualization (2–3 weeks)

Learn to communicate findings visually. Master the key chart types in Matplotlib and Seaborn. Then challenge yourself: can you tell a compelling story with a dataset using only three charts? The storytelling aspect is as important as the technical skill.

4

Statistics Fundamentals (3–4 weeks)

Mean, median, standard deviation, probability distributions, hypothesis testing, and correlation. Without statistics, you'll be using tools you don't truly understand. Khan Academy's statistics course is genuinely excellent and free.

5

Introduction to Machine Learning (4–8 weeks)

Start with Scikit-learn. Understand supervised vs. unsupervised learning. Build your first linear regression model, then a decision tree classifier. The goal here isn't mastery — it's familiarity with the paradigm.

6

Build Real Projects & a Portfolio (Ongoing)

Nothing teaches like doing. Kaggle.com has hundreds of public datasets and competitions. Pick one topic you're genuinely curious about — cricket statistics, air pollution data, movie reviews — and analyse it end-to-end. Publish your notebooks on GitHub. That portfolio is worth more to employers than any certificate.

🎯 Mindset Reminder

Every senior data scientist you admire started exactly where you are now — confused by a KeyError, puzzled by an IndexError, wondering why their loop runs forever. Errors are not failures; they are the curriculum. Read them carefully, understand them, and you will always move forward.

Section 07

Final Thoughts

Here's the truth that no one tells you when you start learning data science: the hardest part isn't the math, or the code — it's the consistency. Data science rewards the person who sits down every day, even for just 30 minutes, and works through the discomfort of not knowing.

Python is, at its core, just a tool. A remarkable, powerful, beautiful tool — but a tool nonetheless. The real skill you're developing is thinking analytically: looking at messy, ambiguous information and asking the right questions. What patterns exist here? What's causing this anomaly? What prediction can I make from this trend? Python just helps you answer those questions efficiently.

Start with something personal. Analyse your Spotify listening history. Scrape your own study timetable and look for patterns. Visualize your monthly expenses. When the data connects to your real life, motivation comes naturally, and the learning accelerates.

Your first line of Python code is the beginning of something genuinely transformative. The world needs more people who can look at a dataset and understand what it means for real human beings. That skill — analytical, empathetic, and rigorous all at once — is what makes a great data scientist. Now go write some code.

Start Right Now

Your first Python program. Run it. Feel that.

# Your very first Python data science program
import random

your_name = "Future Data Scientist"
encouragements = [
    "Every expert was once a beginner.",
    "The data is waiting for you.",
    "Debug it. Learn it. Own it.",
    "Your curiosity is your superpower."
]

print(f"Welcome, {your_name}!")
print(random.choice(encouragements))

Explore More Topics

Dive deeper into data science and Python. Each topic builds on what you've learned in this guide.

🐼
Deep Dive

Pandas Mastery: Data Wrangling

Go beyond the basics — learn merging, reshaping, time-series operations, and advanced groupby patterns with real datasets.

Read more
📊
Visualization

Storytelling with Data

Master Seaborn, Plotly, and the art of communicating insights visually — turning raw numbers into compelling narratives.

Read more
🤖
Machine Learning

Your First ML Model with Scikit-learn

Train, evaluate, and deploy a classification model from scratch — understanding the full machine learning pipeline.

Read more

Comments & Suggestions

We value your perspective. Share your thoughts, ask questions, or suggest Python topics you'd like us to cover next.

0 / 500
💬

We Read Every Response

Our editorial team reviews all submissions and uses your feedback to shape future guides and articles.

✏️

Spot an Error?

We strive for accuracy in all code examples. If you notice a bug or outdated information, please let us know.

💡

Suggest a Topic

Have a Python or data science topic you'd like us to cover in depth? We take topic suggestions seriously.

Typical response within 2–3 business days