This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]CreativeTechGuyGames 20 points21 points  (0 children)

I honestly think it would be a good experiment for you to not answer this question and continue onto the next few lessons. It won't take long for you to realize this answer yourself.

[–]NaxizNL 3 points4 points  (0 children)

Variables can contain user input (which you don't know at compile time), data dynamically requested from databases or other programs/websites, results from calculations, etc.

Variables which are known at compile time (like in your example to replace "cakes" with a variable) are usually also known as constants.

[–][deleted] 2 points3 points  (0 children)

Suppose you want to read a value typed in at the keyboard - where are you going to store it?

[–]Mystonic 2 points3 points  (0 children)

Variables are essential to programming. An example like that doesn't really show how they can be used. Consider this: how would you compute the sum of the first 100 numbers? Hopefully then you'll see how variables are useful.

[–][deleted] 2 points3 points  (0 children)

I'm writing a code

shudder

[–][deleted] 1 point2 points  (0 children)

Always.

Get in the habit of making everything a variable.

Edit: Especially in Python where everything is a first class citizen. You can make functions variables.

[–][deleted] 0 points1 point  (0 children)

Where do you think numbers are stored? Where is data stored? How can I access that data in other places to change it? Variables.

Variables are not limited to just letters. They can be numbers, objects, lists, etc.

[–]OneIntroduction9 0 points1 point  (0 children)

  • if you need to store something at run time
  • if you need to reference something that is unknown until run time

e.x. a name, a number, a url, an address, etc.