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?
Creating Variables
# 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) # TrueThe 4 Main Data Types
Checking Types
# 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)
# 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.0Variable 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).
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