Variables and Assignment

Week 1 — Python Basics Revision

Introduction

Variables are the building blocks of every program. Think of them as labeled boxes where you store information. Before you can build anything in Python — whether it is a simple calculator or a complex banking system — you need to understand how to create and use variables. This lesson covers just that.

What is a Variable?

A variable is a name that refers to a value stored in memory. Think of it like a labeled jar in your kitchen — the label is the variable name, and the contents inside the jar are the value.

When you write name = "Alice", the name name now points to the text "Alice". Python figures out the type automatically — this is called dynamic typing.

# Creating variables
name = "Alice"
age = 22
height = 5.6
is_student = True

print(name)
print(age)
Output
Alice
22

Naming Rules

  • Must start with a letter or underscore
  • Can contain letters, numbers, and underscores
  • Case-sensitive: Name and name are different
  • Cannot use Python reserved words: if, for, while, class, etc.
  • Convention: use snake_case — student_name, not studentName
GoodBadWhy
student_namesnNot descriptive
total_pricetpUnclear
is_activexMeaningless

Reassigning Variables

Variables can change their value at any time. They can even change type, because Python is dynamically typed.

score = 100
print(score)  # 100

score = 200
print(score)  # 200

score = "High"  # Now it's a string!
print(score)  # High

Multiple Assignment

Python lets you assign values to multiple variables in a single line, which keeps your code clean and concise.

# Assign same value to multiple variables
a = b = c = 0
print(a, b, c)  # 0 0 0

# Assign different values in one line
x, y, z = 10, 20, 30
print(x, y, z)  # 10 20 30

# Swap two variables
x, y = y, x
print(x, y)  # 20 10

How This Connects to OOP

In OOP, variables stored inside a class are called attributes. Instead of standalone variables like name = "Alice", you will write self.name = "Alice" inside a class. The concept is identical — you are just organizing variables inside objects.

Best Practices

  • Use descriptive names that explain the purpose of the variable
  • Follow the snake_case convention for all variable names
  • Use UPPERCASE for constants: MAX_SIZE = 100
  • Keep names short but meaningful

Common Mistakes

  • Starting variable names with numbers: 1name = "bad"
  • Using reserved words: class = "Math" will cause an error
  • Misspelling variable names — Python will not catch this; it simply creates a new variable
  • Using inconsistent casing across your code

Practice Exercises

  1. Create variables for a person's first name, last name, age, and city. Print each one.
  2. Swap two variables without using a third variable.
  3. Create a variable greeting, assign it "Hello", then reassign it to "Hi". Print both times.

Summary

  • A variable is a name that refers to a value in memory.
  • Python is dynamically typed — you do not need to declare the type.
  • Follow snake_case naming conventions and use descriptive names.
  • Variables can be reassigned freely, even to different types.

Next up: we will explore the different data types Python offers.