← Back to Hub
Module 2 Week 4 • Declaring and Reassigning Variables

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, and AGE are 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?

Explanation: Variables bind human-readable name labels to memory addresses storing data objects.

Question 2: Which of the following is a VALID variable name?

Explanation: Variable names can only contain letters, numbers, and underscores, and cannot start with a number or contain spaces/hyphens.

Question 3: Which variable name breaks the 'No Initial Digits' rule?

Explanation: 5_total starts with a digit, which is syntactically invalid in Python.

Question 4: Are the variables 'score' and 'Score' the same in Python?

Explanation: Python is case-sensitive, so 'score' and 'Score' are treated as two separate identifiers.

Question 5: What happens to old variable values when no labels point to them?

Explanation: Python's automatic garbage collection cleans up objects that have zero active reference pointers.

Question 6: Which symbol is used to assign a value to a variable?

Explanation: A single equals sign (=) is the assignment operator. Double equals (==) checks for equality.

Question 7: Which of these is a reserved Python keyword that cannot be used as a variable name?

Explanation: The word 'if' is a reserved keyword used for conditional logic scoping.

Question 8: What is the output of: x = 5; y = x; y = 20; print(x)?

Explanation: y initially points to 5. Reassigning y to 20 does not affect x, which still points to 5.

Question 9: Can a Python variable change its data type after declaration?

Explanation: Python is dynamically typed, allowing variables to point to different data types over time.

Question 10: Which variable name follows standard camelCase conventions?

Explanation: camelCase capitalizes the first letter of each word except the very first word.

🖥️ Output Prediction Challenges

Read each code snippet carefully, analyze its execution flow, and predict the exact string result written to the console.

Challenge 1 of 5

Predict the output of the code snippet below. Type the exact output in the console.

x = 10
x = 20
print(x)
Console ready. Type your prediction below...
Challenge 2 of 5

Predict the output of the code snippet below. Type the exact output in the console.

a = 5
b = a
print(b)
Console ready. Type your prediction below...
Challenge 3 of 5

Predict the output of the code snippet below. Type the exact output in the console.

a, b = 1, 2
a = b
print(a)
Console ready. Type your prediction below...
Challenge 4 of 5

Predict the output of the code snippet below. Type the exact output in the console.

value = 100
Value = 200
print(value)
Console ready. Type your prediction below...
Challenge 5 of 5

Predict the output of the code snippet below. Type the exact output in the console.

counter = 1
counter = counter + 2
print(counter)
Console ready. Type your prediction below...

🎮 Interactive Code Playground

Type your own Python code below and click "Run Code" to execute it instantly in your browser!

📝 Python Code Editor
Python WebAssembly runtime is unloaded. Click 'Run Code' to load.
🖥️ Output Console
Console ready.
💬 Teacher Consultation

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

Lesson Complete?

📌 Check the box above to mark this weekend's lesson complete. Your dashboard status ring will update automatically.