Live
Auto strategy optimizer — AI improves your edge while you sleep
guidebeginner12 min

Variables and Values

Learn how to store information in variables—the building blocks of every program.

Last updated: Jan 28, 2026

Variables are how programs remember things. A variable is a name that refers to a value stored in the computer's memory.

Creating Variables

Use the equals sign = to assign a value to a variable:

python
age = 25
name = "Alice"
pi = 3.14159

print(name)
print(age)
print(pi)

Output:

text
Alice
25
3.14159

Think of variables as labeled boxes. The name is the label, and the value is what's inside the box.

Variables Can Change

That's why they're called "variables"—their values can vary:

python
score = 0
print("Starting score:", score)

score = 10
print("After bonus:", score)

score = score + 5  # Add 5 to current score
print("Final score:", score)

Output:

text
Starting score: 0
After bonus: 10
Final score: 15

The line score = score + 5 might look strange at first. It means "take the current value of score, add 5, and store the result back in score."

Naming Rules

Variable names must follow these rules:

  • Start with a letter or underscore (_)
  • Can contain letters, numbers, and underscores
  • Cannot contain spaces or special characters
  • Are case-sensitive (age, Age, and AGE are different variables)
  • Cannot be Python keywords (like print, if, for)
python
# Valid names
user_name = "Bob"
age2 = 30
_private = "hidden"

# Invalid names (these will cause errors)
# 2fast = "nope"      # Can't start with number
# user-name = "nope"  # Can't use hyphens
# my name = "nope"    # Can't have spaces

Naming Conventions

While Python allows many names, follow these conventions for readable code:

  • Use lowercase with underscores: user_name, total_price
  • Be descriptive: price is better than p
  • Avoid single letters except for counters: i, j, x
  • Don't be too verbose: user_email not user_email_address_string

Using Variables in Calculations

python
# Calculate the area of a rectangle
width = 10
height = 5
area = width * height

print("Width:", width)
print("Height:", height)
print("Area:", area)

Output:

text
Width: 10
Height: 5
Area: 50

Practice

  1. Create variables for your name, age, and city
  2. Calculate your age in days (age * 365)
  3. Create a variable for price, another for quantity, calculate total
  4. Swap the values of two variables (hint: you'll need a third variable)

Tags

variablesassignmentnamingbeginner
Related documentation