Python Data Type

Data Type represents the type of data present inside a variable.In Python we are not required to specify the type explicitly. Based on value provided, the type will be assigned automatically. Hence Python is dynamically Typed Language.The interpreter implicitly binds the value with its type.Python has the following data types built-in by default, in these categories:

Numeric Types:

intfloatcomplex

Text Type:

str

Sequence Types:

listtuplerange

Mapping Type:

dict

Set Types:

setfrozenset

Boolean Type:

bool

Binary Types:

bytesbytearraymemoryview

None Type:

None

Numeric Data Type:

Ø  Integers, floating point numbers and complex numbers fall under Python numbers category.  It stores numeric values. We can use the type ( ) function to know which class a variable or a value belongs to.They are defined as:

o   int

o   float

o   complex

a] Int Data Type:

Ø  Integers can be of any length; it is only limited by the memory available.We can use int data type to represent whole numbers (integral values)         

For e.g.

x = 2

     print(type(x))

            Output

         <class 'int'>

Ø  We can represent int values in the following ways:

1) Decimal form

2) Binary form

3) Octal form

4) Hexa-decimal form

 

I) Decimal Form (Base-10):

Ø  It is the default number system in Python.The allowed digits are: 0 to 9

 Eg: a =10

II) Binary Form (Base-2):

Ø   The allowed digits are: 0 & 1 . Literal value should be prefixed with 0b or 0B

 Eg:

        a = 0B1111

        a = 0B123

        a = b111

 

III) Octal Form (Base-8):

Ø  The allowed digits are: 0 to 7 .Literal value should be prefixed with 0o or 0O.

Eg:

        a = 0o123

        a = 0o786

 

IV) Hexa Decimal Form (Base-16):

Ø  The allowed digits are: 0 to 9, a-f (both lower and upper cases are allowed).Literal value should be prefixed with 0x or 0X

Eg:

    a = 0XFACE

    a = 0XBeef

    a = 0XBeer

Base Conversions

Ø  Python provide the following in-built functions for base conversions

1) bin(): We can use bin() to convert from any base to binary

1) >>> bin(15)

 '0b1111'

2) >>> bin(0o11)

 '0b1001'

3) >>> bin(0X10)

 '0b10000'

2)oct( ): We can use oct() to convert from any base to octal

1) >>> oct(10)

 '0o12'

2) >>> oct(0B1111)

     '0o17'

3) >>> oct(0X123)

'0o443'

3)hex( ): We can use hex() to convert from any base to hexa decimal

1) >>> hex(100)

'0x64'

2) >>> hex(0B111111)

'0x3f'

3) >>> hex(0o12345)

'0x14e5'

 b] Float Data Type:

Ø  We can use float data type to represent floating point values (decimal values). Float, or "floating point number" is a number, positive or negative, containing one or more decimals. floating-point number is accurate up to 15 decimal places.

Eg: >>> f = 1.234

    >>> type(f)

        float

We can also represent floating point values by using exponential form (Scientific Notation). The main advantage of exponential form is we can represent big values in less memory. We can represent int values in decimal, binary, octal and hexa decimal forms. But we can represent float values only by using decimal form.

Eg: f = 1.2e3 à instead of 'e' we can use 'E'

    >>> print(f)

        1200.0

Complex Data Type:

Ø   A complex number is of the form



‘a’ and ‘b’ contain Integers OR Floating-Point Values.

Eg:

>>> 3 + 5j

>>> 10 + 5.5j

>>> 0.5 + 0.1j

Ø  In the real part if we use int value then we can specify that either by decimal, octal, binary or hexa decimal form. But imaginary part should be specified only by using decimal form.

>>> a=0B11+5j

>>> a

(3+5j)

>>> a=3+0B11j

SyntaxError: invalid syntax

 

Ø                  Even we can perform operations on complex type values.

>>> a=10+1.5j

>>> b=20+2.5j 

>>> c=a+b

>>> print(c)

    (30+4j)

>>> type(c)

    <class ‘complex’>

 

Ø  Note: Complex data type has some inbuilt attributes to retrieve the real part and imaginary part

>>> c = 10.5+3.6j

>>> c.real

>>> c.imag 

 

Ø  We can use complex type generally in scientific Applications and electrical engineering Applications.

Comments

Search related post on google