Data Types In Python
ganesh kavhar
Python | PySpark | Databricks | ETL | SQL | Unix | Big Data | Data warehouse
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 datatypes. If we work on any project, this is significant to understand what datatypes are, because if we perform any operation using variables and their datatypes in an improper way, then we get errors.
Let's start!
In Python, datatypes 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 argument passed as a parameter.
Example
- lst=[10,25,35,45,50,60]
- print(type(lst))
Output
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
- a=5
- print(type(a))
Output
float
This datatype contains decimal values or point values.
Example
- a=5.2
- print(type(a))
Output
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
- a,b=6,8
- print(a>b)
- print(type(a>b))
Output
complex
It contains a real part + imaginary part.
Example
- a=5+4j
- print(type(a))
Output
Coversion
Python covnersion 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
- a=5
- print(float(a))
Output
float to int
- # float to int
- a=8.5
- print(int(a))
Output
float and int into complex
- a=5.2
- b=6
- print(complex(a,b))
Output
Now, if we want to know the actual value of True and False
- print(int(True))
- print(int(False))
Output
Squence
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([]).
- lst=[10,25,35,45,50,60]
- print(type(lst))
Output
Tuple
A tuple is a collection which 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
- tuple=(12,12,9,15,13,20,15)
- print(type(tuple))
Output
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({}).
- s={12,12,9,15,13,20}
- print(type(s))
Output
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.
- r=range(10)
- print(r)
- print(list(r))
Output
Even numbers between 0-10. There are many ways to do this but we use range function.
- evenNumber=list(range(2,10,2))
- print("Even No: " + str(evenNumber))
Output
Dictonary
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.
- # dictionary
- dict={'Amit':'Kumar','Abhishek':'Yadav','Apress':'Pub'}
- print(dict.keys())
Output
- Get values from keys,dict={'Math':'Algebra','Science':'Physics','Apress':'Publication'}
- print(dict['Apress'])
Output
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 datatype of a variable, using type(). I will explain the remaining (list, tuple, dictionary) in an upcoming article.