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.

5 min readUpdated 2026-11-29
In This Lesson
  • 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."

python
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:

python
print("Line 1: Starting my Python journey")
print("Line 2: This runs right after Line 1")

Screen output:

text
Line 1: Starting my Python journey
Line 2: This runs right after Line 1

Remembering 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.

python
name = "Alex"
print(name)
How Variables Point to Values

1. name = "Alex"

name"Alex"

2. name = "Riya"

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:

  • name is 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 word name.
  • print(name) looks inside the name variable and prints Alex!

Changing a Variable Later

Because it's called a variable (meaning it can vary), you can replace what is stored inside it anytime:

python
score = 10
print(score)   # Prints: 10

score = 25
print(score)   # Prints: 25

You can even use a variable's old value to calculate its new value:

python
coins = 10
coins = coins + 5
print(coins)   # Prints: 15

Why did that print 15? Because Python always solves the right side first:

  1. Look up coins → finds 10
  2. Calculate 10 + 5 → 15
  3. Save 15 back into coins!

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:

TypePython NameExamplesUsed For
Text (String)str"Alex", 'Coffee'Names, messages, emails
Whole Number (Integer)int5, 0, -12Counts, ages, scores
Decimal Number (Float)float4.50, 3.14, -0.5Prices, weights, ratings
Yes/No (Boolean)boolTrue, FalseLogin status, toggles (must start with capital T or F!)

Important: "5" Is Text, NOT the Number 5

Look carefully at the difference quotes make:

python
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(...):

python
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}:

python
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)

3 Mistakes Everyone Makes at First
  1. Forgetting quotes around text: city = London gives NameError: name 'London' is not defined because Python looks for a variable named London. Always write city = "London".
  2. Adding text and a number with +: "Coffees: " + 2 gives a TypeError. Use an f-string instead: f"Coffees: {2}".
  3. Spaces or dashes in variable names: my-score = 10 or my score = 10 breaks. 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!

Python 3Experiment: Variables, Math & f-Strings
Output

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

Check Your Understanding

What is printed by points = 10; points = points + 5; print(points)?

Check Your Understanding

Which line prints Age: 20 without any error?