What Is a Variable in Python?
A variable is a named location in memory used to store a value your program can use and change. Python doesn't require you to declare a variable's type upfront — it figures it out automatically based on the value assigned. This is called dynamic typing.
name = "Aman"
age = 21
price = 499.99
is_active = True
Rules for Naming Variables
- Must start with a letter or an underscore (
_) — never a number - Can contain letters, numbers, and underscores only
- Case-sensitive —
ageandAgeare different variables - Cannot be a reserved keyword like
class,for, orimport
# Valid
student_name = "Riya"
_marks = 90
# Invalid
2ndYear = "error" # cannot start with a number
class = "error" # 'class' is a reserved keyword
Python's Core Data Types
Every value in Python belongs to a data type. Here are the ones you'll use constantly:
1. Numeric Types
x = 10 # int
y = 10.5 # float
z = 2 + 3j # complex
2. String (str)
message = "Learning Python at Coding Now"
print(message.upper()) # LEARNING PYTHON AT CODING NOW
3. Boolean (bool)
is_enrolled = True
has_paid = False
4. Sequence Types — List & Tuple
skills = ["Python", "SQL", "Pandas"] # list — mutable
coordinates = (28.70, 77.13) # tuple — immutable
5. Mapping Type — Dictionary
student = {"name": "Aman", "course": "Data Science"}
6. Set
unique_ids = {101, 102, 103}
List, Tuple, Set, and Dictionary each get their own dedicated note on this hub — this page focuses on the fundamentals.
Checking a Variable's Type
print(type(10)) # <class 'int'>
print(type(10.5)) # <class 'float'>
print(type("hello")) # <class 'str'>
Type Conversion (Type Casting)
a = "25"
b = int(a) # converts string to integer → 25
c = float(b) # converts integer to float → 25.0
d = str(c) # converts float to string → "25.0"
Key Takeaways
- Python variables don't need explicit type declarations — the type is decided at runtime
- Core types include int, float, complex, str, bool, list, tuple, set, and dict
- Use
type()to inspect a value and functions likeint(),str(),float()to convert between types