Python Data Types & it's Examples

Hey people! Welcome to the another interesting article on Python Programming Language. If you are searching the data types of python programming langu then here you will find answers to your questions. 

Python Data Types & Types of Python Data Type

In this article, we will going to discuss about the Meaning and types of Python Data types. So, let's start the article!


Python Data Type

Data type categories or classifies the data items. Data types tells us about what operation can be performed on a particular data by the representation of kind of value. 


Types of Python Data Type 

There are five types of Python Data types which are discussed as follows;


1. Numeric

Numeric data type are those type of data type which represents the data which have numeric value. The numeric data types are further divided into three parts which are as follows;

Integers- Int class represents this value and it includes both positive and negative whole numbers. And there is no limit on integers in python.

Float- This type of value is represented by the float class. These are the real numbers which includes floating point representation. 

Complex- These are those data types whose value is represented by complex class. 

Python Numeric Example;

a = 7

print(a, "is of type", type(a))


a = 4.0

print(a, "is of type", type(a))


a = 1+4j

print(a, "is complex number?", isinstance(1+4j,complex))


Output

7 is of type <class 'int'>

2.0 is of type <class 'float'>

(1+4j) is complex number? True

2. Boolean

Boolean are those data types which are with one of the two in-built values i.e., True or False. Truthy are those Boolean objects that are equal to true and falsy are those Boolean objects that are equal to false. These are represented by the bool class.

Python Boolean Example;

#Python program to check the boolean type  

print(type(True))  

print(type(False))  

print(false)  

Output

<class 'bool'>

<class 'bool'>

NameError: name 'false' is not defined

3. Set

The collection of data types which are iterable, mutable and has no duplicate elements are known as set. We can create set by using the in-built function set().

Python Set Example;

a = {5,2,8,3,7,1,4,6}


# printing set variable

print("a = ", a)


# data type of variable a

print(type(a))

Output

a = {1, 2, 3, 4, 5, 6, 7, 8}

<class 'set'>

4. Dictionary

Dictionary are those data types which represents the collection of unordered data value which are used to store data values. Dictionary data types holds key : value pair. We can create dictionary by placing sequence elements within curly braces {}, which are separated by 'comma'.

Python Dictionary Example;

d = {1:'Deepak', 2:'Satvik', 3:'Rahul', 4:'Ritik'}     

  

# Printing dictionary  

print (d)  

  

# Accesing value using keys  

print("1st name is "+d[1])   

print("2nd name is "+ d[4])    

  

print (d.keys())    

print (d.values())    

Output

{1: 'Deepak', 2: 'Satvik', 3: 'Rahul', 4: 'Ritik'}

1st name is Deepak

2nd name is Ritik

dict_keys([1, 2, 3, 4])

dict_values(['Deepak', 'Satvik', 'Rahul', 'Ritik'])

5. Sequence

Sequence data types are those data types which represents the ordered collection of similar or different data types. Sequence data type are further classified into three types which are discussed as follows;

String- String are those data types which are arrays of byte which represents unicode characters. String is defined as a organized collection of one or more than one characters which are put in single, double and triple quote.

Python String Example;

String1 ="Welcome"

String2 ="To"

String3 ="Answersjet"

print(String1+String2+String3)

Output

 WelcomeToAnswersjet

List- List are an ordered collection of data similar to arrays declared in different language. By placing the sequence inside the square brackets[] we can create list in python.

Python list Example;

list1 = [4, "Hello", "Answersjet", 9]    

#Checking type of given list  

print(type(list1))  

  

#Printing the list1  

print (list1)  

  

# List slicing  

print (list1[3:])  

  

# List slicing  

print (list1[0:2])   

  

# List Concatenation using + operator  

print (list1 + list1)  

  

# List repetation using * operator  

print (list1 * 3)  

Output

<class 'list'>

[4, 'Hello', 'Answersjet', 9]

[9]

[4, 'Hello']

[4, 'Hello', 'Answersjet', 9, 4, 'Hello', 'Answersjet', 9]

[4, 'Hello', 'Answersjet', 9, 4, 'Hello', 'Answersjet', 9, 4, 'Hello', 'Answersjet', 9]

Tuple- Tuple is a database type which contains the ordered collection of python data objects. Tuple are immutable which means it can't be modified once it is created. By placing values in sequence separated by 'comma', with or without using parentheses creates tuple.

Python Tuple Example;

t = (10,'program', 1+6j)


# t[1] = 'program'

print("t[1] = ", t[1])


# t[0:6] = (10, 'program', (1+6j))

print("t[0:6] = ", t[0:6])


# Generates error

# Tuples are immutable

t[0] = 20

Output

t[1] =  program

t[0:6] =  (10, 'program', (1+6j))

Traceback (most recent call last):

File "<string>", line 11, in <module>

TypeError: 'tuple' object does not support item assignment

Conclusion

Above we have discussed the Python Data types. Data type categories or classifies the data items. Data types tells us about what operation can be performed on a particular data by the representation of kind of value. There are five types of Python Data types i.e., Numeric, Boolean, Set, Dictionary and Sequence.

Related Posts

Post a Comment