131.1 Declaring and using variables

Learn how to define variables, store values in memory, and follow best practices for naming and usage.

What is a variable?

A variable is a named container for data. It allows a program to store, reference, and update information in memory. By using variables, you can make your code flexible and reusable.

Declaring a variable

Different programming languages have different rules for declaring variables. In pseudocode, we often declare a variable with an explicit SET statement. In Python, variables are created when a value is first assigned.

SET name TO "Myles"
SET age TO 16
SET is_student TO TRUE

Naming rules and conventions

Rules (apply to most languages, including Python):

  • Variable names must begin with a letter or underscore.

  • They cannot contain spaces or punctuation (except _).

  • Reserved keywords (like IF, FOR, WHILE) cannot be used as names.

Good conventions

  • Use lowercase and underscores for readability: user_score, first_name.

  • Choose names that describe what the variable stores: temperature instead of x.

Changing values

Variables can be updated during a program's execution. When you assign a new value, it replaces the old one.

SET score TO 10
SET score TO score + 5

Using multiple variables

You can use multiple variables to break down a problem and store intermediate values.

SET length TO 20  
SET width TO 5  
SET area TO length * width

Memory and data types

Even though Python doesn't require you to declare a variable’s type, the data type still matters. When you assign a value, Python decides its type behind the scenes (string, integer, Boolean, etc.).

You don’t always need to declare types in pseudocode, either, but you should be clear about what data each variable holds.

Key concepts

  • A variable stores a value that can change.

  • Variable names should be descriptive and follow naming conventions.

  • Values can be read, written, and updated using variables.

  • Each variable holds a value of a particular type.

Last updated

Was this helpful?