Lecture 2 B Handout Variable
Lecture 2 B Handout Variable
A variable in programming can be thought of as a "box" that stores data. Just like you might use
different boxes to store different types of items (e.g., a box for toys, another for books), variables in
programming are used to store various kinds of information, such as numbers, text, or more
complex data.
What is a Variable?
A variable is a symbolic name given to a memory location where data can be stored. This data can
be modified or retrieved during the program's execution. Variables help programmers organize,
manage, and manipulate data efficiently.
For example:
name = "Alice"
age = 25
Here:
• name is a variable that holds the value "Alice" (a piece of text).
• age is another variable that stores the number 25.
1
• Complex data types (like lists or dictionaries)
Here:
• We use the price and quantity variables to calculate total_cost.
• If price or quantity changes, the program automatically adjusts the result.