Python variables and data types
// updated 2025-11-06 20:57
A variable simply stores data inside a named piece of memory:
Declaring variables
Recall in the input article that we stored input for later use inside a thing called a variable:
name = input('Please enter your name')We then noticed how we don't need to use a keyword like "let" (in JavaScript, for example) to declare variables! Simply state the variable name and the data:
variable = valueThe value could have any type: string (text), number, Boolean (a true or false value) or even another variable!
Casting values to specific types
However, to ensure that a value has a certain data type, we can cast it by specifying its type in front of it:
position1 = str(1)
position2 = str(10)
position3 = str(11)So, we have just ensured that each of those variables have string representations of those numbers, on which we can no longer perform mathematical operations! Were we to do this:
positionX = position1 + position2 + position3We would have positionX with a value of "11011" (a string concatenation or "gluing together" of "1" and "10" and "11") instead of "22" (adding the numbers 1+10+11)!
Finding the type of a variable
To find out the type of a variable, Python has a built-in function simply called type:
temperature = 100
print(type(temperature))
# <class 'int'>Other data types
More basic "formats" of data in Python (with their Python name) include:
- integer ("int")
- discrete or whole numbers like -2, 0, 3, 700
- float
- decimal numbers like -2.5, 0.29, 1.18
- boolean ("bool")
- true or false
- string ("str")
- text that can include "numbers" (or rather, digits)