Your First Python Programs

Variables and Data Types

6 min read

Programs need to remember things

Imagine an app that tracks your fitness. It needs to remember your name, your step count, your weight, and whether you've completed today's workout. Without a way to store that information, the app would forget everything the moment it started.

In Python, variables are how programs store and remember information.


What is a variable?

A variable is a named container that holds a value. You give it a name, and Python remembers what's inside it.

name = "Alex"
age = 20
price = 9.99
is_student = True
  • name holds the text "Alex"
  • age holds the number 20
  • price holds the decimal number 9.99
  • is_student holds the value True

You can then use those names anywhere in your program:

print(name)        # Alex
print("Age:", age) # Age: 20
print("Price:", price)        # Price: 9.99
print("Is student?", is_student)  # Is student? True

The assignment operator: =

The = sign in Python does not mean "equals" the way it does in math. It means "assign this value to this variable."

Read it right to left:

age = 20

"Put the value 20 into the container called age."

This is a common source of confusion for beginners. In Python:

  • = means assign (store a value)
  • == means compare (check if two values are equal) — more on this later

The four core data types

Python needs to know what kind of value a variable holds — not just its value. The category of value is called a data type (or just "type").

The four types you'll use constantly:

1. String (str) — text

A string is any sequence of characters enclosed in quotes.

first_name = "Taylor"
city = "New York"
message = 'Welcome to Python!'

Strings can contain letters, numbers, spaces, symbols — anything. The quotes are what tell Python this is text, not code.

2. Integer (int) — whole numbers

An integer is a whole number with no decimal point.

quantity = 12
year = 2024
temperature = -5
score = 100

Use integers for counts, ages, rankings, and anything that's naturally a whole number.

3. Float (float) — decimal numbers

A float is a number with a decimal point.

price = 9.99
height = 5.11
temperature = 23.5
pi = 3.14159

The name "float" comes from "floating point" — a technical description of how computers store decimal numbers.

4. Boolean (bool) — true or false

A boolean can only ever be one of two values: True or False.

is_student = True
has_membership = False
is_open = True

Note the capital letters — True and False must be capitalized in Python. Booleans are essential for decisions: "is the user logged in?", "has this task been completed?", "does the product have stock?"


Checking a variable's type

Python has a built-in function called type() that tells you what kind of value a variable holds:

name = "Alex"
age = 20
price = 9.99
is_student = True

print(type(name))       # <class 'str'>
print(type(age))        # <class 'int'>
print(type(price))      # <class 'float'>
print(type(is_student)) # <class 'bool'>

You don't need to declare the type yourself — Python figures it out from the value you assign. This is called dynamic typing.


Variables can change

The word "variable" literally means "able to vary." You can reassign a variable to a new value at any time:

score = 85
print(score)  # 85

score = 92
print(score)  # 92

The old value is replaced by the new one. Python doesn't remember that score was ever 85 once you've assigned 92.


Doing math with numbers

You can use standard math operators with integer and float variables:

quantity = 12
price = 9.99

total_cost = quantity * price
print("Total cost:", total_cost)  # Total cost: 119.88

Python supports all the basic math operations:

SymbolOperationExampleResult
+Addition5 + 38
-Subtraction10 - 46
*Multiplication3 * 721
/Division15 / 43.75
//Integer division15 // 43
%Remainder15 % 43
**Exponent (power)2 ** 8256

Booleans and decisions

Booleans become especially useful when combined with if statements (covered in a later lesson). Here's a preview:

has_membership = False

if has_membership == False:
    print("Access granted")
else:
    print("Please sign up for membership")

Output:

Access granted

The == operator (two equals signs) compares the value of has_membership with False. Because they match, the first branch runs.


Naming rules for variables

Python is flexible about names, but there are a few rules:

Must follow:

  • Names can contain letters, numbers, and underscores _
  • Names cannot start with a number
  • Names cannot have spaces (use _ instead)
  • Names cannot be Python reserved words (if, for, True, print, etc.)

Should follow (conventions):

  • Use lowercase letters with underscores for multi-word names: first_name, total_cost
  • Choose names that describe what the variable holds: age not x, shopping_cart not sc
# Good variable names
user_age = 25
item_count = 4
is_logged_in = True

# Confusing variable names (works, but avoid)
x = 25
y = 4
z = True

A real-world example

Here's a complete example using all four types to model a simple user profile:

# User profile
name = "Priya"
age = 22
account_balance = 148.75
is_verified = True

print("Name:", name)
print("Age:", age)
print("Balance: $" + str(account_balance))
print("Verified:", is_verified)

Output:

Name: Priya
Age: 22
Balance: $148.75
Verified: True

Note: str(account_balance) converts the float to a string so it can be joined with the "Balance: $" text. You can't join a string and a number directly — they're different types.


Key takeaway

Variables are named containers that store values. Python has four core data types: strings (text), integers (whole numbers), floats (decimals), and booleans (True/False). Python figures out the type automatically from the value you assign — you don't need to declare it.

What's next?

Strings deserve a deeper look. They're one of the most-used data types in all of Python, and they come with a powerful set of built-in tools for cleaning, transforming, and searching text.