8 min read
🐍beginner

Python Variables & Data Types — Storing Information

Understand how Python stores different kinds of information — numbers, text, true/false values, and more. Written for complete beginners.

What Are Variables?

Imagine you have a bunch of labeled boxes in your room. One box says 'Name' and inside it you put a piece of paper that says 'Aarav'. Another box says 'Age' and inside you put the number 12. That's exactly what variables are in Python — labeled containers that hold information. When you write code, you need to remember things: the player's score, someone's name, whether the game is over. Variables let your program remember all of this.

Creating Variables

python
# Just pick a name, use =, and give it a value!
my_name = "Aarav"
my_age = 12
favorite_number = 3.14
loves_coding = True

# Now Python remembers all of this!
print(my_name)        # Aarav
print(my_age)          # 12
print(favorite_number) # 3.14
print(loves_coding)    # True

The 4 Main Data Types

Every piece of data in Python has a type. Think of it like different kinds of boxes: • String (str) — Text, always wrapped in quotes. Like "Hello" or "pizza". Even "123" is a string if it has quotes! • Integer (int) — Whole numbers without decimals. Like 5, 100, or -3. Used for counting things. • Float (float) — Numbers WITH decimals. Like 3.14, 99.9, or 0.5. Used for measurements and money. • Boolean (bool) — Only two possible values: True or False. Like an on/off switch. Used for yes/no decisions. Why does this matter? Because Python treats each type differently. You can add two numbers (5 + 3 = 8) but if you 'add' two strings, Python glues them together ("Hi" + "There" = "HiThere").

Checking Types

python
# Use type() to check what kind of data you have
name = "Priya"
age = 11
height = 4.5
is_student = True

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

# Surprise! Numbers in quotes are strings, not numbers
tricky = "42"
print(type(tricky))  # <class 'str'> (not int!)

Changing Types (Type Conversion)

python
# Sometimes you need to convert between types
age_text = "12"          # This is a string
age_number = int(age_text)  # Now it's an integer!
print(age_number + 1)      # 13 (math works now!)

# Convert number to string
score = 100
message = "Your score is " + str(score)
print(message)  # Your score is 100

# Convert to float
whole = 5
decimal = float(whole)
print(decimal)  # 5.0
Pro Tip

Variable names should describe what they hold. Use 'player_score' instead of 'x', and 'user_name' instead of 'n'. Your future self will thank you when reading the code later! Also, Python variable names can't start with a number and can't have spaces — use underscores instead (like_this).

Try It Yourself

Create variables for a movie: its title (string), release year (integer), rating out of 10 (float), and whether you've watched it (boolean). Print each one along with its type. Then try converting the year to a string and adding it to a sentence like 'This movie came out in 2024'.

Ready to build?

Put what you learned into practice — pick a project and start coding.

Start Building Free