Python for Excel users · Lesson 1 of 14

Python for Excel users: why and when

What Python adds to what you already do in Excel, when Excel is still the better tool, and the small amount of Python you need to start.

About 14 minutes · code runs in your browser, nothing to install

If you can build a VLOOKUP, a pivot table and a chart in Excel, you already think in tables, columns and formulas. Python with the pandas library works with the same ideas. This course takes the Excel tasks you know and shows the Python way next to each one. For a refresher on the Excel side, the function pages on Excelversity explain each formula.

Why add Python if you know Excel?

  • Repeat work in one click. A report you rebuild every month with 30 clicks becomes a script you run again on next month's file.
  • Big files. A sheet stops at 1,048,576 rows and gets slow long before that. pandas handles millions of rows on a normal laptop.
  • Many files at once. Combine 50 workbooks from a folder with a short loop instead of copy and paste.
  • Every step is written down. A script shows exactly what happened to the data, so a colleague (or you, in six months) can check it.
  • No broken formulas. Nobody can type over a formula in row 812 by mistake.

When Excel is still the better tool

  • Quick one-off sums and a glance at a small table.
  • Typing and editing data by hand.
  • Sharing a file with people who will click around in it.
  • Nicely formatted print layouts.

Most people use both: Python does the heavy, repeated work and writes the result to an Excel file that everyone can open.

The minimum Python you need

A variable is a named box for a value, like a named cell. You create it with =. Text goes in quotes, numbers do not. print() shows a value on the screen.

Pythoneditable
price = 19.99
quantity = 3
product = "Desk lamp"
total = price * quantity
print(product)
print(total)
print(round(total, 2))
Output
Desk lamp
59.97
59.97

Like typing 19.99 in B1, 3 in B2, =B1*B2 in B3 and =ROUND(B3, 2) in B4. Each variable is a cell with a name instead of an address.

A list holds several values in order, like one column of cells. Python has built-in versions of =SUM(), =MAX(), =MIN() and =COUNT(): sum(), max(), min() and len().

Pythoneditable
sales = [120, 85, 240, 60, 155]
print("SUM:", sum(sales))
print("MAX:", max(sales))
print("MIN:", min(sales))
print("COUNT:", len(sales))
print("AVERAGE:", sum(sales) / len(sales))
Output
SUM: 660
MAX: 240
MIN: 60
COUNT: 5
AVERAGE: 132.0

=AVERAGE(A1:A5) is simply the sum divided by the count.

An f-strings builds text from values, like ="Total: "&TEXT(B3,"0.00"). Put f before the quotes and the value in curly braces; :.2f means two decimals.

Pythoneditable
total = 59.97
print(f"Total: €{total:.2f}")
print(f"Total with 24% VAT: €{total * 1.24:.2f}")
Output
Total: €59.97
Total with 24% VAT: €74.36

Meet pandas

pandas is the library that gives Python a spreadsheet: a DataFrame, a table with column headers. import pandas as pd loads it; pd is the short name everyone uses.

Pythoneditable
import pandas as pd

df = pd.DataFrame({
    "Product": ["Lamp", "Chair", "Desk"],
    "Price": [19.99, 45.00, 120.00],
    "Qty": [3, 2, 1],
})
print(df)
print("Total units:", df["Qty"].sum())
Output
  Product   Price  Qty
0    Lamp   19.99    3
1   Chair   45.00    2
2    Desk  120.00    1
Total units: 6

The numbers 0, 1, 2 on the left are row labels, a bit like Excel's row numbers (more in the next lesson).

✎ Exercise

A shop sold these amounts this week. Print the total, the largest sale and the average rounded to 2 decimals, each on its own line, like Total: 512.5.

Hint: Use sum(), max() and len(). The average is sum(week) / len(week), then round(..., 2).

Your codewrite your answer, then Run
week = [80.5, 112.0, 95.25, 140.0, 84.75]
# your code here

Expected output:

Total: 512.5
Largest: 140.0
Average: 102.5
Show a solution
Solutioneditable
week = [80.5, 112.0, 95.25, 140.0, 84.75]
print("Total:", sum(week))
print("Largest:", max(week))
print("Average:", round(sum(week) / len(week), 2))
Output
Total: 512.5
Largest: 140.0
Average: 102.5

Check yourself

1. Which task is usually still quicker in Excel than in Python?

2. What is the Python version of =SUM(A1:A5) for a list called sales?

3. What is a pandas DataFrame?

Spotted a mistake on this page? Tell us