> For the complete documentation index, see [llms.txt](https://ali-abdullah-nasiri.gitbook.io/python/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ali-abdullah-nasiri.gitbook.io/python/chapter-zero-introductions/basic-concepts-of-programming/introduction-to-data-types.md).

# Introduction to Data Types

This article aims to provide a comprehensive understanding of data types, including an explanation of the concepts of "static", "dynamic", "strong", and "weak" when discussing data types.

## What Are Data Types? <a href="#whataredatatypesanyway" id="whataredatatypesanyway"></a>

In programming, a data type is what we tell the computer the type of data it’s dealing with, e.g. a `string`, `number`, or `object`. When defining a variable, a computer needs both the name and the type of data before it can store and process it. This way it knows much much memory to set aside, how to access it and how to change it.

For example, in Python, `name = "Ali Nasiri";` creates a variable called `name` with a type of `string`.

```python
# Assigning the value "Ali Nasiri" to the variable 'name'.
name = "Ali Nasiri"
print(name)
```

Each programming language has a different way of handling how it assigns data types (`static` vs. `dynamic`) and how flexible (`strong` vs. `weak`) they are when trying to change them.

* **Static vs. Dynamic** defines how a language expects you to declare data types. Static typed languages require explicit definition of a data type when they create a piece of data (e.g. variable, parameter, return value). Dynamic languages are the opposite and can infer, or at least try to guess, the type that we’re using.
* **Strong vs. Weak** defines how flexibly a language allows operations between data types. For example, strongly typed languages will not allow you to add a `float` to an `integer`, without you converting it first, even though they are both numbers. On the other hand, a weak language tries its best to satisfy the programmer's wishes and perform these operations.

## Static and Dynamic Data Typing Systems <a href="#staticvsdynamicdatatypingsystems" id="staticvsdynamicdatatypingsystems"></a>

Static data typing languages ​​are those languages ​​that require the programmer to explicitly define a data type when creating a piece of data (variable, parameter, return value, etc.). Normally, these types are also fixed as that type for the lifetime of the program and do not change their type.

Let's look at an example:

```cpp
#include <iostream>
using namespace std;

int main() {
    int age = 25; // Here, the variable 'age' is declared and assigned an integer value.
    cout << "Age: " << age << endl;

    // age = "30"; // Uncommenting this line will result in a compilation error.
    // The variable 'age' is of type 'int', and assigning a string value will violate the static typing rules.

    float height = 1.75; // The variable 'height' is declared and assigned a float value.
    cout << "Height: " << height << endl;

    return 0;
}
```

In C++, variables have static types, which means their types are determined and fixed at compile-time. In the above example, the variable `age` is declared as an integer and assigned the value `25`. If we try to assign a string value like `"30"` to `age`, it will result in a compilation error because the static typing system of C++ does not allow such assignments.

On the other hand, the variable `height` is declared as a float and assigned the value `1.75`, which is a valid assignment since the type of `height` matches the assigned value's type.

Let’s contrast this with a dynamic data typed language. Below is an example:

```python
# Assigning different types of values to the variable 'name'
name = "Ali Nasiri"
print("Name:", name)

name = 25
print("Age:", name)

name = [1, 2, 3]
print("List:", name)
```

In Python, variables are dynamically typed, meaning their types can change during runtime. In the above example, the variable `name` is initially assigned a string value `"Ali Nasiri"`. Later, it is reassigned with an integer value `25` and then with a list `[1, 2, 3]`. Python allows this flexibility as it performs type inference at runtime, allowing variables to hold different types of values.

## Strong and Weak Data Typing Systems <a href="#strongvsweakdatatypingsystems" id="strongvsweakdatatypingsystems"></a>

Strong and weak typing refer to the strictness of type checking in programming languages.

In a strongly typed language, variables have a specific type that is enforced by the language. Operations between different types often require explicit type conversion. The type system ensures that only compatible operations are performed, and it helps prevent type-related errors. Examples of strongly typed languages include `C++`, `Java`, `Python`, and `Rust`.

Here's an example related to strong typing in the Python programming language:

```python
# Strong typing example in Python

num1 = 5
num2 = "10"

# Uncommenting the following line will result in a TypeError
# sum = num1 + num2

# To perform the addition, we need to explicitly convert num2 to an integer
sum = num1 + int(num2)

print("Sum:", sum)
```

In Python, which is a strongly typed language, operations between different types generally require explicit type conversion. In the example above, the variable `num1` is an integer, and `num2` is a string. If we try to directly add them together (`sum = num1 + num2`), it will result in a `TypeError` because Python does not automatically convert the string to an integer.

To perform the addition, we need to explicitly convert `num2` to an integer using the `int()` function (`sum = num1 + int(num2)`). This ensures that both operands are of the same type (integer) before performing the addition operation.

In contrast, weakly typed (or loosely typed) languages are more permissive with type handling. Variables can hold different types, and the language allows implicit type conversions. Operations between different types may be automatically converted or coerced. This flexibility can lead to unexpected behavior and potential type-related errors. Examples of weakly typed languages include PHP, JavaScript, and Perl.\
Here's an example related to weak typing in the JavaScript programming language:

```javascript
// Weak typing example in JavaScript

var num1 = 5;
var num2 = "10";

var sum = num1 + num2;

console.log("Sum:", sum);
```

In JavaScript, which is a weakly typed language, operations between different types can be automatically converted or coerced. In the example above, the variable `num1` is a number, and `num2` is a string. If we try to add them together (`sum = num1 + num2`), JavaScript will automatically convert the number to a string and perform string concatenation instead of numerical addition.

The resulting value of `sum` will be the string `"510"` because JavaScript's weak typing allows the concatenation of different types without explicit type conversion.

This behavior of automatic type conversion can sometimes lead to unexpected results or errors if not carefully handled.
