Python · Week 1 of 4 · 4–5 hours
Python Basics
Run Python code and understand variables and the basic data types.
Learning objectives
By the end of this week you will be able to:
- Open a notebook and run code cells.
- Create variables and follow naming rules.
- Use integers, floats, strings and booleans.
- Use print(), type() and basic string methods.
Lessons
Getting Started with Notebooks
A notebook (Google Colab or Jupyter) mixes code cells and text cells in one document. Type code in a cell and press Shift+Enter to run it; the result appears directly underneath. Notebooks are perfect for analysis because your code, results and notes stay together.
Your first program:
print("Hello, data!")
Lines starting with # are comments; Python ignores them. Use them to explain what your code does.
Python Basics: Variables and Data Types
A variable is a name that stores a value. You create one with =:
store_name = "Downtown" units_sold = 42 unit_price = 19.99 is_open = True
Naming rules: start with a letter, no spaces (use underscores), and names are case sensitive (Sales and sales are different). Choose names that describe the value.
The four basic types:
| Type | Example | Used for |
|---|---|---|
int (integer) | 42 | whole numbers, counts |
float | 19.99 | decimals, money, measurements |
str (string) | "Downtown" | text, always in quotes |
bool (boolean) | True / False | yes/no facts |
Check a type with type(unit_price). Convert between types with int(), float() and str(): float("19.99") turns text into a number. This matters because data read from files often arrives as text.
Math: + - * /, plus ** (power), // (whole-number division) and % (remainder). revenue = units_sold * unit_price
Working with strings: .upper(), .lower(), .strip() (remove spaces at the ends), .replace("a","b"). F-strings put variables inside text: print(f"{store_name} sold {units_sold} units").
Watch
- VideoLearn Python - Full Course for Beginners (freeCodeCamp)
Watch roughly the first hour: setup, variables, data types, strings and numbers.
- VideoPython Tutorial for Beginners 2: Strings - Working with Textual Data (Corey Schafer)
Clear, slow explanation of string methods and f-strings.
Practice
In a new notebook, create variables for a small shop: shop name, number of items sold, price per item and whether a discount applies. Calculate revenue, print a sentence using an f-string, check the type of each variable, and convert the text "125.50" into a float and add 10 to it.
Check your understanding
Try each question first, then open it to see the answer.
What type is 3.0?
float.
What does " north ".strip().title() return?
"North".
Why might you need float("19.99")?
Because values read from files are often text and must be converted before doing math.
Assignment
Submit your notebook (share link or .ipynb file) with every cell run and a comment above each one explaining what it does.