What You Will Learn
Beginner
- How to create and use variables
- Python dynamic typing
- Variable naming rules and conventions
A variable is a named container for data. You assign a value with =:
Terminalpython
name = "Anita" # String
age = 28 # Integer
height = 5.6 # Float
is_student = True # Boolean
print(name) # Anita
print(age) # 28Dynamic Typing
Python is dynamically typed - you do not declare the type. Python figures it out automatically. You can even change the type:
Terminalpython
x = 10 # x is an int
print(type(x)) #
x = "hello" # now x is a string
print(type(x)) # Multiple Assignment
Terminalpython
# Assign same value to multiple variables
a = b = c = 0
# Assign different values in one line
x, y, z = 1, 2, 3
print(x, y, z) # 1 2 3
# Swap variables (no temp needed!)
a, b = b, aNaming Rules
| Rule | Example |
|---|---|
| Letters, numbers, underscores | my_var, age2, _count |
| Cannot start with a number | 2name is INVALID |
| Case-sensitive | name != Name != NAME |
| Cannot use keywords | class, if, for, def are reserved |
Naming convention
Use snake_case for variables: first_name, total_count, is_active. Use ALL_CAPS for constants: MAX_SIZE, PI.Practical Exercise
Create variables: name, age, city
Print each variable
Try: x, y = 10, 20 then swap them
Check types: print(type(name))
Key Takeaways
- Variables store data. Assign with =.
- Python is dynamically typed - no type declaration needed.
- Multiple assignment: a, b, c = 1, 2, 3.
- Naming: snake_case for variables, ALL_CAPS for constants.
- Case-sensitive: name != Name.
Comments
Comments
Post a Comment