Data Types In Python

Introduction

 
Today, in this article, we will learn about Python data types and their usage. This is a very important topic because every programming language has some specific data types. If we work on any project, this is significant to understand what data types are, because if we perform any operation using variables and their data types in an improper way, then we get errors.
 
Let's start!
 
In Python, data types are divided into 4 types. 
  • Numeric
  • Sequence
  • Dictionary
  • None 
Now, let us talk about each and every datatype mentioned above. 
 

Numeric

 
It has four types.
  • int
  • float
  • complex
  • bool
type()
 
This function always returns a class type of the argument passed as a parameter.
 
Example
  1. lst=[10,25,35,45,50,60]  
  2. print(type(lst))  
Output
 
Data Types in Python 
 
In the output, you can see that the type() method returns variable (list) datatype which is list type.
 
int
 
An Integer is a whole number that can have both zero, or positive and negative values but it cannot store decimal values.
 
Example
  1. a=5  
  2. print(type(a))  
Output
 
Data Types in Python 
 
float
 
This datatype contains decimal values or point values.
 
Example
  1. a=5.2  
  2. print(type(a))  
Output
 
Data Types in Python 
 
In the above result, the data type of variable "a" is float type. 
 
bool
 
It is a special type of datatype which returns true of false. 
 
Example
  1. a,b=6,8  
  2. print(a>b)  
  3. print(type(a>b))  
Output
 
Data Types in Python 
 
complex
 
It contains a real part + imaginary part.
 
Example
  1. a=5+4j  
  2. print(type(a))  
Output
 
Data Types in Python 
 
Conversion
 
Python conversion is different as compared to other programming languages, like in C# we use Convert.ToInt() or .ToString() etc. In Python, it is something different.
 
Int to float 
  1. a=5  
  2. print(float(a))  
Output
 
Data Types in Python 
 
float to int
  1. # float to int  
  2. a=8.5  
  3. print(int(a))  
Output
 
Data Types in Python 
 
float and int into complex
  1. a=5.2  
  2. b=6  
  3. print(complex(a,b))  
Output
 
Data Types in Python 
 
Now, if we want to know the actual value of True and False
  1. print(int(True))  
  2. print(int(False))  
Output 
 
Data Types in Python
 

Sequence

 
It is divided into five types, as given below. 
  • List
  • Tuple
  • String 
  • Set 
  • range objects
List
 
A list is a collection of items or objects of any type that can be accessed by index. This class also provides methods to search, sort, and manipulate lists. In Python, the list is created by using Square braces([]).
  1. lst=[10,25,35,45,50,60]  
  2. print(type(lst))  
Output
 
Data Types in Python 
 
Tuple
 
A tuple is a collection that is ordered and unchangeable. A tuple is similar to a list. In Python, tuples are written with round brackets. The only difference between the list and tuple is that we cannot change the elements of a tuple once it assigned whereas in a list, elements can be changed after assigned. For more details about tuples
  1. tuple=(12,12,9,15,13,20,15)  
  2. print(type(tuple))  
Output
 
Data Types in Python 
 
Set
 
Set is a collection of items in any order. A Python set is similar to the mathematics terms like union, intersection, difference and complement. In python set is created by using curly braces({}).
  1. s={12,12,9,15,13,20}   
  2. print(type(s))  
Output
 
Data Types in Python 
 
String
 
String is a collection of one or more than one character. The string type in Python is called str. For more detail see String Operation in Python
range object: It uses range().
 
The range() type returns an immutable sequence of numbers between the given start integer to the stop integer.
  1. r=range(10)  
  2. print(r)  
  3. print(list(r))  
Output 
 
Data Types in Python 
 
Even numbers between 0-10. There are many ways to do this but we use range function. 
  1. evenNumber=list(range(2,10,2))  
  2. print("Even No: " + str(evenNumber))  
Output
 
Data Types in Python 
 
Dictionary
 
Python dictionary is an unordered collection of items. A dictionary has a key: value pair. The items are separated by commas, and the whole thing is enclosed in curly braces({}). Keys are unique within a dictionary while values may not be, and values of the dictionary can be any type.
  1. # dictionary  
  2. dict={'Amit':'Kumar','Abhishek':'Yadav','Apress':'Pub'}  
  3. print(dict.keys())  
Output
 
Data Types in Python 
 
Get values from keys,
  1. dict={'Math':'Algebra','Science':'Physics','Apress':'Publication'}   
  2. print(dict['Apress'])  
Output
 
Data Types in Python 
 
None
 
When a variable is not assigned with any values. Normally, in other languages, we use null but in Python, we use none.
 

Conclusion

 
In this article, we learned about the datatypes Python has. We saw how to find the data type of a variable, using type(). I will explain the remaining (list, tuple, dictionary) in an upcoming article.


Similar Articles