# Data types in python | Python tutorial

## There are mainly 7 data types in python:

1. Numerical
2. String
3. List
4. Tuple
5. Dictionary
6. Set
7. Range

Let’s try to understand these data types.

## 1. Numerical data types

Numerical data types mainly have numerical values and these are of 4 types, i.e. integer, float, complex and boolean.

### 1.1- Integers in python

Integers mainly refer to the whole number values without any decimal point values.
To know the type of data in a variable, we use type(x) function, where x is replaced by the variable or data.

### 1.2- Floating point numericals in python

Floating point numbers are the numbers having decimal point values.

### 1.3- Complex numbers in python

Complex number are the unreal numbers in which we represent the imaginary part using j and add it to the real part. These are generally not used by programmers in ordinary programs. So you don’t have to burden yourself with this right now.

### 1.4- Boolean data type in python

Boolean in python returns True or False, so it is used for comparison or category related operations.

## 2. String data type in python

A string is used to store text or a collection of UNICODE characters. In most of the programming languages including python, a string is written inside ‘single’ or “double” quotes and ‘’’triple quotes’’’ are used for multiline strings.

Indexing can be used to access the characters of a string.

In python, strings are immutable, i.e., they cannot be changed positionally after they’re created. For example,

## 3. List in python

A list is a collection of arrays which is changeable and ordered which means they’re indexed.
We use square brackets to declare lists.

Note that we can different data types in a list, even a list can be passed as a value to another list and also duplicate entries are accepted.
We can access or modify the values of a list using the index numbers.

Many type specific functions can be used to operate over lists which are discussed separately.

## 4. Tuple in python

A tuple is an ordered and immutable group of objects. It can be understood as an immutable list. Parentheses are used to represent and define tuples. As it is ordered, values inside a tuple can be accessed by indexing.

## 5. Dictionary in python

Dictionary is an unordered collection of key-value pairs, which is changeable. We use curly braces to declare a dictionary in python and provide key-value pairs as entries.

The keys can be used as the index for its corresponding value. As the keys are to be used as an index, so they’re unique in a dictionary while the values can have duplicate entries.

## 6. Sets in python

A set is a collection of unordered, unique, arbitrary and immutable objects.

Unordered means that the objects are not stored in a fixed order, their position in the set is not ‘indexed’, so sets can be used when the order of data does not matter at all, but still sets are iterable.

Unique means that a set does not include duplicate elements so it can be used to remove duplicate data elements from a file or some data.

Arbitrary means that a set can have objects of various data types.

Immutable means that you cannot replace an object from a set, although we can add/remove objects from a set, but replacement of objects is not allowed in a set. Again, don’t confuse immutability with operations, means, you can still perform operations on a set. Also, a set can only contain immutable objects, i.e., it cannot have a list as its element but can have a tuple as an element as it is immutable. See example below.

In python, a set can be defined as follows:

OR

Note that the string object ‘ubuntu’ is also an element of the set, it is to illustrate that a string in python is also immutable and immutable objects can be an element of a set as we’ve already discussed.

From python 2.x to python 3.x, the representation of a set has also been changed. Which is well understood from the example given below, [set([1,2,3]) vs {1,2,3}:

Now let’s learn about some of the basic operations on a set and we’ll do that using some QnA.

Q- How to find the length of a set in python?
A- len() function in python sets.

Q- How to access the elements of a set?
A- Use loop.

And this is all about sets on this site as these are not used so widely.

## 7. Range in python

range in python is defined by the term itself. It is used whenever we are iterating through values. Actually it is a python built-in function which produces a list of successive integers. And in python 3.X you have to put it into a list to display its values. It excludes the higher limit. See in the example below.

In this article, we learned about the basic data types used in python. This is all about introduction to basic data/object types in python. You can learn more from the official website of python.

Linux and Python enthusiast, in love with open source since 2014, Writer at programming-articles.com, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →