Skip to main content

Command Palette

Search for a command to run...

Types of Variables: A Quick Guide

Published
3 min read
Types of Variables: A Quick Guide
M

Hello, I’m Badhon. I tend to forget things easily, which is why I started this blog.

When learning programming or working with tools like Linux, APIs, or automation platforms, you often hear about different types of variables. Terms like local variable, global variable, or environment variable can feel confusing at first. This article explains these variable types in a simple and practical way so that beginners can understand them easily.

What Is a Variable?

A variable is a named storage location used to hold data. This data can change while a program is running. Variables help us store values such as numbers, text, or collections of data and reuse them when needed.

Different variable types mainly differ in two things:

  1. Scope, meaning where the variable can be accessed

  2. Lifetime, meaning how long the variable exists

Local Variables

Local variables exist only inside a specific function or block of code. They cannot be accessed from outside that function. They are useful for temporary calculations and help keep code clean and safe.

Example in Python:

def calculate_sum():
    total = 10 + 20 #local variable
    print(total)

Here, the variable total is a local variable. It only exists inside the function calculate_sum. If you try to use it outside the function, it will cause an error.

Local variables are preferred whenever possible because they reduce unexpected side effects.

Global Variables

Global variables are declared outside of functions and can be accessed from anywhere in the program.

Example in Python:

count = 5

def show_count():
    print(count)

The variable count is global and can be accessed inside the function.

While global variables can be useful, using too many of them can make programs harder to understand and debug. Changes made in one place can affect other parts of the program unexpectedly.

Environment Variables

Environment variables are managed by the operating system, not directly by the program. They store configuration values such as system paths, credentials, or settings.

They are widely used in Linux, servers, and DevOps environments.

Example in Linux:

echo $PATH

Setting an environment variable:

export APP_MODE=production

Common use cases include storing database passwords, API keys, and application configuration values. Environment variables are considered secure and portable because they keep sensitive data outside the source code.

Collection Variables

Collection variables store multiple values together. Instead of holding a single value, they hold a group of related values.

Examples of collections include lists, arrays, sets, and dictionaries.

Example in Python:

students = ["Sakib", "Rahim", "Karim"]

Here, students is a collection variable that holds multiple names.

In tools like Postman, collection variables refer to variables shared across all requests inside a collection. They help avoid repeating the same values again and again.

Data Variables

Data variables represent actual data used by an application. They often come from user input, files, APIs, or databases.

Example:

user_age = 21
salary = 50000

In data science and analytics, columns in a dataset are often referred to as data variables. These variables hold meaningful information that is processed, analyzed, or displayed.

Key Differences at a Glance

Local variables work only inside a function and exist temporarily

Global variables work across the entire program

Environment variables exist outside the program at the operating system level

Collection variables store multiple values together

Data variables hold actual information used by the application