DEV Community

Programming Entry Level: learn variables

Understanding Variables for Beginners

Have you ever wondered how computers remember things? How do they keep track of your score in a game, or the name you enter on a website? The answer lies in variables! Understanding variables is one of the very first steps to becoming a programmer, and it’s something you’ll use every single day. It’s also a common topic in entry-level programming interviews, so getting a solid grasp now will really help you down the line.

Understanding Variables

Imagine you have a bunch of labeled boxes. Each box can hold one thing – a number, a word, or even a list of things. In programming, these boxes are called variables.

A variable is a named storage location in your computer's memory. You use variables to store data that your program needs to use. The "name" of the variable is how you refer to that box, and the "data" is what's inside the box.

Think of it like this: you have a box labeled "age" and you put the number 25 inside. Now, whenever you need to know someone's age, you just look at the "age" box!

Here's a simple way to visualize it:

graph LR
    A[Variable Name: age] --> B(Value: 25)
    C[Variable Name: name] --> D(Value: "Alice")
Enter fullscreen mode Exit fullscreen mode

In this diagram, age and name are the variable names, and 25 and "Alice" are the values they hold. The type of data a variable can hold (number, text, etc.) is often determined by the programming language you're using.

Basic Code Example

Let's look at how this works in a couple of popular languages:

JavaScript:

// Declare a variable named 'message' and assign it the value "Hello, world!"
let message = "Hello, world!";

// Declare a variable named 'number' and assign it the value 10
let number = 10;

// Print the value of the 'message' variable to the console
console.log(message);

// Print the value of the 'number' variable to the console
console.log(number);
Enter fullscreen mode Exit fullscreen mode

Here, let is used to declare a variable. Declaration means you're telling the computer, "Hey, I'm going to need a box with this name." Then, the = sign is used to assign a value to that variable – putting something into the box. console.log() is a way to display the value of the variable.

Python:

# Declare a variable named 'message' and assign it the value "Hello, world!"

message = "Hello, world!"

# Declare a variable named 'number' and assign it the value 10

number = 10

# Print the value of the 'message' variable to the console

print(message)

# Print the value of the 'number' variable to the console

print(number)
Enter fullscreen mode Exit fullscreen mode

Python is a bit simpler. You just write the variable name, then the = sign, then the value. Python automatically figures out the type of data you're storing. print() does the same job as console.log() in JavaScript.

Common Mistakes or Misunderstandings

Let's look at some common pitfalls beginners encounter:

1. Using a variable before it's declared:

❌ Incorrect code:

print(my_variable)
Enter fullscreen mode Exit fullscreen mode

✅ Corrected code:

my_variable = 10
print(my_variable)
Enter fullscreen mode Exit fullscreen mode

Explanation: You need to declare and assign a value to a variable before you try to use it. Otherwise, the computer won't know what you're talking about!

2. Using invalid variable names:

❌ Incorrect code:

let 1stNumber = 5;
Enter fullscreen mode Exit fullscreen mode

✅ Corrected code:

let firstNumber = 5;
Enter fullscreen mode Exit fullscreen mode

Explanation: Variable names usually have rules. They often can't start with a number and can't contain spaces or special characters. Stick to letters, numbers, and underscores.

3. Confusing assignment (=) with comparison (== or ===):

❌ Incorrect code:

let x = 5;
if (x = 10) {
  console.log("x is 10");
}
Enter fullscreen mode Exit fullscreen mode

✅ Corrected code:

let x = 5;
if (x == 10) {
  console.log("x is 10");
}
Enter fullscreen mode Exit fullscreen mode

Explanation: = assigns a value. == (or === in JavaScript) compares two values. In the incorrect example, x is being assigned the value 10, which always evaluates to true, so the if block will always run, even if x wasn't originally 10.

Real-World Use Case: Simple Calculator

Let's build a very simple calculator that adds two numbers.

Python:

# Store the first number

number1 = 5

# Store the second number

number2 = 10

# Calculate the sum

sum = number1 + number2

# Print the result

print("The sum is:", sum)
Enter fullscreen mode Exit fullscreen mode

In this example, we use variables to store the two numbers we want to add, and then another variable to store the result. This makes the code much easier to read and understand. If we wanted to change the numbers, we just need to update the values of the number1 and number2 variables.

Practice Ideas

Here are a few exercises to help you practice:

  1. Temperature Converter: Write a program that converts Celsius to Fahrenheit. Use variables to store the Celsius temperature, the Fahrenheit temperature, and the conversion factor.
  2. Simple Greeting: Ask the user for their name and then print a personalized greeting using variables.
  3. Area Calculator: Calculate the area of a rectangle. Ask the user for the length and width, store them in variables, and then calculate and print the area.
  4. Basic Arithmetic: Create a program that performs addition, subtraction, multiplication, and division using variables.
  5. Shopping Cart: Simulate a simple shopping cart. Store the price of an item in a variable, the quantity in another, and calculate the total cost.

Summary

Congratulations! You've taken your first steps into the world of variables. You now understand what variables are, how to declare and assign values to them, and how to use them in simple programs.

Remember, variables are fundamental to programming. The more you practice using them, the more comfortable you'll become.

Next, you might want to explore different data types (like numbers, text, and booleans) and how they affect how you use variables. Keep practicing, and don't be afraid to experiment! You've got this!

Top comments (0)