Python Variables Explained: Basics, Naming Rules & Practical Examples
Learn Python variables with this beginner-friendly guide. Understand variable basics, naming rules (including valid vs invalid names), reserved keywords, and practice with hands-on coding tasks.
π Table of Contents
1. What Are Variables?
- A variable is a named storage location in a computer's memory that is used to hold data or values. It allows programmers to store and manipulate data within a program.
Purpose: Variables provide a way to store and manage data that can be used and manipulated throughout a program. They make programs more flexible and allow for dynamic data storage.
Assignment statement: in Python is used to assign a value to a variable. Its primary purpose is to store and manage data within a program.
Imagine variables as labeled boxes:
- You have boxes for storing different things (numbers, words, etc.).
- Each box has a name (label) to identify what's inside.
- You can put things in, take them out, and change what's inside.
Example:
name = "Alice" # Stores text
age = 25 # Stores a number
is_student = True # Stores True/False Assignment Operator (=)
Use = to assign values. No need to declare types!
Key Rules:
- Reassignment: Change a variableβs value anytime.
x = 10 x = "hello" # Now x is a string! - Multiple Assignment:
a, b = 1, 2 # a=1, b=2 x = y = z = 0 # All set to 0 - Swapping:
x, y = 10, 20 x, y = y, x # Swap! (Now x=20, y=10)
Common Mistakes:
- Using spaces:
user name = "Bob"β - Starting with numbers:
2nd_place = "Silver"β
2. Variable Naming Rules
In Python, valid variable names must adhere to the following rules:
- Begin with a letter or an underscore: The first character of a variable name must be a letter (a-z, A-Z) or an underscore (_).
- Followed by letters, digits, or underscores: After the first character, the variable name can contain letters, digits (0-9), or underscores.
- Case-sensitive: Variable names are case-sensitive. For example, myVariable and myvariable would be considered different variables.
- No reserved keywords: Variable names cannot be Python reserved keywords (e.g., if, for, while, class, etc.).
Valid vs. Invalid Variable Names in Python
β Valid Variable Names
- Can contain:
- Letters (
a-z,A-Z) - Digits (
0-9) but not as the first character - Underscores (
_)
- Letters (
- Are case-sensitive (
age,Age, andAGEare different). - Should use
snake_case(recommended Python style).
Examples:
name = "Alice"
user_age = 25
_total = 100
item2 = "book" β Invalid Variable Names
- Start with a digit.
- Contain spaces or special symbols (
!,@,#, etc.). - Match Python's reserved keywords (see below).
Examples:
2name = "Bob" # Error: Starts with digit
first-name = "John" # Error: Hyphen not allowed
user age = 30 # Error: Contains space Reserved Keywords
Python has 35 reserved keywords (e.g., if, for, while).
Check All Keywords:
import keyword
print(keyword.kwlist) β οΈ Never use these as variable names!
Valid vs. Invalid Names
| Valid β | Invalid β |
|---|---|
user_name |
user-name (hyphen) |
_total |
2nd_place (starts with digit) |
price2 |
class (reserved keyword) |
π‘ Tip:
If you accidentally use a keyword, Python will raise a SyntaxError:
class = "Biology" # Error: 'class' is a keyword π Interactive Task
Which of these are valid?
1. user_name
2. 3rd_place
3. return
4. _price
5. my-var π Best Practices
β Use descriptive names (e.g., student_count vs. n).
β Stick to snake_case (e.g., total_score).
β Avoid single letters (except in loops like for i in range(5)).
3. Practical Tasks
Task 1: Rectangle Calculator
Given:
length = 10
width = 5 Your Task: Calculate and print:
- Area (
length * width) - Perimeter (
2 * (length + width))
area = length * width
perimeter = 2 * (length + width)
print("Area:", area)
print("Perimeter:", perimeter) Task 2: Exploring type() and id()
Code:
x = 10
print(type(x)) # Output: <class 'int'>
print(id(x)) # Output: Memory address Questions:
- What happens if you set
x = "hello"and checktype(x)? - Assign
y = 10. Doesid(y)matchid(x)? Why?
π‘ Hint: Python reuses memory for small integers!
π Additional Resources: