1. First Steps & Data Basics
Variables, Types & Your First Python Code
Learn Python from absolute zero: print your first message, store information in variables like labeled containers, understand text vs numbers, and write clean sentences with f-strings.
- Display output with print() and understand top-to-bottom code execution
- Store and update values using variables like labeled sticky notes
- Distinguish str, int, float, and bool types and format sentences with f-strings
If you have never written a single line of code before, welcome—you are in the exact right place.
Programming is simply giving a computer clear, step-by-step instructions in a language it understands. Python is loved by beginners and professionals alike because its instructions look a lot like everyday English.
Making Python Talk (print)
Every program needs a way to show results on your screen. In Python, the word print tells the computer: "Display whatever is inside the parentheses."
print("Hello, world!")Notice the double quotes "..." around Hello, world!. Whenever you want Python to treat words as plain text (not as a command), you wrap them in quotes.
When Python runs a file, it reads from top to bottom, one line at a time:
print("Line 1: Starting my Python journey")
print("Line 2: This runs right after Line 1")Screen output:
Line 1: Starting my Python journey
Line 2: This runs right after Line 1Remembering Information with Variables
Printing fixed text is fine, but real apps need to remember changing information—like a player's score in a game, a user's name, or the price of a coffee.
Think of a variable like a labeled jar in your kitchen. You put "Sugar" inside a jar and stick the label jar on the outside so you can grab it whenever you need it.
name = "Alex"
print(name)1. name = "Alex"
2. name = "Riya"
Assigning name = 'Riya' moves the sticky-note label to a new value without editing the old text.
Let's read Line 1 slowly:
nameis the label we chose.=does not mean "equals" like in math class. In Python,=means "save the value on the right inside the label on the left.""Alex"is the value being saved.
Notice Line 2 has no quotes around name:
print("name")would literally print the wordname.print(name)looks inside thenamevariable and printsAlex!
Changing a Variable Later
Because it's called a variable (meaning it can vary), you can replace what is stored inside it anytime:
score = 10
print(score) # Prints: 10
score = 25
print(score) # Prints: 25You can even use a variable's old value to calculate its new value:
coins = 10
coins = coins + 5
print(coins) # Prints: 15Why did that print 15? Because Python always solves the right side first:
- Look up
coins→ finds10 - Calculate
10 + 5→15 - Save
15back intocoins!
The 4 Basic Types of Data
Computers treat text and numbers differently. Think about why: you can divide two numbers (10 / 2), whereas dividing two words ("hello" / "world") makes no sense!
In Python, almost everything you store at the beginning is one of these 4 types:
| Type | Python Name | Examples | Used For |
|---|---|---|---|
| Text (String) | str | "Alex", 'Coffee' | Names, messages, emails |
| Whole Number (Integer) | int | 5, 0, -12 | Counts, ages, scores |
| Decimal Number (Float) | float | 4.50, 3.14, -0.5 | Prices, weights, ratings |
| Yes/No (Boolean) | bool | True, False | Login status, toggles (must start with capital T or F!) |
Important: "5" Is Text, NOT the Number 5
Look carefully at the difference quotes make:
print(5 + 5) # Prints: 10 (math addition)
print("5" + "5") # Prints: 55 (text glued together side by side!)If you aren't sure what type a variable holds, ask Python with type(...):
price = 4.50
print(type(price)) # Prints: <class 'float'>Mixing Variables Into Sentences (f-Strings)
Suppose you have name = "Alex" and coffees = 2, and you want to print:
Alex ordered 2 coffees.
The cleanest way in Python is an f-string (short for formatted string). Simply put the letter f right before the opening quote, and place any variable name inside {curly braces}:
name = "Alex"
coffees = 2
print(f"{name} ordered {coffees} coffees.")Python automatically replaces {name} with Alex and {coffees} with 2—even though coffees is a number!
Common Beginner Mistakes (So You Don't Get Stuck)
- Forgetting quotes around text:
city = LondongivesNameError: name 'London' is not definedbecause Python looks for a variable namedLondon. Always writecity = "London". - Adding text and a number with
+:"Coffees: " + 2gives aTypeError. Use an f-string instead:f"Coffees: {2}". - Spaces or dashes in variable names:
my-score = 10ormy score = 10breaks. Use underscores instead:my_score = 10.
Try It Yourself in the Playground
First, predict in your head what each print() line below will show, then click Run Code to check!
Click “Run Code” to execute this snippet right in your browser.
Mini-Challenge: Update the code above to add a $1.00 tip (tip = 1.00), calculate final_total = total + tip, and print: "Final total with tip: $10.0".
Quick Checkpoints
What is printed by points = 10; points = points + 5; print(points)?
Which line prints Age: 20 without any error?