Page1/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, andNAMEare three different variables!
main.py
Loading...
OUTPUT
βΆClick "Run Code" to executeβ¦