Python Tutorial 11 : Data Type in Python

Following Data Types are available in Python.

Primitive Data Structure

  • Integer
  • Float
  • String
  • Boolean

Non-Primitive Data Structure

  • List
  • Set
  • Tuple
  • Data Dictionary

Python Numbers

In Python, Integers,floating point numbers and complex numbers are all all under Python Numbers category. To identify the type of class we can use the “Type function.

Following is an example:

Number data type in PythonNumber data type in Python - Result

 

 

a = 3

We are assigning an integer 3 into the variable “a”. Therefore, “a” become an integer and following is the result for the first print statement:

3 is a <class ‘int’>

 

b = 3.0

We are assigning a float 3.0 into the variable “b”. Therefore, “b” become an float and following is the result for the second print statement:

3.0 is a <class ‘float’>

 

c= 1 + 2j

We are assigning a complex into the variable “c”. Therefore, “C” become an complex and following is the result for the third print statement:

(1+2j) is a <class ‘complex’>

 

Python List

List is an ordered sequence of items. It is one of the most used datatype in Python and is very flexible. All the items in a list do not need to be of the same type.

For more detail regarding to Python List, please refer to Python Tutorial 10: Basic operation of Python List

 

Python Tuple

Python Tuple is similar to Python List. However, the only difference is that tuples are immutable. Tuples once created cannot be modified.

 

Following example illustrated the difference between List and Tuple

 

Python List Vs Tuple

In this function, we are trying to create a list and a tuple with exactly the same value, then try the update the first element for them and following is the result.

Python List Vs Tuple - Result

The program actually stopped while trying the update the tuple. This is because tuple cannot be updated once it is defined.

 

Python Set

Set is an unordered collection of unique items. Set is defined by values separated by comma inside braces { }. Items in a set are not ordered.

Following example demonstrated set operation in Python

Set operation in Python

We have created two set there the first set have duplicated the element ‘A’ and try to union both two sets.

Following is the result

Set operation in Python - result

We can see that the first set only have one ‘A’ as it eliminated all duplicated elements.

And the second print statement also printed the union result of the set.

 

Python Dictionary will be discussed in later tutorial

Leave a Comment