Lesson 3: Datatypes and Variables
A variable in programming is a place in memory that serves to save data with which the computer can work, read, and change. It's something like a box with a mark on it: the mark is the name of the variable, and the content of the box is the current value of the variable.
A data type is an attribute that tells the computer what kind of value a variable holds. The most basic data types you will need in this course are:
- Integer – used to store whole numbers (5, -50, 102)
- Float – used to store numbers with decimals (3.14, -0.58)
- String – used to store text or characters ("Hello world!")
- Boolean – used to store logical values (True, False)
Here is a simple example to show you how this works:
If you have two variables, a = 2 and b = 5, which store integers, and you add them together (a + b), the outcome will be 7.
a = 2
b = 5
c = a + b
print(c)Output
7However, if you have the same variables stored as strings (a = "2", b = "5") and you add them together, the outcome will be "25". This happens because strings are treated as text/characters, so the computer joins them together instead of calculating a mathematical sum.
a = "2"
b = "5"
c = a + b
print(c)Output
25
Want XP, levels and progress tracking? Play this lesson in the CodeStart game.
Play Lesson 3