#6 Datatypes in Python



Datatypes in Python





Mutable and Immutable Object’s

Mutable Object : The value of mutable object can be modified in place after  it’s creation.
                         ex: list, dict, set, byte array, user defined classes.

Immutable Object: The value of immutable object cannot be changed.

                         ex: int, float, long, complex, string, tuple, bool.

“int” Data Type:

The python implementation first load an array of integer between -5 to 256.
Hence variable referring to an integer within the range would be pointing to the same object that already exists in memory. 

1) a = 256
    b  =  256  
         a is b ?

2) a = 257
  b = 257
         a is b ? 

3) z =  3 + True
  print(z)
  print(type(z))

“float” Data Type:
This are immutable data types.
Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).

1) x = 4.5
    y = 2.5
    z = x + y -2.5
2)  x = 10
      y = 5
      z = x + y - 5

“string” Data Type:

Sequence of collection of character.
The python string can be represented using:
1) Single Quote (‘ ’)
2) Double Quote (“ ”)
3) Triple Quote (”” ’’’)

Exception with immutable object:

1) a = “Python is Cool !”
    b =  “Python is Cool !”

2) a = “python”
    b = “python”

This is a result of string interning, which allows 2 variable to refer to the same string object.
One can forcibly intern string by calling sys.intern() function.

“bool” Data Type:

True Values:
  • Any string is True, except empty strings.
  • Any number is True, except 0.
  • Any list, tuple, set, and dictionary are True, except empty ones

False Values:
  • In fact, there are not many values that evaluates to False, except empty values, such as (), [], {}, "", the number 0, and the value None. And of course the value False evaluates to False.

“complex” Data Type:

Complex numbers are written in the form, x+yj, where x is real part and j is the imaginary part.
Complex number by using complex() function.
Previous
Next Post »