PYTHON DATA TYPES (PYTHON FUNDAMENTALS)

Created with Sketch.

DATA TYPES

 

  •  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.

 


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

Note: Python contains several inbuilt functions
1) type()
to check the type of variable
2) id()
to get address of object

3) print()
to print the value
In Python everything is an Object.

1) int Data Type:


We can use int data type to represent whole numbers (integral values)
Eg: a = 10
type(a) #int


Note:

  •  In Python2 we have long data type to represent very large integral values.
  •  But in Python3 there is no long type explicitly and we can represent long values also by
    using int type only.


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

Note: Being a programmer we can specify literal values in decimal, binary, octal and hexa
decimal forms. But PVM will always provide values only in decimal form.

  •  a=10
  •  b=0o10
  •  c=0X10
  •  d=0B10
  •  print(a)10
  •  print(b)8
  •  print(c)16
  •  print(d)2


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)
2) ‘0b1111’
3) >>> bin(0o11)
4) ‘0b1001’
5) >>> bin(0X10)
6) ‘0b10000’

2)oct():


We can use oct() to convert from any base to octal
1) >>> oct(10)
2) ‘0o12’
3) >>> oct(0B1111)
4) ‘0o17’
5) >>> oct(0X123)
6) ‘0o443’


3)hex():


We can use hex() to convert from any base to hexa decimal
1) >>> hex(100)
2) ‘0x64’
3) >>> hex(0B111111)
4) ‘0x3f’
5) >>> hex(0o12345)
6) ‘0x14e5’


2) Float Data Type:

  •  We can use float data type to represent floating point values (decimal values)
    Eg: f = 1.234
    type(f) float
  •  We can also represent floating point values by using exponential form
    (Scientific Notation)
    Eg: f = 1.2e3  instead of ‘e’ we can use ‘E’
    print(f) 1200.0

  •  The main advantage of exponential form is we can represent big values in less
    memory.
    ***Note:
    We can represent int values in decimal, binary, octal and hexa decimal forms. But we
    can represent float values only by using decimal form.
 >>> f=0B11.01 
 File "<stdin>", line 1 
 f=0B11.01 
 ^ 
 SyntaxError: invalid syntax 
 
 >>> f=0o123.456 
 SyntaxError: invalid syntax 
 
 >>> f=0X123.456 
 SyntaxError: invalid syntax 

3) Complex Data Type:



  •  A complex number is of the form

  •  ‘a’ and ‘b’ contain Intergers 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.
    1) >>> a=0B11+5j
    2) >>> a
    3) (3+5j)
    4) >>> a=3+0B11j
    5) SyntaxError: invalid syntax
  •  Even we can perform operations on complex type values.
    1) >>> a=10+1.5j
    2) >>> b=20+2.5j
    3) >>> c=a+b
    4) >>> print(c)
    5) (30+4j)

    6) >>> type(c)

    7) <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 –> 10.5
c.imag –> 3.6
We can use complex types generally in scientific Applications and electrical engineering
Applications.

4) bool Data Type:

 

  •  We can use this data type to represent boolean values.
  •  The only allowed values for this data type are:
  •  True and False
  •  Internally Python represents True as 1 and False as 0
    b = True
    type(b) –> bool
    Eg:
    a = 10
    b = 20
    c = a<b
    print(c)  –> True
    True+True –> 2
    TrueFalse –> 1

Leave a Reply

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