Continuing with series of Python after Identifier and reserved keywords today we’re going to look into the most important topic “Data Types in Python” I’ll be briefing about all the data types even which are not used mostly but are equally important.
In general Python has 14 Data Types. Since in the first tutorial itself I’ve mentioned we’ll be breaking each topic and deep dive to make our concept clear, Data types will be divided briefly in 4 posts so that we cover all the aspects of it.
We will also consider Data Types with respect to Python2 and Python3 so that you all will be aware which is deprecated and which is being replaced.
- int: It is used to represent integral values, In Python2 there was long but from Python3 long can also be used and represented by int.
- complex: 10+2j
Before we deep dive into individual data types, the first and the most important thing to remember is that, In Python everything is Object even methods/function and Datatypes.
In the above schematic diagram…… a is variable who holds the value 10. As we said even the data types in Python is Object Oriented we should understand the concept as per Object Oriented Programming. Now when we talk about the Object Oriented Programming, a is reference variable who points to a location in memory and that holds the value 10. I know some of the people are confused, but “patience is the key to success”
I’ll be having one separate tutorial in the same series itself regarding the Object Oriented Concepts.
Now as the background is clear let’s deep dive into data types.
1. int: Integer, the number without decimal point i.e. Integral values.
e.g. 10,20,30,10000 etc
>>> a = 10 >>> type(a) <class ‘int’> >>> print a 10
In the first line we have defined an variable of type int a, and assigned value 10. type(a) gives the class of the variable which says int. In the next line we are printing the value of the variable where we’re getting 10.
int can represent value in 4 ways only.
- Decimal form – (Base -10) [0-9]
- Binary form – (Base -2) [0&1]
- Octal form – (Base -8) [0-7]
- Hexa Decimal – (Base -16)[0-9] & [a-f] Here Python is case insensitive.
2. float: Number with decimal point, it doesn’t include double values.
e.g Salary: 234556.55, price 233.44
>>> f =123.445 >>> type(f) <class ‘float’>
Exponential calculation is allowed for floating data types.
In this tutorial we’ve seen two types of Data types in the next consecutive tutorial we will be covering rest of them.