No history yet

Introduction to Variables

What Are Variables?

Think of a variable as a labeled box where you can store information. In programming, you give this box a name, and you can put data inside it. Later, you can look inside the box, change what's inside, or use the contents to do something interesting.

Variables are a fundamental concept in programming and allow us to store values that can be used later in our code.

Without variables, programs would be very rigid. Imagine a game that always greets you with "Hello, Player!" It works, but it’s impersonal. With variables, the game can ask for your name, store it in a variable called playerName, and then greet you with "Hello, Alex!" or whatever your name is. The greeting is now dynamic, changing based on the data stored in the variable.

This is the core purpose of variables: to hold onto information that can change or be used multiple times. They allow us to write flexible, powerful code that can adapt to different inputs and situations.

Lesson image

Variables in Action

The idea of a named container for data is universal in programming, though the exact way you write it can look a little different depending on the language. Don't worry about the specific syntax here. Just notice how each example creates a named spot to hold a piece of information.

# Python
# Here, we store the number 28 in a variable called 'age'.
age = 28

# We store the text "Maria" in a variable called 'name'.
name = "Maria"

Here is an example in a different language.

// JavaScript
// Storing the price of an item in a variable.
let itemPrice = 19.99;

// Storing a user's online status.
let isLoggedIn = true;

And one more to show how common this concept is.

// C++
// Storing a single character, like a grade.
char grade = 'A';

// Storing the number of points a player has.
int score = 1500;

In all these cases, we're doing the same thing: giving a name to a piece of data so we can refer to it later. This simple idea is one of the most important building blocks in all of programming.