Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Variables0:00 with Jay McGavren
Variables can hold numbers, text, a calendar date, or any other piece of data that can be stored in your computer's memory.
- Variables in a programming language are like variables in algrebra, but they can hold other things besides numbers. They can hold text, or a calendar date, or any other piece of data that can be stored in your computer's memory.
- When you assign a value to a variable, you're giving that value a name that you can refer to it by.
- You assign a value to a variable with a single equals sign:
number = 4 greeting = "hello"
- We can then use that variable anywhere we might use the original piece of data.
puts number # 4 puts greeting # "hello" puts number + 2 # 6 puts 12 - number # 8
- If we change the value the variable holds, the remainder of the program will use that new value instead of the old one.
number = 6 greeting = "hi" puts number # 6 puts greeting # "hi" puts number + 2 # 8 puts 12 - number # 6
- We can even replace the value a variable holds in the middle of a program.
- Valid variable names - same as method names.
- All lower case
- Numbers are legal but rarely used
- Snake case - separate words with underscores.
word = "hi" multiple_words = "hi there"
You need to sign up for Treehouse in order to download course files.Sign up