## Python Data Types

As the name suggests, Data Type is the **type of data that a variable can hold**. Every value we declare in python or every variable we use in python belongs to some data type. We can say that these data types are the classes and variables are the objects of the classes.

## Different Classifications of Data Types

In Python, there are multiple ways to classify data types. Thought the main data types remain the same but just way to categorize changes. Let us go through each of them one by one:

### 1. Based on Type of Data

Types of Data | class | Details |
---|---|---|

Numeric | int, float, complex | holds numeric values |

String | str | holds sequence of characters |

Sequence | list, tuple, range | holds collection of items |

Mapping | dict | holds data in key-value pair form |

Boolean | bool | holds either True or False |

Set | set, frozeenset | hold collection of unique items |

Binary types | bytes, bytearray, memoryview | holds binary data |

### 2. Primitive, Container and User Defined:

- Basic/Primitive – int, float, complex, bool, string.
- Container/Collections – list, tuple, set, dict.
- User Defined – class.

### 3. Mutable and Immutable

**Mutable Data Types**: Data types in which the value assigned to a variable can be changed.

**Immutable Data Types**: Data types in which the value assigned to a variable cannot be changed.

## Variable Type – type() function

In Python, we do not declare the type of variable that we are using. So at a later stage, if we want to know the type of variable, then we use the type() function.

We are exploring the type function in detail in the below Data Types section.

## Data Types in Brief

These are the different data types python supports:

Data Type | Example | type() function | value returned by type() |
---|---|---|---|

Integer | a = 5 | type(5) | int |

Float | a = 5.5 | type(5.5) | float |

Complex | a = 5+7j | type(5+7j) | complex |

Boolean | a = True, a = False | type(True) | bool |

String | a = “John”, a = ‘Steve’ | type(“Steve”) | str |

List | a = [1,2,3,4,5] | type([1,2,3,4,5]) | list |

Tuple | a = (1,2,3,4,5) | type((1,2,3,4,5)) | tuple |

Set | a = {1,2,3,4,5} | type({1,2,3,4,5}) | set |

Dictionary | a = {‘name’:’Steve’, ‘company’:’apple’} | type({“name”:”Steve”,id:25}) | dict |

**Integer**

An Integer is a whole number. It can be positive or negative and can be of any size. An integer is denoted by an int keyword. Some examples of Integers are -225, 350, 987654321987654321, etc.

**Float**

Any number that contains a decimal point is a floating-point number or a float in short. There is no size limit for floats as well. A floating-point number is denoted by a float keyword. Some Examples of float are -23.7, 5698.5, 987654321987.21.

**Complex**

These are the numbers that contain a real part and an imaginary part. Both are separated by the ‘+’ sign, and j is appended with the imaginary part, which comes after the + sign. Complex numbers are denoted by a complex keyword. An example of a complex number is 7+8j, here 7 is a real number and 8 is an imaginary number. Complex numbers are not commonly used in our day-to-day programming, but they are used by expert mathematicians.

**Boolean**

It can take any of the two values, either True or False. Note that the first letter should be capitalized. It is named after a mathematician, George Boole. Boolean is denoted by the *bool* keyword. Some examples are x = True, y = False, etc.

**String**

A collection of Unicode characters is referred to as a string. A string can be enclosed in single, double, or triple quotes. A few examples of strings are ‘Logical Python’, “Logical Python”, “””Logical Python”””, etc.

**List**

In simple words, we can say that a list is a collection of data items. Square brackets [] are used to define the lists, and data items are separated by a comma. An example of a list is [“Steve”, “USA”, 654789].

**Tuple**

The tuple is the same as a list, but it is immutable which means, values inside the tuple cannot be changed. Parentheses () are used to define the tuple, with values separate by commas. An example of a tuple is (“Steve”, “USA”, 654789). Note that, this is the same example we used for the list, but here square brackets [] are replaced by parentheses ().

**Set**

A set is a container that contains only unique values. A set is defined using curly brackets {} and we have comma-separated values inside curly brackets. An example of a set is {2, 4, 9, 12}.

**Dictionary**

It is a collection of key-value pairs. We use curly brackets {} to define the dictionary, and inside the curly brackets, we have the comma-separated key : value pairs. An example of a dictionary is {“Name” : “Steve”, “Country” : “USA”, “Id” : “654789”}.

Here is an example of each data type:

Integer

```
a = 25
print(type(a))
```

Float

```
a = 25.5
print(type(a))
```

Complex

```
a = 5+6j
print(type(a))
```

Boolean

```
a = True
print(type(a))
```

String

```
a = 'Python'
print(type(a))
```

List

```
a = [1,2,3,4]
print(type(a))
```

Tuple

```
a = (1,2,3,4)
print(type(a))
```

Set

```
a = {1,2,3,4}
print(type(a))
```

Dictionary

```
a = {'firstname':'Steve', 'lastname':'Smith'}
print(type(a))
```

## Manually Specify Data Type

Though, Python assigns the data type to the variable of its own, but we can also manually specify the Data Type using the following constructor functions:

Data Type | Example |
---|---|

String | name = str(“Steve”) |

Integer | x = int(5) |

Float | x = float(8.8) |

Complex | x = complex(2+5j) |

List | names = list((“Steve”, “Bill”, “Elon”)) |

Tuple | names = tuple((“Steve”, “Bill”, “Elon”)) |

Dictionary | details = dict(name=”Bill”, age=66) |

Set | names = set((“Steve”, “Bill”, “Elon”)) |

Boolean | x = bool(5) |