In programming and for databases, data types are used to specify the type of a variable. This also determines, for example, which operations can be performed with these variables and which lead to errors. For example, when storing a text, mathematical operations such as additions are not possible.
What are Data Types?
In computer science, one defines data types for which a certain set of operations is possible without problems. With all data, which belong to a data type, these operations can be accomplished and it is ensured that no error message occurs.
For example, for the “Integer” data type, the “Add” and “Subtract” operations are defined. This means that if we have any two elements of the “Integers” data type, then they can add or subtract and no error will occur. On the other hand, two objects of the “Text” data type cannot perform this operation because this is not defined for the data type.
What do you use Data Types for?
The use of data types makes it possible to perform certain operations between several variables in programming. For each data type, certain calculations or transformations are defined, which can be executed with another variable of the same data type without problems.
To make sure that runtime errors cannot occur during operations with multiple variables, one uses the so-called type system.
What is the Type System in Computer Science?
The Type System is the term used in computer science to describe the possibility of restricting the value ranges of a variable within a programming language. A programming language, which has a type system, is called typed. These systems can be classified into a total of three classes:
- Strong vs. Weak: This is about how strictly the respective programming language distinguishes the types. For example, strict type systems do not allow variables to be converted to another data type after the variable has been defined.
- Dynamic vs. Static: This dimension is about when the typing of the object takes place. For example, in Python, the variable itself does not have a data type, only the object that is assigned to the variable does. This is called dynamic typing. However, this also means that errors due to incompatible data types are not detected until the entire program is started. With static typing (for example in Java), the data type of the variables must be explicitly defined while writing the program.
- Explicit vs. Implicit: This dimension is closely interwoven with static/dynamic typing. It is a question of whether the data type of a variable is already explicitly specified during the definition, or is only implicitly recognizable via the assignment of the object.
General Data Types
Depending on the programming language or database, different data types are defined, which is why the names can also differ slightly. However, the general data types are very similar between different systems and programming languages.
Integers are used for numbers, i.e. for positive and negative numbers without decimal places, for example, -841 or +903.
Floating Point (Float)
The float data type is also used for numeric variables, but unlike integers, they also have decimal numbers, e.g. -130.45 or +923.58923.
The string in turn denotes textual variables whose values are stored in quotation marks, i.e. ” ” or ‘ ‘. In addition to character strings consisting of letters, the string can also store numbers or other symbols. However, these are not interpreted as numbers, which means that no arithmetic operations are possible with them.
Boolean data types are used when a variable can take exactly one of two possible values. In many cases either the value pairs, 0/1 or true/false is used.
The Datetime data type stores values that contain a date including a time, such as 2021-09-12 15:23:41, so the format used for this is YYYY-MM-DD hh:mm:ss.
The timestamp is another way to store temporal information in a variable. The most common is the so-called Unix Timestamp, which measures how many seconds (depending on the format also milliseconds) have passed since 01/01/1970.
This data type is used to store single letters, symbols, numbers, etc.. It is also possible to store a single space character.
Why should you care about Type Systems as a Programmer?
In many programming languages data types are explicit, so already when a variable is defined, it must be specified which data type it has. So in these cases, a programmer has no choice but to deal with data types.
However, it also makes sense in implicit programming languages, such as Python, to keep the data types at least in mind. As soon as Python recognizes a data type with which the desired operation is not executable, a so-called type error is returned.
a = 'string1' b = 'string2' print(a-b) Out: TypeError: unsupported operand type(s) for -: 'str' and 'str'
So when these errors occur, you immediately know where to look to get the code running again. It must be an operation that is not possible with the given data types. So, either we used the wrong operation for the given data types or the variables have other data types that we did not intend them to have.
To avoid this mistake, we can check the data type of a variable and only run the operation if we are sure that the types are correct:
a = 1 b = 2 if isinstance(a, int) and isinstance(b, int): print(a-b) else: print('The operation is not possible.') Out: -1
Since Python version 3 there is also the possibility to declare the data type when defining a function. That way, the programmer defines exactly which types she expects as inputs and what types she provides as output.
def example_function(input1: int, input2: list) -> int: return input2[input1]
However, a type error is not automatically issued if this declaration is violated. Nevertheless, the specification helps other programmers in the team to understand the code better and to adjust downstream functions to this function at hand. In addition, it helps the programming front-ends to better adjust their auto-completion to the given data type and thus detect errors at an early stage.
Another advantage of data types is the optimization of performance and data storage. For example, integer variables require significantly less memory than decimal numbers. At the same time, integers can also process values faster than variables of the type double.
This is what you should take with you
- Data types are used to define the type of a variable. This also determines which operations are possible with the variable and which are not.
- Typing is the ability to restrict the value ranges of a variable.
- In general, there are the data types Integer, Floating Point, String, Boolean, Datetime Timestamp, and Character. In addition, there are more defined data types, which can vary depending on the programming language or database.
Explanation of the Apache Hadoop Distributed File System with examples and benefits.
Other Articles on the Topic of Data Types
- On this page, you will find an overview of all data types in the Python programming language and useful commands on how to define or change them.