1/20
Variables & Data Types Β· Page 1 of 5

What is a Variable? (Beginner Explanation)

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…