Python data types

Python Data Types

Python is object-oriented programming. Python data types are also a part of object programming as each value in python is considered as a data type. Data types can be classes or variables as well. They can be instances of the classes. Variables can store different types of data and different things can do different work. They can identify the types of operations which are to be performed on specific data.

The main role of using data types in python is the classification and categorization of items. To determine the type of data in python, the type() function is used.

Python Data Types List

There are a number of data types in python. The standard classification of data types is done in the following manner:

  • Numeric
  • Sequence
  • Boolean
  • Set
  • Dictionary

Number Data Type

Numeric data type in python represents numeric values in python. They can be integer values (int), floating(float) numbers or complex numbers.

  • Integer Values: It can be represented under β€˜int’ class. It can have all the numbers including positive and negative numbers. There is no limit for an integer value for how long it can be.
  • Floating Values: It can be represented under the β€˜float’ class. It can contain all the decimal values for both positive as well as negative values.
  • Complex Numbers: It is represented as β€˜complex’ in python. Its representation is done in a manner 2+5i where i is an imaginary value and 2 is the real part.

Below is a python code for the representation of numeric data and their types:

x = 10
print("Type of a: ", type(x)) 
y = 10.0
print("\nType of b: ", type(y)) 
z = 2 + 3j
print("\nType of c: ", type(z))

The output of the above python code will be:

('Type of a: ', <type 'int'>)
('\nType of b: ', <type 'float'>)
('\nType of c: ', <type 'complex'>)

Sequence Data Type

A sequence is a form of ordered collection including both similar as well as different types of data. It can have multiple values in an organized way. There are 3 types of sequences in python which are as follows:

1.) String in Python

Strings in python represent unicode characters. It is represented by the β€˜str’ class. It can be a collection of characters either single or multiple in a secluded manner.

Python Compare Strings

There is also a way in python for comparing two strings. It can be done using operators. The characters present in both the strings are made to compare with each other one by one as each character has its own Unicode value. The char which has smaller Unicode value out of the two is considered to be small.

Python Compare Strings Example

car1 = 'Honda'
print(car1 == 'Honda')
print(car1 != 'Honda')
print(car1 < 'Honda')
print(car1 > 'Honda')
print(car1 <= 'Honda')
print(car1 >= 'Honda')

The output of this python program will be:


2.) List in Python

It is a form of ordered sequence and is considered as one of the most flexible data types. There is a condition when the user works with a list is that all the values in the list not necessarily need to be of similar type. But they only work when represented with square brackets []. For example [1, 20.4 , 3].

Below is a python program to understand how lists work in python:

x = [1,2,3.3,4,6,9,23,4]

# a[2] = 4
print("x[2] = ", x[2])

# a[0:3] = [5, 10, 15]
print("x[0:3] = ", x[0:3])

# a[5:] = [20, 35, 40]
print("x[5:] = ", x[5:])

The output of this python program would be:

('x[2] = ', 3.3)
('x[0:3] = ', [1, 2, 3.3])
('x[5:] = ', [9, 23, 4])
Python Remove Item from List

Let us now try to understand how the user can remove an item from the list. If the user wishes to remove the whole list, there is a function remove(). Otherwise, there are other multiple functions are well. Have a look below to understand:

  • Remove all the items from the list: clear()
  • If the user wants to remove an item by index: pop()
  • To remove an items by slice : del

Below is a program to understand how the user can remove elements from the list:

animals = ['dog', 'cat', 'rat', 'pig']
print('Animals list: ', animals)
print('Updated animals list: ', animals)

The output of this python program will be:

('Animals list: ', ['dog', 'cat', 'rat', 'pig'])
('Updated animals list: ', ['dog', 'cat', 'pig'])

3.) Python Tuples

Just like the list in python, tuples are a form of ordered sequence. The only difference between tuples and lists in python is that once the tuples are created, it is not possible to change them. They are immutable whereas lists are mutable forms of the data type.

For more information on tuples, refer to the whole section of python tuples here.

Python Boolean

Boolean values return true for the values that are true and false for the values which are not true. The user can represent it by class β€˜bool’.

Python Sets

In python, sets are a form of unordered data types. We can represent it as { }. We can term it as the representation of a collection of items that are not in an ordered fashion.

Sets are created using the set() function which is built-in in python. The items are put in curly brackets however; the items in a set need not be of similar type. Hence, multiple mixed values can be passed to the set data type where the values are separated by commas.

Below is a python program to understand the use of sets:

x = {23, 12,34,10,4}
print("x = ", x)

The output of the python program will be:

('x = ', set([4, 34, 12, 10, 23]))
<type 'set'>

Python Dictionary

It is considered as the unordered collection of data types such as a map. The item is represented in the form of key: value pair. Every value in this data type is separated by a colon (:) however, each key value is separated by a comma.

A dictionary in Python is by using { } curly brackets. The good thing about using a dictionary in python is that it allows the use of duplicate values.  The user cannot repeat the key values and cannot change them also, but the values can be of any data type. a user can also create an empty dictionary if he wants to.

Below is a python program to illustrate the use of dictionary data type:

d = {}  
print("Empty Dictionary: ")  
d = {1: 'Welcome', 2: 'To', 3: 'Site'} 
print("\nDictionary with Integer Keys: ")  
d = {'Welcome': 'Dear', 1: [1, 2, 3, 4]}  
print("\nDictionary with mixed Keys: ")  

The output of the above python program will be:

Empty Dictionary: 

Dictionary with Integer Keys: 
{1: 'Welcome', 2: 'To', 3: 'Site'}

Dictionary with mixed Keys: 
{1: [1, 2, 3, 4], 'Welcome': 'Dear'}

There are binary types of data in python as well. It includes bytes, byte array where buffer protocol is the memory view. The memory can be accessed with the help of other data types which are of binary form. It doesn’t copy the actual data but returns the actual form of the object type.

For example, if you want to use bytearray, it will be perfomed in the following manner:

x = bytearray(6)

The output of the above python code using binary data types will be:

<type 'bytearray'>

Leave a comment

Your email address will not be published. Required fields are marked *

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Python Online Quiz

Level up your coding skills with our interactive programming quiz!