Data Types in Python Programs
In the previous tutorial (Click Here), you have learned the concept of identifier in python. In this tutorial, we will discuss the data types in Python programs.
Data Type represents the type of data present inside a variable. In Python, we are not required to specify the type of a variable explicitly. Based on the value assigned or provided, the type will be assigned to variables automatically. Hence Python is known as Dynamically Typed Language.
Python contains the following inbuilt data types
In Python, everything is an object. Python contains several inbuilt functions to know the type, object ID and print object.
int data type
We can use int data type to represent whole numbers (integral values).
Example: a=10 type(a) #int
In Python 2 (Version 2) we have long data type to represent very large integral values. But in Python 3 (version 3) there is no long data type and we can represent long values using int type only. We can represent int values in the following ways
- Decimal form
- Binary form
- Octal form
- Hexadecimal form
1. Decimal form (base-10):
It is the default number system in Python. The allowed digits are: 0 to 9
Example: a =10
2. Binary form (Base-2):
The allowed digits are : 0 & 1
Literal value should be prefixed with 0b or 0B
Example: a = 0B1111
3. Octal Form (Base-8):
The allowed digits are : 0 to 7
The literal value should be prefixed with 0o or 0O. Example: a=0o123
4. Hexa Decimal Form (Base-16):
The allowed digits are 0 to 9, a-f (both lower and upper cases are allowed). The literal value should be prefixed with 0x or 0X.
Python provides the following in-built functions for base conversions.
We can use bin() function to convert from any base to binary.
1. bin(15) Output: '0b1111' 2. bin(0o11) Output: '0b1001' 3. bin(0X10) Output'0b10000'
We can use oct() function to convert from any base to octal form.
1. oct(10) Output: '0o12' 2. oct(0B1111) Output: '0o17' 3. oct(0X123) Output'0o443'
We can use hex() function to convert from any base to hexadecimal.
1. hex(100) Output: '0x64' 2. hex(0B111111)) Output: '0x3f' 3. hex(0X123) Output'0x14e5'
In this tutorial, you have learned the different data types in Python. The int data type is discussed in detail. in the next tutorial, you will learn data types such as float, complex, bool and str.
If you like the tutorial, share with your friends. Do like our Facebook page for regular updates.