Data Types

A data type tells the program what kind of value something is and what can be done with it. Every value in a program has a type, and the type determines how the program stores and works with it.

For example, the number 42 and the text "42" look similar, but they are different types. You can add two numbers together, but adding two pieces of text just joins them into a longer string.

Primitive Types#

Primitive types are the most basic building blocks of data in a program. Here are the most common ones:

String#

A string represents text. Strings are written between quotes.

"Hello, World!"
"Welcome to Odds and Evens!"
"Hello, World!"
"Welcome to Odds and Evens!"
"Hello, World!"
"Welcome to Odds and Evens!"

Integer#

An integer represents a whole number, positive or negative, with no decimal point.

0
42
-7
0
42
-7
0
42
-7

Float#

A float represents a number with a decimal point. It is used when precision beyond whole numbers is needed.

3.14
-0.5
100.0
3.14
-0.5
100.0
3.14
-0.5
100.0

Boolean#

A boolean represents one of two possible values: true or false. Booleans are commonly used to make decisions in a program.

true
false
True
False
true
false

Common Mistakes#

Confusing a number with a string that looks like a number The value 42 and the value "42" are different types. 42 is an integer you can do arithmetic with. "42" is text — adding it to another string produces "4210", not 52. Always check whether a value is meant to be used as a number or as text.

Using a float when an integer is needed If you need to count something — rounds played, number of attempts, a player’s score — use an integer. Floats introduce decimal precision that makes no sense for whole-number quantities and can cause unexpected comparison results.

Assuming boolean values are the same across languages In C# and JavaScript, boolean literals are true and false (lowercase). In Python they are True and False (capitalised). Using the wrong capitalisation is a syntax error in Python and will produce unexpected behaviour in other languages.

Resources#