easycodelearning.com
🐍 Learn Python Online
Back to All Tutorials

Variables & Data Types

What is a Variable?

A variable is a named container that stores a value. Think of it as a labeled box where you can put data. In Python, you don't need to declare the type of variable - Python figures it out automatically.

Creating Variables

To create a variable, use the assignment operator =:

name = "Alice"
age = 25
height = 5.6
is_student = True

Data Types in Python

1. Integer (int)

Whole numbers without decimal points:

age = 25
score = 100
temperature = -5

2. Float

Numbers with decimal points:

price = 19.99
temperature = 36.6
pi = 3.14159

3. String (str)

Text data enclosed in quotes:

name = "Python"
message = 'Hello World'
greeting = """Multi-line
string example"""

4. Boolean (bool)

True or False values:

is_active = True
has_permission = False
is_valid = True

Variable Naming Rules

  • Must start with a letter or underscore (_)
  • Can contain letters, numbers, and underscores
  • Case-sensitive (age and Age are different)
  • Cannot use Python keywords (if, for, while, etc.)
  • Use descriptive names (good: user_age, bad: ua)

Good Variable Names:

user_name = "John"
student_age = 20
total_score = 95
is_logged_in = True

Bad Variable Names:

x = "John"  # Not descriptive
1name = "John"  # Starts with number
user-name = "John"  # Contains hyphen
if = 5  # Python keyword

Checking Variable Type

Use the type() function to check the data type:

name = "Alice"
age = 25
height = 5.6

print(type(name))    # 
print(type(age))     # 
print(type(height))  # 

Key Takeaways

  • Variables store data values
  • Python has four main data types: int, float, str, bool
  • Use descriptive variable names
  • Use type() to check variable types

Practice Time

Create variables for your personal information (name, age, height, is_student) and print them using the code editor!

Open Code Editor
Next: Math Operations