Chapters List
- A Mental Model for Programming
- What Happens When a Program Runs
- Programming Languages
- Variables and Data
- Decisions: Conditional Logic
- Repetition: Loops
- Functions: Building Reusable Code
- Data Structures: Organizing Information
- Debugging: Fixing Programs
- How Programs Become Real Software
- Different Types of Programming
- Modern Programming and Software Development
programming
Variables and Data
Learn how programs store and manipulate values using variables and basic data types.
In the previous chapter we discussed programming languages.
Now we begin the core mechanics of writing programs.
To do anything useful, a program needs to work with data.
Why Data Matters
Programs answer questions and perform actions using values.
Examples:
- temperature in a weather app
- account balance in a banking app
- username in a login form
- score in a game
Without data, program logic has nothing to operate on.
What Is a Variable?
A variable is a named storage location for a value.
You can think of it as a labeled box in memory.
graph LR
A[Variable Name: age] --> B[Stored Value: 21]
If the value changes later, the variable can be updated.
Basic Data Types
Programs usually categorize values by type.
Common beginner types:
- number:
42,3.14 - text/string:
"hello" - boolean:
trueorfalse
Types matter because they determine what operations are valid.
For example, adding two numbers makes sense. Adding a number directly to plain text may require conversion.
Simple Examples
age = 21
name = "Ahnaf"
is_student = True
Here:
agestores a numbernamestores textis_studentstores a boolean
Programs combine these values to make decisions and produce output.
Variables Change Over Time
One important idea is state.
Program state is the current set of variable values.
Example:
count = 0
count = count + 1
count = count + 1
The same variable now holds different values as execution progresses.
This changing state is central to most software behavior.
Input, Processing, Output
Variables are often used in a simple flow:
- Read input.
- Store in variables.
- Process values.
- Produce output.
This pattern appears everywhere from small scripts to large systems.
Key Ideas to Remember
- Programs need data to do meaningful work.
- Variables are named storage for values.
- Data types classify values and valid operations.
- Program state changes as variables are updated.
- Tracing variable values is a powerful way to understand code.
What Comes Next
Once values are stored, programs must decide what to do with them.
Next we learn conditional logic:
how code makes decisions with if statements and comparisons.