Unit 1 · Lesson 3

Variables, Naming & Assignment

Give values clear names, use them, and track how they change.

🏷️ Type First, Name Second

Recognize what kind of value you have, then choose a name that identifies its role.

🏷️ Give Values a Name

A meaningful name makes a value easier to reuse and understand.

92

A Value Alone

The value exists, but its purpose is not clear.

score = 92

A Named Value

The name score tells us what 92 means and lets us use it again.

💾 Create and Use a Variable

Python creates a name when an assignment runs.

player_profile.py
Console Output
Maya 4 True

🔍 Ready Before Use

What happens when Python runs this code?

before_assignment.py

⬅️ Assignment Logic

Evaluate the right side, then bind the result to the name on the left.

⬅️

total = 12 + 3

Evaluate the right side, then bind the result to the name on the left.

Data flows from right to left.

= assigns a value; it is not a mathematical equal sign.

Evaluation

The right side is evaluated first.

Binding

The result is connected to the left-side name.

Direction

Data flows from right to left.

🔍 Assignment Check

What value is bound to total?

assignment_check.py

🧮 Variables in Expressions

When Python sees a variable name, it uses that name's current value.

score_total.py
Console Output
15

🔍 Trace the Current Value (1/3)

Read the code from top to bottom. What is printed?

value_trace.py

🔍 Trace the Current Value (2/3)

Read the code from top to bottom. What is printed?

value_trace.py

🔍 Trace the Current Value (3/3)

Read the code from top to bottom. What is printed?

value_trace.py

🔄 Update a Variable

Running another assignment changes the value currently connected to that name.

lives.py
Console Output
3 2

📌 Earlier Results Stay the Same

Reassigning score does not recalculate total automatically.

score_update.py
Console Output
13 20

🔍 Reassignment Trace (1/2)

Track each assignment. What is printed?

reassignment_trace.py

🔍 Reassignment Trace (2/2)

Track each assignment. What is printed?

reassignment_trace.py

🏷️ Naming Rules

A variable name must be legal Python and should be clear to people.

Rules

  • Use only: atoz,AtoZ,0to9,_
  • Cannot start with a digit
  • Case-sensitive: score ≠ Score
  • Cannot use reserved keywords such as class

Conventions

Use snake_case: student_score, is_ready

Use meaningful names: score > s

Use consistent lowercase style: total > Total

Avoid hiding built-in names: input, str

🔍 Naming Check

Click each name to reveal whether its syntax is valid Python.

Green means syntactically valid, not always recommended.Names such as Total and input should still be improved.

📝 Homework

Complete it on time and submit it before the deadline.

🔖 Summary

Key takeaways from this lesson.

Name

A variable gives a reusable name to a current value.

Assign

Evaluate the right side, then bind the result to the left-side name.

Update

Reassignment changes the name's current value, not earlier results.

Name Clearly

Use legal, meaningful snake_case names with consistent capitalization.

🏷️ Task 1: Classify the Names

Separate syntax rules from good naming choices.

Your Goal

1. Classify each name as valid, invalid, or valid but avoid.

2. Rewrite every invalid or unsuitable name as clear snake_case.

Variable Names
player_score
3lives
class
student-name
Total
is_ready
input

🔍 Task 2: Trace the Values

Write the three outputs before running the program.

Your Goal

1. Predict the value printed by each line.

2. Run the program and correct any prediction that differs.

Example Code

👤 Task 3: Build a Profile

Store profile data in clearly named variables.

Your Goal

1. Create variables for a student name, age, favorite subject, and ready status.

2. Use meaningful snake_case names.

3. Print all four variables and run the program.

🎮 Task 4: Update a Scoreboard

Show the score before and after an update.

Your Goal

1. Print the original total.

2. Reassign score to 20.

3. Calculate total again and print the updated total.

4. Confirm that the two displayed totals are different.

Example Code

🛠️ Task 5: Debug the Variables

Repair the naming error without changing the intended values.

Your Goal

1. Predict the first output and identify why the second print fails.

2. Repair the smallest naming mistake.

3. Run the program and confirm that it prints 15 and 20.

4. Explain why total_score stays 15.

Example Code
01 / 24