One of the most powerful tools of programming is the variable: a storage container for information in your programs.
You are probably familiar with variables in math. (“Solve for x: 2x + 4 = 3x”) In math problems, the goal
is often to figure out the secret value of a variable. Once you figure it out, the problem is finished.
Variables work differently in computer science:
- You can create them whenever you want. In fact, you already made one. Remember typing
number = 5above? You made a variable called “number” and set its value to 5. This is called declaring a variable. - In computer science, you get to decide what value variables have, and you can change them whenever you want. This is called assigning values to variables. Try this:
>>> name = "Willie"
>>> "my name is " + name
- You can call them whatever you want (no spaces though!) In math, variables have names like
x,y, andt. In computer science, we usually call them things likenumber_of_coins_in_my_handordirection_my_character_is_facingbecause it’s less confusing.
You can think about variables as little boxes in your computer’s memory that store things so you can use them later.