Introduction to Variables

What is a Variable?

The Storage Container

In programming, a variable is like a physical box in the computer's memory. To use it, you need a label (the name) and data (the value inside).

Welcome to the building blocks of programming! Think of learning variables like learning how to use a backpack—you need a place to put your things so you can find them later. In code, a variable is essentially a storage container or a box. First, you label the box with a name, like 'player_name'. Then, you put a piece of data inside, like the name 'Alex'. The computer sets aside a tiny piece of its memory just for this box.

The Game Score Example

Updating Values

Variables aren't static; they change as the program runs. Think of a game score starting at 0 and increasing as you play.

Great! We took the 0, added 10, and now the 'score' box holds 10. Think about your favorite video game. When you start, your score is zero. In code, we represent this with 'score = 0'. As you play and earn points, the computer takes the current value, adds to it, and puts the new result back into the box. The old value is gone, and the new one takes its place.

Sequential Execution & Assignment

The Flow of Logic

Computers follow instructions in sequential order: line-by-line, top to bottom. The assignment operator (=) means 'store the right side into the left side'.

Computers are very literal. They follow instructions line-by-line. We use the equals sign as an 'assignment operator'. It doesn't mean 'is the same as' like in math. It means: 'take the value on the right and move it into the box on the left'.

Mastering the Trace Table

Tracking Changes

A trace table is a professional tool used to track variable states. Let's trace this specific logic step-by-step.

Let's look at a more complex example. We have variables a, b, c, and d. To keep track of them, we use a trace table. In step 1, 'a' becomes 40. In step 2, 'b' becomes 'a + 50', which is 90. In step 3, 'c' becomes 'b + 10', which is 100. In step 4, 'd' becomes 'a + c', which is 140. Finally, we print 'd', giving us our final output: 140.

Practice: Fill the Trace Table

Your Turn

Complete the trace table for the code below. Enter the value for each variable at every step.

Now it's your turn. Look at the code on the left and fill in the missing values in the trace table. Remember to follow the instructions line-by-line. Excellent! You correctly tracked how the values changed at every step.

Common Pitfalls

Avoid These Mistakes

Programming has strict rules. Two common errors are using variables before they exist and forgetting that one box holds one value.

First, order matters. If you try to use 'a' before you've created it, the computer will get confused and throw an error. Second, remember: one box, one value. When you put a new number in, the old one is gone forever.

Why Did it Fail?

Look at the code snippet below. It contains a logical error. Explain why this code would not work as intended.

Look at this code: 'result = first_num + 10' followed by 'first_num = 5'. Why will this cause an error? Type your explanation and submit.