Type Casting


Type Casting

Some times we may not be satisfied with Python determined datatype so we can explicitly type cast our data to some other type.

                                int(object) -------- float,str(number),boolean -> int
                                float(object) ------ int,str(number),boolean -> float
                                complex(object) -- int,float,str(number),boolean -> complex
                                str(object) ---------  any type to string
                                bool(object) - Not None and 1 -- True
                                                0 and None -- False

Python Number Conversion

By default every non decimal number in python is always treated as integer only.


                                       Integer number system  -----  Base 10 (0-9)
                                       Binary number system ------ Base 2 (0,1)
                                       Octal number system -------- Base 8 (0-7)
                                       Hexa Decimal number system ---- Base 16 (0-9, A,B,C,D,E,F)


Binary :-
   
      0b0101010111011 -- Valid
      0B01234  ----- Invalid

Octal :-
   
      0o12341234561123412351 --- Valid
      0O0123647812364671 -------- Invalid

Hexa Decimal :-
    
      0X2318467924619234ABCDEF ---- Invalid
      0x1234123456Abcdef ------------- Valid

Conversion Functions

             int(number)
             bin(number)
             oct(number)
             hex(number)


Comments