Skip to content

Latest commit

 

History

History
38 lines (32 loc) · 1.42 KB

File metadata and controls

38 lines (32 loc) · 1.42 KB

TypeCasting

The process of conversion of one datatype to another.

Example:
  num = int(input("Enter the Number: ")) # conversion of str to int
  # By default the input function returns a string value & therefore in above line we have wrapped out input() function inside of int() function.
Before moving ahead let's just take a look on some basic datatype:
  b = 0b100 # binary value [ 0b100 - 4 ]
  o = 0o100 # octal value [ 0o100 - 64 ]
  h = 0x100 # Hex value [ 0x100 - 256 ]
  e = 5.5e2 # exponential value [5.5e2 - 550.0]
  c = 5+10j complex value
  # a.real - 5.0
  # a.imag - 10.0
  # a.conjugate() - 5-10j
Now lets do some typecasting:
  # conversion of int to binary
  binary_number = bin(175) # 0b10101111
  # conversion of binary to int
  integer_number = int(binary_number, 2) # 175
  # conversion of int to octal
  octal_number = oct(100) # 0o144
  # conversion of octal to int
  integer_number = int(octal_number, 8) # 100
  # conversion of int to hex
  hex_number = hex(50) # 0x32
  # conversion of hex to int
  integer_number = int(hex_number, 16) # 50
  # conversion of int to complex
  complex_number = complex(10, 12) # 10+12j
  # conversion of int to string
  string_value = str(integer_number) # '50' 
  # Note: string to int can only be converted if the string is of number type