# What data types does Python have?

Python data types are essential for representing, processing and using data. By utilising **different data types**, you can store information in an efficient manner and optimise the performance of your application.

## What are Python data types?

Python data types are **value categories** that are used to represent different kinds of data. They dictate how information can be stored and manipulated. Python provides a range of different data types, including integers (whole numbers), floats (decimal numbers) and strings (character strings). More advanced types include lists, tuples, dictionaries and sets. Data types facilitate data structuring and processing, making them pivotal to Python programming.

The primary objective of Python data types is to structure data according to predefined rules so the needs of diverse applications can be fulfilled. Each data type possesses **distinct functions and properties**. For instance, lists maintain items in an ordered sequence, while dictionaries employ key-value pairs for targeted data retrieval. Selecting suitable data types for your data will make your program flexible and easier to maintain.

## What kinds of data types are there in Python?

Python has various built-in data types, including:

- **Numeric data types**: `int`, `float`, `complex`
- **Strings**: `str`
- **Sequential data types**: `list`, `tuple`, `range`
- **Binary types**: `bytes`, `bytearray`, `memoryview`
- **Dictionaries**: `dict`.
- **Boolean data types**: `bool`.
- **Sets**: `set`, `frozenset`

### Numerical data types

There are several Python data types that you can use to work with numbers:

1. Integer (`int`): The integer type represents whole numbers without decimal places.
2. Long (`long`): Long is used for integers with an unlimited length. As of Python 3, `long` and `int` are merged.
3. Float (`float`): The float type includes numbers with decimal places.
4. Complex (`complex`): The complex type includes complex numbers with a real part and an imaginary part, indicated by the suffix `j`.

```python
# Variable with integer value. 
a=3 
 
# Variable with float value. 
b=3.17 
 
# Variable with complex value. 
c=50+7j
```

### Strings

A [Python string](https://www.ionos.co.uk/digitalguide/websites/web-development/python-string/) (`str`) represents a sequence of characters. You can mark them with **single, double or triple quotation marks**.

```python
# Single quotes 
str1 = 'Hello World!' 
 
# Double quotes 
str2 = "This is a string." 
 
# Triple quotes for multiline strings 
str3 = '''This is a multiline string.'''
```

In Python, strings are **immutable**, meaning they cannot be changed once created. However, strings support numerous methods and operations for manipulation, concatenation and analysis. You can store the results in variables to get new strings.

Examples of string operations:

- Length of a string: `len(str)`
- Slicing: `str[start:end]`
- Concatenate strings: `str1 + str2`

### Sequential data types

Sequential data types in Python are data structures that store an **ordered collection** of elements. They allow access to elements based on their position within the sequence. There are several sequential Python data types:

**Lists (`list`)**: [Python lists](https://www.ionos.co.uk/digitalguide/websites/web-development/python-list/) are **modifiable sequential data types** that represent an ordered collection of elements. You can change, add and remove elements in a list. Lists are created using square brackets and contain elements of different data types.

```python
my_list = [1, 2, 3, 'Hello', 'World']
```

**Tuple (`tuple`)**: Tuples are **unchangeable sequential data types** which, like lists, display an ordered collection of elements. In contrast to lists, tuples cannot be changed afterwards. Use round brackets for tuples.

```python
my_tuple = (4, 5, 6, 'Python')
```

**Range (`range`)**: This is a special Python data type utilised for generating sequences of numbers, often used in loops and iterations. The `range` data type creates a sequence of integers within a specified range. The **range object** generates numbers on demand rather than storing them as a complete list in memory, enhancing efficiency, particularly with large number sequences.

```python
# Range from 0 to 4 
my_range = range(4) 
for i in my_range: 
    print(i) 
# Output: 0, 1, 2, 3
```

### Binary types

**Bytes (`bytes`)**: The data type `bytes` represents an **unchangeable sequence of bytes**. Bytes can be created using the `bytes()` constructor or the prefix `b`.

```python
my_bytes = b'Hello'
```

**bytearray (`bytearray`)**: In contrast to `bytes`, `bytearray` belongs to the **modifiable Python data types**, representing a sequence of bytes. This means that you can modify the values after declaration.

```python
my_bytearray = bytearray(b'Python')
```

### Dictionaries

In Python, a dictionary (`dict`) is a data structure that stores an unordered collection of elements in the form of **key-value pairs**. Unlike lists or tuples, which contain an ordered sequence of elements, unique keys are used to access elements in a dictionary.

```python
my_dict = {
    "name": "Max",
    "age": 25,
    "city": "Berlin"
}
```

### Boolean data types

Boolean Python data types represent **truth values** that can be either true (`True`) or false (`False`). This data is crucial for logical evaluations and decisions within a program.

```python
a = True
b = False
result_1 = (a and b) # returns False
result_2 = (a or b) # returns True
result_3 = (not a) # returns False
```

### Sets

A set is an **unordered collection** of unique values that doesn’t allow duplicates. You can use it to store multiple elements where each element is unique.

```python
my_set = {1, 2, 3, 4, 5}
```

A `frozenset` is an **unchangeable version of a set**. Once created, elements cannot be added, removed or changed.

```python
my_set = {1, 2, 3, 4, 5}
frozen_set = frozenset(my_set)
```


This is a markdown version of: [https://www.ionos.co.uk/digitalguide/websites/web-development/python-data-types/](https://www.ionos.co.uk/digitalguide/websites/web-development/python-data-types/) for AI/LLM consumption.