Python3 Data Types

In this Python article, we will learn about the classification and different Python data-types and will also implement some examples.

In every programming language, we use data ranging from transferring data, manipulating data to get the output, user input data, etc. Data can be of many types e.g. character, integer, string, float, etc.

1. Classification of Data types in Python

Everything in Python is an object so are the data types. Python data types are divided into two categories: mutable and immutable objects.

1.1. Mutable objects

Mutable means something that is “liable to change.” So in Python, when we can change the value of a data type, it is known as a mutable object or mutable data type.

E.g. List, dictionary, set.

1.2. Immutable objects

Immutable means something “that can not be changed or updated.” So in Python, when we cannot change the value of a data type, it is known as an immutable object or immutable data type.

E.g. String, tuple , frozenset

2. Python Basic Data Types

In python, we do not have to define the type of a variable. The interpreter automatically binds the value with its data type.

There are ways by which you can define the type of variable explicitly where type safety is critical but generally, it is not required.

Table below represents some of the built-in data types

Python Inbuilt data types

3. Numbers

In Mathematics we have a chapter called numbers where we learn about integers, decimal number, complex number, real number etc.

Python also deals with different types of numbers like integer, float, decimal, and complex numbers.

3.1. Integer (int)

Integers are the numbers without decimal which can be positive as well as negative.

E.g. 2 , -2 , 45698 , -45896

We use type() function to know the data-type of an object. So let’s declare some variables and verify whether they are of integer type or not.

number1 = 10

number2 = -10

number3 = 48697

number4 = -14589
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>

We will discuss the detailed explanation of these data types, in future articles.

3.2. Decimal (float)

Floating numbers can be represented in two forms: Decimal and Exponent.

The decimal form contains a decimal point even if it is an integral value that means if we want to write 2 in the decimal form it will be 2.0.

More examples of Decimal form are 2.0 , -5.2 , 29.256 , -596.26 etc.

Note: 11,256.25 is invalid because we can’t use comma(,).

The exponential form is basically a scientific notation where the number is broken into two parts: mantissa and exponent.

The mantissa is the floating-point number of decimal form and for the exponent, we use e.

Example: 222.48 can be represented as 222e48 in exponential form.

Remember : Floating points are accurate upto 15 decimal points.

number1 = 2.3

number2 = -5.9

number3 = 4e23

number4 = -4e+23
<class 'float'>
<class 'float'>
<class 'float'>
<class 'float'>

3.3. Complex number (complex)

In Python we can also represent the complex numbers and they are written in the form : <real-part> + <imaginary-part> + j

E.g. 2+3j , 5.89j , 5-7j , 1j

number1 = 2+3j

number2 = 5.89j

number3 = 5-7j

number4 = 1j
<class 'complex'>
<class 'complex'>
<class 'complex'>
<class 'complex'>

4. Sequences

The sequence is an ordered collection of data. An ordered collection remembers the order of data and can always be accessed sequentially i.e. if we iterate an ordered collection then the elements are processed in the same order.

There are seven types of sequences in which 3 (highlighted types) are most commonly used:

  • Strings
  • List
  • Tuples
  • Byte Arrays
  • Unicode string
  • Buffers
  • range objects

Here we will discuss only the 3 most commonly used data types which we use more frequently in our daily programming tasks.

4.1. String (str)

Python Strings are the combination of characters that are written inside either a single double quote (“example”), three double-quotes (“””example”””), or a single quote (‘example’).

Note: Three double quotes (“”” “””) are used when we have to deal with multiline strings.

Some of the other programming languages have a character data type, but in Python, there is no in-built character data type, and can simply use a string of length 1.

Example : “It’s a Beautiful Day to save lives” , ‘Dance it out’

Sentence1 = "It's a Beautiful Day to Save lives"

Sentence2 = 'Dance it out'
<class 'str'>
<class 'str'>

4.2. List (list)

The list is the sequence of one or more elements in order. Out of multiple ways to initialize a list the most common one is to use square brackets [].

Elements in a list can be accessed like an array using the index value. Example: nameOfList[index]

Lists are mutable and allow duplicate values.

List1 = [2,4,5,2]

List2 = list()

List3 = ["Hello",5,-7]
print(List3[1]) #If we want to access the second element
<class 'list'>
<class 'list'>
<class 'list'>

4.3. Tuples (tuple)

The tuples are the ordered collection of different data types or similar ones. Tuples are similar to a list, except that tuples are immutable which means we can’t change their values once declared.

We will use () to initialize the tuple. Using round brackets is the most common way to initialize a tuple.

Tuples are faster than List as it is immutable and we can’t change it later on.

E.g. (1,”Hello”,-5,1),(“Name”,) , ()

Invalid : (“Name”) –> compiler will consider it as a String so to make it as tuple use comma at the end of the element like this (“Name”,)

Tuple1 = (1,"Hello",-5,1)

Tuple2 = ()

Tuple3 = ("Name",)
<class 'tuple'>
<class 'tuple'>
<class 'tuple'>

” Every sequence is a collection but not every collection is a sequence. “

5. Sets (set)

Sets are an unordered collection of different or similar data types. Sets contain unique values that mean it does not allow duplicate values.

We will use {} to initialize the set in the following example.

Remember: We can put the duplicate value in the set, but it will identify that and will keep only one copy of that element.

Sets are mutable and unordered that means it does not have index values.

Set1 = {5,"MerDer" ,7.0 }

Set2 = {"I ", "am" , "your" , "person"}

Set2 = frozenset(Set2)
<class 'set'>
<class 'set'>
<class 'frozenset'>

Remember : An empty set {} is a dictionary in python.

As we know sets are mutable but using frozenset() we can make it immutable.

6. Dictionaries (dict)

Dictionaries are the unordered collection of key-value pairs. Dictionaries are mutable and do not allow duplicates. Dictionary is generally used to handle a large amount of data where the access pattern for the data is based on keys.

In the dictionary, we can store different data types as well.

Remember : we can’t have the same key but can have same value.

Dictionary1 = {1:"MerDer" , 2:"MerDer" }

Dictionary2 = {}

Dictionary3 = {"Derek" : "It's a beautiful day to save lives" , 143:"Meredith"}
<class 'dict'>
<class 'dict'>
<class 'dict'>

7. Booleans (bool)

Booleans are represented by two values : True , False

a = True

b = False
<class 'bool'>
<class 'bool'>

8. Conclusion

In this, article we have discussed

  • What are mutable and immutable data types in Python?
  • A brief overview of different data types such as Numbers, Sequence type, Set, Dictionary, and Boolean along with their examples.

Helpful Links

Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.

Also for examples in Python and practice please refer to Python Examples.

Complete code samples are present on Github project.

Recommended Books

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x