Technology

Variables and Data Types: Programming Building Blocks

Variables are how programs remember information. Learn about variables and data types—the fundamental building blocks of every program.

Superlore TeamJanuary 19, 20264 min read

Variables and Data Types: Programming Building Blocks

Every program needs to store and manipulate information—a user's name, a score, whether something is true or false. Variables make this possible. They're the fundamental building blocks of programming.

Think of variables as labeled containers that hold data. You can put information in, check what's there, and change it as your program runs.

What Is a Variable?

A variable has three parts:

Name: A label you choose to identify it
Examples: userName, score, isLoggedIn

Value: The actual data it contains
Examples: "Alice", 42, true

Type: What kind of data it holds
Examples: text, number, true/false

Creating a variable (in Python):
\\\`python
name = "Alice"
age = 30
is_student = True
\\\`

Now name holds "Alice", age holds 30, and is_student holds True.

Why "Variable"?

They're called variables because their values can vary—change—as the program runs:

\\\`python
score = 0 # Start at zero
score = score + 10 # Now it's 10
score = score + 5 # Now it's 15
\\\`

This is how games track points, apps remember preferences, and programs handle dynamic data.

The Main Data Types

Different types of data behave differently. Here are the fundamental types:

1. Strings (Text)

Strings hold text—letters, words, sentences. They're surrounded by quotes.

\\\`python
greeting = "Hello, World!"
first_name = "Alice"
sentence = "The quick brown fox"
\\\`

Strings can be combined (concatenated):
\\\`python
full_name = "Alice" + " " + "Smith" # "Alice Smith"
\\\`

2. Numbers (Integers and Floats)

Integers are whole numbers:
\\\`python
age = 25
count = -7
year = 2024
\\\`

Floats (floating-point numbers) have decimals:
\\\`python
price = 19.99
temperature = 98.6
pi = 3.14159
\\\`

Numbers support math operations:
\\\`python
total = 10 + 5 # 15 (addition)
difference = 10 - 5 # 5 (subtraction)
product = 10 * 5 # 50 (multiplication)
quotient = 10 / 5 # 2.0 (division)
\\\`

3. Booleans (True/False)

Booleans have only two possible values: True or False.

\\\`python
is_logged_in = True
has_permission = False
game_over = False
\\\`

Booleans are essential for decision-making in code:
\\\`python
if is_logged_in:
show_dashboard()
else:
show_login_page()
\\\`

4. Lists (Collections)

Lists hold multiple values in order:
\\\`python
numbers = [1, 2, 3, 4, 5]
names = ["Alice", "Bob", "Charlie"]
mixed = [1, "hello", True, 3.14]
\\\`

Access items by position (starting from 0):
\\\`python
first_name = names[0] # "Alice"
second_name = names[1] # "Bob"
\\\`

Naming Variables

Good variable names make code readable. Follow these guidelines:

  • Bad: x = 25
  • Good: user_age = 25
  • snake_case: user_name, total_score (Python convention)
  • camelCase: userName, totalScore (JavaScript convention)

Avoid reserved words: Don't use words the language reserves (like if, for, class).

Be concise but clear: user_name is better than the_name_of_the_user.

Type Conversion

Sometimes you need to convert between types:

\\\`python

String to number


age_string = "25"
age_number = int(age_string) # 25 (as integer)

Number to string

price = 19.99 price_text = str(price) # "19.99" (as string)

String to float

rating = float("4.5") # 4.5 (as float) \\\`

This matters when combining different types:
\\\`python
age = 30
message = "You are " + str(age) + " years old"
\\\`

Constants

Some values shouldn't change. These are called constants. By convention, they're written in ALL_CAPS:

\\\`python
MAX_USERS = 100
PI = 3.14159
API_URL = "https://api.example.com"
\\\`

The all-caps signals to other programmers: don't change this value.

Scope: Where Variables Live

Variables exist in different "scopes"—areas of your program where they're accessible:

\\\`python
global_variable = "I'm accessible everywhere"

def my_function():
local_variable = "I only exist inside this function"
print(global_variable) # This works
print(local_variable) # This works

print(global_variable) # This works
print(local_variable) # Error! Not accessible here
\\\`

Understanding scope prevents confusing bugs and keeps code organized.

Practical Example

Here's how variables work together in a real scenario:

\\\`python

Shopping cart calculation


item_name = "Widget"
item_price = 29.99
quantity = 3
tax_rate = 0.08

subtotal = item_price * quantity
tax = subtotal * tax_rate
total = subtotal + tax

print(f"Item: {item_name}")
print(f"Subtotal: ${subtotal:.2f}")
print(f"Tax: ${tax:.2f}")
print(f"Total: ${total:.2f}")
\\\`

Variables store each piece of information, allowing calculations and formatted output.

Next Steps

  • Loops and conditionals: Making decisions and repeating actions
  • Functions: Organizing code into reusable pieces
  • Data structures: More complex ways to organize data

Every program, from simple scripts to complex applications, builds on these fundamentals.

Related Reading

Listen to the Full Course

Master programming fundamentals in Learn to Code: Programming Fundamentals.

Prefer Audio Learning?

Learn to Code: Programming Fundamentals for Beginners

Master the building blocks of programming — variables, loops, functions, and computational thinking

Listen Now
Variables and Data Types: Programming Building Blocks | Superlore - Superlore