📚

Assign Variables

What is a Variable?

Think of a variable as a labeled box. You give the box a name, put a value inside it, and later you can use that name to get the value back.

In Python, you create a variable by typing: name = value

age = 25

This creates a box labeled

age
and puts the number
25
inside it. Now anywhere in your code, when you write
age
, Python knows you mean
25
.

The Three Main Types of Data

Python has different types of data, and it's important to understand them:

  • Integers (int) — whole numbers:
    25
    ,
    100
    ,
    -3
  • Floats (float) — decimal numbers:
    5.9
    ,
    3.14
    ,
    -0.5
  • Strings (str) — text, always in quotes:
    "Alice"
    ,
    'hello'
name = "Alice"     # This is a string (text)
age = 25           # This is an integer (whole number)
height = 5.9       # This is a float (decimal number)

print(name)        # → Alice
print(age)         # → 25
print(height)      # → 5.9

Notice: strings need quotes, but numbers don't. If you wrote

name = Alice
without quotes, Python would think
Alice
is another variable name — and crash because it doesn't exist!

💡

💡 Naming rules: Variable names can contain letters, numbers, and underscores. They must start with a letter or underscore (not a number). Use descriptive names like

user_age
instead of
x
.

main.pySandbox
main.py