Variables & Data Types
Master Python variables, understand different data types, and learn how mutability works.
Master Python variables, understand different data types, and learn how mutability works. This hands-on tutorial focuses on practical implementation of variables & data types concepts.
Variables & Data Types
Variables are the building blocks of any program. In this lesson, you will learn how to store data, what types of data exist, and how to differentiate between mutable and immutable objects.
What are Variables?
In many languages, variables are like boxes that hold values. In Python, it's more accurate to think of them as labels attached to objects in memory.
Variable Naming Rules
✅ Valid Names
❌ Invalid Names
# These will cause errors:
# 2users = 10 # Cannot start with number
# user-name = "Bob" # No hyphens
# class = "Math" # Cannot use keywords
# user name = "Sue" # No spaces
Naming Conventions
Python Data Types
1. Numbers
Integers (int)
Floats (float)
2. Strings (str)
3. Booleans (bool)
4. None Type
5. Lists (list)
Ordered, mutable collections of items.
6. Tuples (tuple)
Ordered, immutable collections. Once created, they cannot be changed.
7. Dictionaries (dict)
Ordered collections of key-value pairs (preserves insertion order as of Python 3.7+).
8. Sets (set)
Unordered collections of unique items.
9. Bytes (bytes)
Immutable sequences of single bytes.
Mutability vs Immutability
This is a key concept in Python.
- Immutable: The value cannot be changed. If you change it, Python creates a new object. (int, float, bool, str, tuple)
- Mutable: The value can be changed in place. (list, dict, set)
Type Checking
Type Conversion (Casting)
AI Mentor
Confused about "Python data types and type conversion"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 5Which is a valid variable name in Python?
Key Takeaways
✅ Variables are labels for data in memory.
✅ Basic types (int, float, str, bool) are immutable.
✅ Use type() to check data types.
What's Next?
In the next lesson, we'll learn about Operators & Expressions to perform calculations and logic.
Keep coding! 🚀