Zamil's CSE Directory

programming

Variables and Data

Learn how programs store and manipulate values using variables and basic data types.

#programming#variables#data-types#basics
programming, variables, data-types, basics guides

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: true or false

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:

  • age stores a number
  • name stores text
  • is_student stores 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:

  1. Read input.
  2. Store in variables.
  3. Process values.
  4. 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.