What data types are there?

What data types are there? Chapter 8

Remember how I explained that everything in programming is about data processing and manipulation? Well, let’s look at what types of data we can play with.

These data types are common to any programming language. They’re also the most basic and primitive data types.

Let’s dive right in!

Number data types

Integers

An integer is a number that doesn’t have a decimal point. It can be both positive and negative.

In Python, even though we don’t need to declare types, the interpreter implicitly assigns the type of int to integers.

In C++, an integer is also declared by the type int.

Now, we need to note one very important thing. As you can remember, all our numbers are transformed into the binary system so that the computer can store them. When transforming them, the computer only gives it a certain number of bits (space for 0s and 1s) to store the number. This means that you can’t store really long numbers. Well, you can, but the computer truncates some of the digits. This will undoubtably introduce bugs to your program.

What C++ does to counter this is that it has more data types for integers. The default int has 32 bits of precision, so it can store integers from -2147483648 to 2147483647. C++ also has the type short, which can store integers from -32768 to 32767, and the type long long int, which can store integers from -9223372036854775808 to 9223372036854775807.

On the other hand, Python 3 (yes, this is a feature only for Python 3) has only one type for integers, and it can hold an infinitely large integer. This is possible because of Python magic. Obviously, that’s a joke, but it’s not important to know for now. As I’ve mentioned before, you can’t learn everything at once. This is something at a deeper level of the onion. If you’re really curious, try searching for something like “Why do Python 3 integers not have a max?”.

Here’s how you declare and assign an integer value to a variable in Python:

x = 43
y = -7

For C++, it would be like this:

int x = 43;
int y = -7;

Floating-point numbers

A floating-point number (FPN) is a number with a decimal. It can be both positive and negative.

In Python, if you assign a FPN to a variable, the interpreter implicitly assigns the type float to that variable.

It looks like this:

x = 23.88
y = -0.72

In C++, a FPN can be declared as float or double. float has 32 bits of precision, and double has 64 bits of precision. It’s called “double” because it’s a double precision float. This usually means that a double can store more digits after the decimal point than a float.

It looks like this:

float x = 23.88;
double y = -0.72;

Truth values data type

Another important data type is the boolean. This data type can only take two values: true or false. The name doesn’t have any logic, it’s just named after the mathematician that created the branch of mathematics where booleans are used.

In both Python and C++, the type is called bool.

For Python, it looks like this:

x = True
y = False

For C++, it looks like this:

bool x = true;
bool y = false;

Text data types

Characters

This is a data type that stores a single character in a variable with the type char. Python doesn’t have this concept, but C++ does.

It looks like this:

char x = 'L';

What C++ actually stores in the variable x is the ASCII integer value of the character L.

It might seem weird to have a data type for a single character instead of having a data type for text (multiple characters), but let me ask you this: How many bits of memory do you want to reserve for a “text” data type? Depends on the length of your text, right? Well, char ensures a fixed length that the computer knows to always set aside for your variable (8 bits). Also, you will see that the “text” data type that we’re looking for is just a bunch of char variables next to each other.

String (of characters)

Here’s the “text” data type that we were looking for. In most languages, we call it a string (which implies a string of characters).

Python calls its string data type str. It virtually has an unlimited amount of space to store text.

It looks like this:

x = "This is a string."
y = "This is kind of a long stringgggggggggggg."
z = "-4"
a = "variable z is also a string because of the quotation marks"

For C++, the string data type is not one of its “basic” data types. Instead, the string data type needs to be imported from a C++ library called string. Under the hood, a string in C++ is just an array of characters. Array means an ordered list. Don’t worry if that word doesn’t really make sense because we will learn about arrays in the next chapter.

It looks like this:

#include <string>

int main() {
    std::string x = "This is a string.";
    std::string y = "This is kind of a long stringgggggggggggg.";
    std::string z = "-4";
    std::string a = "variable z is also a string because of the quotation marks";
}

Play with the data types

Now that you know how to run a program that prints something in the console, try playing around with the data types.

In Python, since you don’t need to declare the types explicitly, try making a few variables and adding them together. Do that for string variables too:

x = 5
y = 82
z = x + y
print(z)

In C++, try putting a value that doesn’t match the declared variable data type. What happens?

int x = 3.2;
double y = 4;

Next: Chapter 9 – What are data structures?

If you want to start from the beginning of the series, go to chapter 0.

By Radu

Software Engineer and Computational Scientist graduated from the Technical University of Munich. Worked at Shopify, DRW and Ubisoft.

Leave a comment

Your email address will not be published.

Join my mailing list to get an email whenever a new chapter comes out!