Academy
ProgrammingPython for BeginnersVariables & Data Types

Variables & Data Types

The Problem With Lesson 1

Every program from the previous lesson had a flaw: it forgot everything the moment it finished.

print(3 + 7) calculates 10 and displays it. But that 10 is gone instantly. The next line has no idea it ever existed. For a program that prints one thing and ends, that's fine. For anything real — a shopping cart that remembers your items, a quiz that tracks your score, a form that holds your name — you need the computer to hold on to values.

That's what a variable is.

A Label on a Box

Think of your computer's memory as a long row of storage boxes. Each box can hold one value. A variable is a label you stick on a box so you can find it again later.

name = "Priya"

This one instruction does three things:

  • Creates a box in memory
  • Puts the value "Priya" inside it
  • Sticks the label name on it

From this point on, wherever you write name, Python opens that box and uses whatever is inside.

The box's label doesn't change — only its contents do. This is called reassignment, and it's one of the most common operations you'll ever write.

The = Sign Is Not Mathematics

In Python, = does not mean "is equal to." It means "store this value in this box."

The right side is always evaluated first. Whatever it produces, that result goes into the box on the left.

Line 2 reads: open the score box, take out 10, add 5, get 15, put 15 back in. The label stays. The contents change.

Mathematics would call score = score + 5 nonsense. Python calls it Tuesday.

Four Kinds of Values

Python distinguishes between different kinds of data. This matters because what you can do with a value depends entirely on what kind it is.

Integers — whole numbers

age = 25
floor = 7
year = 2024

Whole numbers, positive or negative, no decimal point. Python calls these int.

Floats — decimal numbers

price = 149.99
temperature = 36.6
pi = 3.14159

Any number with a decimal point is a float. Even 1.0 is a float — the point is what matters.

Strings — text

city = "Kolkata"
message = "Good morning"

Any text inside quotes is a string — a sequence of characters. You can use single quotes or double quotes; Python accepts both. Just be consistent within one string.

Booleans — true or false

is_logged_in = True
has_paid = False

A boolean holds exactly one of two values: True or False. Note the capital letters — Python is strict about this. true with a lowercase t means nothing to Python; True with a capital T is a real value.

Booleans seem small, but they are the foundation of every decision a program ever makes.


Finding Out the Type

Python has a built-in command called type() that tells you exactly what kind a value is.

The output uses Python's internal names: int, float, str, bool. You'll see these names everywhere as you learn more Python.

Types Are Not Interchangeable

This is the critical thing to understand early. Python treats different types differently — and it won't silently convert them for you when it causes confusion.

This crashes. "5" is a string. 3 is an integer. You can't add text to a number the way you'd add two numbers. Python stops and tells you clearly.

Fix it by making both the same type:

Now both are integers and the addition works. The value in the box matters, but so does the kind of value.

Naming Your Variables

You can name a variable almost anything. But names carry meaning, and meaning matters enormously when you return to code you wrote three weeks ago.

Both programs do the same calculation. One tells a story. The other is a puzzle. Always write the story.

A few rules Python enforces:

  • Names can contain letters, digits, and _ (underscore)
  • Names cannot start with a digit
  • Names are case-sensitive: score, Score, and SCORE are three completely different variables
  • Python convention: use lowercase with underscores — first_name, not firstName

What Happens If You Use a Variable That Doesn't Exist?

A NameError. Python searched every labelled box in memory, found nothing called phone_number, and stopped.

This is the computer being precise, not cruel. It would rather crash with a clear message than silently use the wrong value and give you a wrong answer with no warning.

What You Have Learned

Variables are how a program builds memory. Without them, every instruction is an island.

The key ideas:

  • A variable is a named box in memory — a label attached to a stored value
  • = means store this value, not is equal to
  • Python has four basic data types: int, float, str, bool
  • Types matter — mixing incompatible types causes errors
  • Name variables for what they contain, not for what they're doing
  • Using an undeclared variable causes NameError

In the next lesson, you'll learn how to do things with these values — arithmetic, text manipulation, and the rules Python follows when calculating.