Page of Alena's post with the title Data structures introduction

⭐️ 💖

Back to home page

Alena Nikolaeva

Germany

Frontend Developer

Data structures introduction

Updated: 25.10.2020

7 min

Basic elements used in the languages of programming in general

Introduction

The types of data used in the main programming languages

Data Structure forms an integral part of any system or database design. No matter what problem are you solving, in one way or another you have to deal with data. Based on different scenarios, the information that is processed by computer can be represented in different ways. If we want to process numerical information we will use simple data values like int or float.

  • If we work with logical expressions with true or false results we will use logical or boolean data.
  • If we want to manipulate text, we will use character char or string data type.
  • To represent numerical (or even logical or text) information where the data in table form, such as vectors and matrices, or more complex structures will be used types of composite data.

The types of data used in the main languages are:

Simple

  • int
  • float
  • bool
  • char

Compound

  • tables, arrays, string
  • structure, record
  • Tuple, string (static, immutable)
  • List, dictionary, set (dynamic, mutable)

Notice that they might change depending on the language you use.

Integer (int)

In mathematics, integers are natural numbers, their negatives and the zero. The integers, in most programming languages including Python, are defined by the word int.

Python

>>> type(7)
<class 'int'>
>>> a = 45
>>> type(a)
<class 'int'>
      

Javascript

Number.isInteger(22); // true
Number.isInteger(22.2); // false
Number.isInteger('22'); // false      

Real numbers (floating-point representation - float)

Not all real numbers are can be accurately represented in computers, because many have infinite decimal points. However, according to the level of precision we want, we can approximate these numbers well enough. For several decades it has been agreed the use of the IEEE 754 standard to represent real or floating point numbers, using scientific notation.

>>> 25e-2
0.25
>>> 4e3
4000.0
>>> type(4.)
<class 'float'>
>>> .2e2
20.0
>>> 1.1 + 2.2 # represented with an error
3.3000000000000003

Booleans (boolean)

The data type for representing logical or boolean values in Python is bool, in Pascal and C++ are defined as boolean and bool, respectively. Boolean data takes the value True (1 logical) or False (0 logical).

>>> a = 3 > 2
>>> a
True
>>> type(a)
<class 'bool'>
>>> 4 > 5
False

Character

The character data type used in various programming languages is the scalar element or indivisible from the texts used in computing. The texts are called character strings. For example, the ASCII characters ordered from the decimal value 20 to 127 are

 ! “ # $ % & ‘ ( ) * +, -. / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V
W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~

The order in which they are represented serves to evaluate which is greater than the other, according to the numerical code in which they appear in the ASCII code. For example, 'b' is greater than 'a'.

>>> type('a')
<class 'str'>

It can be seen that the character a in Python is of the string type, although in other languages such as Pascal would be of the character type (char).

Compound data types

Compound or structured data comprise data with value elements of the same type or of different types, which are represented unified to be saved or processed. The elements can all be of the same type, such as character strings, and in this case are called homogeneous structured data. Other languages (C/C++, Matlab, Pascal) have homogeneous structures such as the array (arrangement or table), very useful for with vectors or matrix (two dimensional array where each data element is of strictly same size). The elements can also be of different types: heterogeneous data. In Python, except for the string, the other types of composite data are heterogeneous, achieving a high flexibility for data processing of problems of all kinds.

In Python, composite or structured data can be classified into two groups, according to to the characteristic of whether or not its elements can be changed, reduced or extended: data structured mutable and immutable

  • immutable: the elements of your sequence cannot be changed or removed (character strings (string) and tuples (tuple), as data sequences, and frozen sets (frozenset)
  • mutable: lists, sets and dictionaries. Lists and sets can be considered as the mutable equivalents of tuples and frozen sets (Frozenset), respectively.

En example of frozenset. It cannot be changed, removed or added to.

>>> FS1 = frozenset({25, 4, 'a',2, 25, 'home', 'a'})
>>> FS1
frozenset({2, 'a', 4, 'home', 25})
>>> type(FS1)
<class 'frozenset'>
>>> len(FS1)
5

The lists, as well as the tuples and strings, are made up of a sequence of data. But unlike of the frozenset their elements can be modified, eliminated or increased.

>>> v = list(range(1,11))
>>> v
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Other common data structures are:

  • Queues
  • Trees
  • Graphs
  • Stacks
  • Hash Tables

Being such a large topic, there are great article that have been around for a long time and definitely worth reading if you interested in the subject. Like this one:

Why learning about data structures?

Data structures are important for storing data efficiently. Combined with algorithms, they allow us to write efficient and optimized computer programs. Knowing this concepts will help when you try to understand some of the existing data systems or organizing data in memory. In a coding interviewing context, picking the right data structure can have a major impact on performance.