# Python Data Types

Table of Contents

## Python Data Types:

A variable in a program occupies some space in the computer’s memory where some value is stored. The value to be stored can be an integer, floating point number, a string or a Boolean value, etc. But now, the question arises that how much memory is allocated to these values and what is the range of the values that can be stored as different types of values cannot occupy the same space in memory. So to store these values, we require different data types depending on the needs of the application. The data types determine how much memory is allocated to store data and what operations can be performed on it.

## Mutable and Immutable Data Types:

Sometimes we may require to change or update the values of certain variables used in a program. However, for certain data types, Python does not allow us to change the values once a variable of that type has been created and assigned values. Variables whose values can be changed after they are created and assigned are called mutable. Variables whose values cannot be changed after they are created and assigned are called immutable. When an attempt is made to update the value of an immutable variable, the old variable is destroyed and a new variable is created by the same name in memory.

## Number Data Type:

Number data types store numeric values. They are immutable (the value of its object cannot be changed) data types, which means that changing the value of a number data type results in a newly allocated object.

• int (integer)- It is a whole number, positive or negative, without decimals, of unlimited length. It cannot hold fractional values. For Example- 12, 15, -45, -77789, etc.
• float (floating point)- Floating Point number is a number that contains a decimal point. For Example- 0.89, 45.9, 13.5, -14.8, etc.
• complex (complex numbers)- A complex number consists of an ordered pair of real floating point numbers denoted by a + bj, where a is the real part and b is the imaginary part of the complex number. The imaginary part is written with a j suffix. For Example- 14 + 4j.

## Sequence Data Type:

A sequence data type is a group of elements or items, for example, a group of integer numbers and a group of characters. A variable of a sequence data type may contain different values of the same data type or it may contain values of different types.

• str (string)- A string is a group of characters. These characters may be alphabets, digits, or special characters including spaces. Strings are immutable in nature. Strings are created using characters enclosed in quotes either in single quotes or double quotes or triple quotes. Python treats single quotes and double quotes in the same manner.
• List- A list is a sequence of items separated by commas and the items are enclosed in square brackets [ ].

The list is a mutable data type because we can update values in the list. Data inside the square bracket are in the format of positions 0, 1, 2, 3, and so on as shown below.

• Tuple- A tuple is a sequence of items separated by commas and items are enclosed in parenthesis ( ). This is unlike a list, where values are enclosed in brackets [ ]. Once created, we cannot change the tuple.

## Dictionary Data Type:

Dictionaries (dict) are Python’s most powerful data collection. Dictionaries store a mapping between a set of keys and a set of values. Dictionaries allow us to do fast database-like operations in Python. Dictionary literals use curly braces { } and have a list of key: value pairs.

## Set Data Type:

Set is an unordered collection of items separated by commas and the items are enclosed in curly brackets { }. A set is similar to a list, except that it cannot have duplicate entries. Once created, elements of a set cannot be changed.