A data type is simply what kind of thing a value is. A number, some text, a yes/no answer, or a collection of other values. The type decides what you are allowed to do with it.
This sounds abstract until it explains one of the most common beginner surprises:
print(2 + 2) # 4
print("2" + "2") # 22Same symbols, different result, because the first line has numbers and the second has text. With numbers, + adds. With text, + glues.
The four types you will actually use#
Numbers#
Most languages separate whole numbers from decimals.
count = 7 # integer (int)
price = 19.99 # decimal (float)You can add, subtract, multiply and divide them. In Python, dividing with / always gives a decimal: 10 / 2 is 5.0, not 5. Use // if you want the whole-number answer.
Text (strings)#
Anything in quotes is text, even if it looks like a number.
name = "Ayesha"
postcode = "54000"postcode is text. You could not usefully multiply it, and you would not want to — leading zeros matter in codes and would be lost in a number.
True and false (booleans)#
A boolean has exactly two possible values. They are what conditions produce.
is_open = True
has_paid = False
print(10 > 3) # TrueCollections#
Lists hold several values in order; dictionaries hold labelled values.
scores = [10, 8, 9]
person = {"name": "Sam", "age": 30}More on these in arrays and lists explained and Python dictionaries explained.
The one that catches everyone: input is text#
When you ask a person to type something, you get text back, even if they typed digits.
age = input("Your age: ")
print(age + 1) # TypeErrorPython refuses because you asked it to add a number to a piece of text. The fix is to convert first:
age = int(input("Your age: "))
print(age + 1) # worksJavaScript has the opposite problem: it converts silently and gives you a wrong answer instead of an error.
const age = "20";
console.log(age + 1); // "201" — glued, not added
console.log(Number(age) + 1); // 21 — converted firstConverting between types#
| Goal | Python | JavaScript |
|---|---|---|
| Text to whole number | int("42") |
parseInt("42") |
| Text to decimal | float("3.5") |
parseFloat("3.5") |
| Number to text | str(42) |
String(42) |
| Check the type | type(x) |
typeof x |
Conversion can fail. int("hello") raises an error, and parseInt("hello") gives you NaN, which stands for “not a number” and quietly poisons every calculation it touches.
How to check a type when you are stuck#
When a line behaves strangely, print the type before it:
value = input("Enter something: ")
print(type(value)) # <class 'str'>const value = document.querySelector("#age").value;
console.log(typeof value); // "string"Nine times out of ten the type is not what you assumed, and that assumption was the bug. This habit is the core of reading errors properly.
Questions people ask#
Why do some languages make you declare the type?
Languages like Java, C# and TypeScript ask you to state the type up front so the computer can catch mismatches before the program runs. Python and JavaScript work it out as they go, which is faster to write but lets some mistakes survive until runtime. Neither approach is better; they trade speed of writing against speed of catching errors.
What is None or null?
They represent “no value here”. Python uses None, JavaScript uses null and undefined. Trying to use one as if it were a real value is behind a large share of runtime errors, such as JavaScript’s “cannot read properties of null”.
Is a decimal always exact?
No. 0.1 + 0.2 gives 0.30000000000000004 in most languages, because decimals are stored in binary and some fractions do not fit exactly. For money, work in whole units such as paisa or cents, or use a decimal type built for it.
Where to go next#
- If statements explained — booleans put to work
- Python data types in more depth
- Common Python errors — most of them are type errors