Lesson goal: Introduction to Variables

Previous: Zoom in or out | Home | Next: Mathematical expressions with variables

Variables are an extremely important aspect of programming. By look, variables are simply single word names, like score to keep track of the score in a game, count to keep track of hits on a website, or x as a variable in a math problem.

By purpose, variables are used to keep track of some information in a program that may change. For example, at one moment in a game, the score might be 100. A few seconds later, it might be 200.

In this lesson, you'll be able experiment with variables and see how they work.

variable=value
Move the mouse over a dotted box for more information.

Note that the name of your variable does not have to be "variable" as shown above. Often it is better to choose a variable some name that gives it some context. If you were keeping score in a computer game for example, you'd call the variable score or game_score. In mathematics, x and y are often used as variables, but with the computer and keyboard, you are free to have more descriptive variables. How about money or amount_left or EmployeeName? The only condition on variable names is that they start with a letter of the alphabet, contain no spaces, and typically cannot contain any funny characters either (like !@#$%^&). Variables can contain the underbar _ however.

Now you try. Try setting a variable to some value, then printing that value through the variable name.

Type your code here:


See your results here:

Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.