1/205
Variables & Data Types · Page 1 of 5

What is a Variable? (Beginner Explanation)

18 min Beginner

Variables & Data Types — The Foundation

What is a Variable?

In computer science, a variable is a named storage location in memory that holds a value. The name acts as a symbolic reference — when your program runs, Python stores the actual data in memory and the variable name points to it. Unlike statically typed languages like C or Java, Python variables are dynamically typed: you don't declare a type upfront, Python infers it automatically at runtime.

Think of it like a labeled box in your computer's memory. You put a value inside and label the box so you can access it later.

┌──────────────────┐
│  name = "Alice"  │  ← Variable "name" holds the text "Alice"
└──────────────────┘

┌──────────────────┐
│  age = 28        │  ← Variable "age" holds the number 28
└──────────────────┘

┌──────────────────┐
│  score = 95.5    │  ← Variable "score" holds 95.5
└──────────────────┘

When you write name = "Alice", you're telling Python:

  • Create a box called name
  • Put the value "Alice" inside
  • Remember it so I can use it later

Variables in Action

student = "Bob"
marks = 87
gpa = 3.5

print(student)      # Bob
print(marks)        # 87
print(student, "scored", marks)  # Bob scored 87

Variable Naming Rules

Valid names:

age = 25
student_name = "Alice"
_private = 42
total2 = 100

Invalid names:

2start = 10        # ✗ Can't start with number
my-age = 25        # ✗ Can't use hyphens
my age = 25        # ✗ Can't have spaces

Naming conventions (style guide):

  • Use lowercase with underscores: student_name
  • Not camelCase: studentName (that's JavaScript style)
  • Not UPPER_CASE: STUDENT_NAME (reserved for constants)

Gotcha: Python is case-sensitive. name, Name, and NAME are three different variables!

Overview
main.py
Loading...
OUTPUT
Click "Run Code" to execute…