Storing information
Computers remember
Have you ever forgotten a distant relative's name or a friend's birthday? Well, computers don't forget these things when they're told to remember them.
OK computer: remember Joe as my name display my name
//Output Below
Joe
The North .. err .. the computer remembers until we tell it to hold on to a different name.
Psst: these instructions are a little oversimplified, of course, but we'll get to actual computer-speak in no time.
Variables
To remember things, computers use variables, a concept that's a lot like boxes with labels. Variables, too, have a label and content.
OK computer: remember 1970/1/1 as my birthday display my birthday
//Output Below
1970/1/1
Great! Just like you'd label a box my birthday
, it makes sense to call a variable my birthday
, right?
Labels
In computer-speak, labels have to be single letters or single words, which disqualifies my birthday
.
Can you think of a valid label?
- my_birthday
- 1/1
- your birthday
- 1970
Sweet it's the first! Labels don't have to be real words and can include numbers, as long as they don't start with them. Also, it's smart to use meaningful labels.
Tracking information
Now, why would we use variables when we could also use the information they carry?
OK computer: remember "Walter White" as my_name display my_name
//Output Below
Walter White
Well, because variables can change the values they represent as the program runs. That's why we can use variables to keep track of things.
Different kinds of information
There are different kinds, or types, of information. Most importantly, there are words and numbers.
Which two statements would you say make sense?
OK computer:
- add "White" to my_name
- add 1 year to my_birthday
- subtract 1 year from my_name
It wouldn’t make sense to subtract a year from your name, would it?
Numbers
There are different kinds of numbers as well. Among others, whole numbers and floating-point numbers.
OK computer: divide 10 by 3 display the result divide 10 by 1 display the result
//Output Below
3.3333333333 10
In practice, floating-point numbers often occur when we divide whole numbers.
Using numbers
Also, numbers are great for arithmetic.
OK computer: remember 1 as my_number add 42 to my_number multiply my_number by 3 display the result
//Output Below
129
Boom! Even if we were mathematicians, it'd be hard to beat the arithmetic accuracy and speed of computers.
Strings
Words, which are also called strings, have quotation marks and can't be used for arithmetic. They're still pretty great, though.
Have a guess: What's the computer going to display?
OK computer: remember my_name as "Walter" add "White" to my_name display my_name
- WalterWhite
Yup, the strings are combined without a space. As spaces are also strings, we've got to add them if we want to see them.
Lists
As we've seen, variables are kind of like boxes for values. What if we want to store more values, though?
Well, there are boxes with space for multiple values: lists.
OK computer: create a list add "cereal" to the list add "milk" to the list display the list
//Output Below
cereal, milk
See that? Lists are a great way to store multiple values; especially, if they're of the same kind.
Comments
NorvorGloria
It's somehow understandable but l don't get it completely. I don't know if it's helping me.
Jan. 27, 2020, 10:41 a.m. Reply