Variables & Assignments
Understand how variables act as labels pointing to memory addresses, dynamic type assignment, and Python variable naming rules.
1. What is a Variable?
A variable is a name that points to a value stored in your computer's memory (RAM). Think of RAM as a grid of storage boxes. When you assign x = 10, Python stores 10 in a box and labels the box "x".
2. Variable Reassignment
Python is dynamically typed, meaning a variable can change its value (or even its data type) at any time during execution:
age = 10 # points to integer 10
age = "Ten" # now points to string "Ten"
3. Variable Naming Rules
To write valid Python variables, you must follow these rules:
- Names must start with a **letter** or an **underscore** (
_). They cannot start with numbers. - Names can only contain alphanumeric characters and underscores (
A-z,0-9, and_). No spaces or symbols like$,@,-. - Variable names are **case-sensitive** (e.g.,
age,Age, andAGEare three different variables). - You cannot use Python's **reserved keywords** (like
import,print,if,def) as variable names.
💻 Python Code Examples
Example 1: Declaring Variables
name = "Peter"
age = 22
print(name)
print(age)
Description: Binds values 'Peter' and 22 to descriptive variable names in memory.
Example 2: Reassigning Variable Values
score = 100
print(score)
score = 150 # Points to a new value
print(score)
Description: Updates the variable label to point to a new value object in RAM.
Example 3: Variable Case Sensitivity
val = 10
Val = 20
print(val)
print(Val)
Description: Capitalization changes the variable name, creating two distinct slots in memory.
Example 4: Multi-Variable Assignment
x, y, z = 1, 2, 3
print(x, y, z)
Description: Assigns multiple variables in a single line using comma separation.
Example 5: Swapping Variables
a = 5
b = 10
temp = a
a = b
b = temp
print(a, b)
Description: Swaps variable values using a temporary helper variable.
Example 6: Dynamic Typing Shifts
item = 50
print(item)
item = "Laptop"
print(item)
Description: Changes the variable from an integer to a string, demonstrating dynamic typing.
🧠 Active Retrieval Quizzes
Answer the following 10 multiple-choice questions to test your storage strength.
Question 1: What is a variable in Python?
Question 2: Which of the following is a VALID variable name?
Question 3: Which variable name breaks the 'No Initial Digits' rule?
Question 4: Are the variables 'score' and 'Score' the same in Python?
Question 5: What happens to old variable values when no labels point to them?
Question 6: Which symbol is used to assign a value to a variable?
Question 7: Which of these is a reserved Python keyword that cannot be used as a variable name?
Question 8: What is the output of: x = 5; y = x; y = 20; print(x)?
Question 9: Can a Python variable change its data type after declaration?
Question 10: Which variable name follows standard camelCase conventions?
🖥️ Output Prediction Challenges
Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.
Predict the output of the code snippet below. Type the exact output in the console.
x = 10
x = 20
print(x)
Predict the output of the code snippet below. Type the exact output in the console.
a = 5
b = a
print(b)
Predict the output of the code snippet below. Type the exact output in the console.
a, b = 1, 2
a = b
print(a)
Predict the output of the code snippet below. Type the exact output in the console.
value = 100
Value = 200
print(value)
Predict the output of the code snippet below. Type the exact output in the console.
counter = 1
counter = counter + 2
print(counter)
🎮 Interactive Code Playground
Type your own Python code below and click "Run Code" to execute it instantly in your browser!
Console ready.
Stuck on any concept, or want to explore an edge case? Don't hesitate to ask follow-up questions to your instructor! Active questioning helps build storage strength.
📋 Progress Status
📌 Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.