Python Essentials You Need for Data Work
You do not need to learn every corner of Python to do AI and data science. You need a working knowledge of about eight building blocks. This lesson covers all of them, with examples you can paste straight into a Colab cell.
What You'll Learn
- The Python "vocabulary" needed for data work: variables, types, lists, dicts, loops, functions
- How to import libraries and read documentation
- Common beginner mistakes and how AI helps you fix them
- A short prompt template for asking AI to teach you a Python concept
1. Variables and Types
A variable is a name pointing at a value.
name = "Alice"
age = 22
gpa = 3.85
is_enrolled = True
Python figures out the type for you. Strings have quotes, numbers do not, and True and False are capitalized. The four types you will see most are str, int, float, and bool. You can check a type:
type(gpa) # <class 'float'>
2. Lists
A list is an ordered collection. They are everywhere in Python.
scores = [88, 92, 79, 95, 84]
print(scores[0]) # 88 -- indexing starts at zero
print(scores[-1]) # 84 -- negative indexes count from the end
print(len(scores)) # 5
print(sum(scores) / len(scores)) # 87.6 -- average
Slicing pulls a sub-list:
scores[0:3] # [88, 92, 79]
scores[2:] # [79, 95, 84]
Adding and removing:
scores.append(100) # add to the end
scores.remove(79) # remove first occurrence
3. Dictionaries
A dictionary maps keys to values. Think of it like a labeled box.
student = {
"name": "Alice",
"age": 22,
"courses": ["AI", "Statistics", "Linear Algebra"],
}
print(student["name"]) # Alice
student["graduated"] = False # add a new key
Dictionaries become important the moment you start working with JSON data — and most APIs (including AI APIs) return JSON.
4. Loops
A for loop runs code once for each item in a list or other iterable.
for score in scores:
if score >= 90:
print(f"{score} is an A")
else:
print(f"{score} is not an A")
if / elif / else give you decisions. Indentation defines what is "inside" the loop or the if. Use four spaces.
5. Functions
A function is a reusable chunk of code.
def average(numbers):
return sum(numbers) / len(numbers)
print(average([88, 92, 79])) # 86.33...
In data science, you will often write small helper functions to clean up a column, compute a metric, or process a row.
6. Importing Libraries
Real work happens through libraries. You bring them in with import.
import pandas as pd # alias pandas to pd by convention
import numpy as np # alias numpy to np by convention
import matplotlib.pyplot as plt
When a tutorial or AI gives you code that uses pd.something(...), that something is a function defined in pandas. The pd. prefix tells Python which library to look in.
7. Reading Files
You can read a CSV in two lines using pandas.
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv")
df.head()
df is a DataFrame — a table with rows and columns. We will spend an entire lesson on this, but the takeaway here is that one line of import plus one line of read_csv gets you working with real data.
8. f-strings (the modern way to format text)
name = "Alice"
score = 92.5
print(f"{name} scored {score} on the final.")
The f prefix tells Python to substitute variables inside the curly braces. It is cleaner than + concatenation and far easier than the older %-style formatting you may see in old tutorials.
Common Beginner Mistakes (and How AI Helps)
Here are three errors you will hit in your first week, and exactly what to do about each.
IndentationError — you mixed tabs and spaces, or did not indent something. Fix: in Colab, press Tools → Settings → Editor and turn on "Indent with 4 spaces". Most editors do this automatically.
KeyError: 'name' — you tried to read a dictionary key that does not exist. Fix: print the keys first with student.keys() and check spelling.
ModuleNotFoundError: No module named 'sklearn' — the library is not installed. In Colab, run !pip install scikit-learn in a cell (the ! runs a shell command). Locally, pip install scikit-learn from your terminal.
When you hit an error, do not panic. Paste it into Claude or ChatGPT with this prompt:
I am a beginner learning Python for data science. Here is the code I tried to run and the error I got. Please explain in plain English what the error means, why it happened, and how to fix it. Do not just give me corrected code — teach me.
The "teach me" instruction is important. You want explanations, not just patches.
A Prompt Template for Concept Questions
Here is a prompt to keep in your notes for any Python concept you do not understand:
I am a complete beginner learning Python for AI and data science. Please explain
[concept]in three parts: (1) what it is in one sentence, (2) a tiny code example with comments, (3) a real situation where I would use it in data science. Keep it simple — assume I have never heard of[concept]before.
Try it now. Drop "list comprehension" or "lambda function" into the prompt and see what comes back.
A Practical Drill
Run this in Colab. Read it line by line, predict what each will print, then run it.
students = [
{"name": "Alice", "scores": [88, 92, 95]},
{"name": "Bob", "scores": [70, 75, 78]},
{"name": "Cara", "scores": [99, 100, 98]},
]
def average(numbers):
return sum(numbers) / len(numbers)
for s in students:
avg = average(s["scores"])
grade = "A" if avg >= 90 else "B" if avg >= 80 else "C"
print(f"{s['name']}: average {avg:.1f}, grade {grade}")
If you can predict what this prints, you have enough Python to keep moving. If you cannot, paste it into ChatGPT and ask the AI to walk you through each line.
Key Takeaways
- You only need eight building blocks to start: variables, types, lists, dicts, loops, functions, imports, f-strings
- Indentation defines code blocks in Python — use four spaces
- The
import library as aliaspattern is everywhere; learn the common aliases (pd,np,plt) - When you hit an error, paste the full message into AI with "teach me, do not just patch"
- Practice by reading code line by line and predicting what each line does before running it

