Data Types in Python Programs

 

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.

Video Tutorial

Python contains the following inbuilt data types

  1. int
  2. float
  3. complex
  4. bool
  5. Str
  6. bytes
  7. bytearray
  8. range
  9. list
  10. tuple
  11. set
  12. frozenset
  13. dict
  14. None

In Python, everything is an object. Python contains several inbuilt functions to know the type, object ID and print object.

Data Types in Python Programs

int data type

We can use int data type to represent whole numbers (integral values).

Example:
a=10
type(a) #int

Note:

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

  1. Decimal form
  2. Binary form
  3. Octal form
  4. 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
a =0B123
a=b111

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
a=0o786

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.
Example:
a =0XFACE
a=0XBeef
a =0XBeer

Base Conversions

Python provides the following in-built functions for base conversions.

bin():

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'

2. oct()

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'

3. hex():

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'

summary :

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.

Leave a Comment

Your email address will not be published. Required fields are marked *