Python Basics for Data Science
Why Start With Plain Python?
Pandas, NumPy and scikit-learn are all written for Python. Before you can use them well, you need to be comfortable with the core language: variables, lists, dictionaries, loops and functions. These are the building blocks every data scientist uses dozens of times a day, whether they work at Access Bank, Flutterwave, Spotify or Google.
This lesson focuses on the parts of Python you will actually use for data work. You do not need to master every feature of the language to be productive.
Variables and Data Types
A variable is a name that points to a value. Python works out the type for you.
customer_name = "Chiamaka Obi" # str (text)
account_balance = 245000.50 # float (decimal number)
transactions_this_month = 37 # int (whole number)
is_premium = True # bool (True or False)
print(type(account_balance)) # <class 'float'>
The four types above cover most raw data you will meet. When you later load a CSV into pandas, each column will have one of these types (plus dates). Knowing the difference matters: "245000" in quotes is text, and you cannot add it to a number until you convert it with float().
Lists: Ordered Collections
A list holds many values in order. Think of it as one column of a spreadsheet.
daily_sales = [120000, 98000, 143500, 110250, 167000]
print(daily_sales[0]) # 120000 (first item, indexes start at 0)
print(daily_sales[-1]) # 167000 (last item)
print(daily_sales[1:3]) # [98000, 143500] (a slice)
print(len(daily_sales)) # 5
print(sum(daily_sales) / len(daily_sales)) # average: 127750.0
daily_sales.append(155000) # add a new day
Dictionaries: Labelled Data
A dictionary stores key: value pairs. It is how you represent a single record, like one row in a table or one JSON object from an API.
customer = {
"name": "Tunde Bakare",
"bank": "Zenith Bank",
"city": "Lagos",
"monthly_spend": 185000,
}
print(customer["bank"]) # Zenith Bank
customer["segment"] = "high_value" # add a new key
print(customer.get("age", "unknown")) # safe lookup with a default
A list of dictionaries is the most common shape of raw data you will receive. It maps directly onto a pandas DataFrame, which you will meet in lesson 3.
customers = [
{"name": "Tunde", "city": "Lagos", "spend": 185000},
{"name": "Aisha", "city": "Kano", "spend": 92000},
{"name": "Emeka", "city": "Enugu", "spend": 143000},
]
Loops and Conditions
Loops let you repeat an action for every item in a collection. Conditions let you make decisions.
for c in customers:
if c["spend"] > 100000:
print(c["name"], "is a high spender")
else:
print(c["name"], "is a regular customer")
List comprehensions
Data scientists love list comprehensions because they turn a four-line loop into one readable line:
high_spenders = [c["name"] for c in customers if c["spend"] > 100000]
# ['Tunde', 'Emeka']
Functions
A function packages logic you want to reuse. When you clean data, you will write small functions like this all the time:
def naira_to_usd(amount_ngn, rate=1550):
"""Convert a Naira amount to US dollars at a given rate."""
return round(amount_ngn / rate, 2)
print(naira_to_usd(185000)) # 119.35
print(naira_to_usd(185000, rate=1600))
Notice the default argument rate=1550. Exchange rates move, so making the rate a parameter keeps the function useful when the CBN rate changes.
How This Connects to Pandas
Everything above appears again once you use pandas:
| Plain Python | Pandas equivalent |
|---|---|
| List of values | Series (one column) |
| List of dictionaries | DataFrame (a table) |
for loop with if | Boolean filtering, e.g. df[df["spend"] > 100000] |
| Function | df["col"].apply(my_function) |
Pandas is faster and shorter, but it builds on the same ideas. If you understand lists, dictionaries and loops, pandas will make sense quickly.
Tip: Practise in the browser lab below, then open a free Google Colab notebook (colab.research.google.com) and type every code example from this lesson yourself. Typing code out builds memory far faster than reading it.
Try it yourself
Key Takeaways
- The four core types you will meet in raw data are str, int, float and bool; text that looks like a number must be converted before doing maths.
- Lists hold ordered values and are zero-indexed; negative indexes count from the end.
- Dictionaries hold labelled key: value pairs, and a list of dictionaries is the natural shape of tabular or API data.
- Loops, conditions and list comprehensions let you filter and transform collections; pandas replaces most of these loops with faster vectorised operations.
- Small reusable functions with sensible default arguments are the foundation of clean, repeatable data-cleaning code.
Quick Quiz
1.Which Python structure is the best match for one row of customer data, such as name, bank and monthly spend?
2.What does this list comprehension return? [s for s in [50, 200, 80, 310] if s > 100]
3.Why is it useful to give a currency conversion function a default rate parameter, such as rate=1550?
Ready to go further?
CareerEx gives you structured 12-week training, live classes every Saturday and Sunday, real tutor feedback, and a certificate. Join the next cohort.
Join CareerEx