🔄 Type Casting

Last Updated: 13th August 2025


  • Type Casting means converting a value from one data type to another.
  • We use Python’s built-in functions to perform casting.
  • Here, we focus only on int(), float(), complex(), bool(), and str().

Hinglish Tip 🗣: Type casting ka matlab hai ek data type ka value dusre data type me badalna.


Converting to Integer → int()

  • Removes decimal part when converting from float.
  • True1, False0.
print(int(3.99))     # 3
print(int(10.0))     # 10
print(int(True))     # 1
print(int(False))    # 0

Converting to Float → float()

  • Adds .0 when converting from int.
  • True1.0, False0.0.
print(float(5))      # 5.0
print(float(2 + 0j)) # 2.0
print(float(True))   # 1.0
print(float(False))  # 0.0

Converting to Complex → complex()

  • Creates a complex number (real + imaginary j).
  • Default imaginary part is 0 if not provided.
print(complex(3))         # (3+0j)
print(complex(3.5))       # (3.5+0j)
print(complex(True))      # (1+0j)
print(complex(2, 5))      # (2+5j) → two arguments: real, imaginary

Converting to String → str()

  • Returns a string representation of the object.
print(str(5))      # '5'
print(str(2 + 0j)) # '(2+0j)'
print(str(True))   # 'True'
print(str(False))  # 'False'

Converting to Boolean → bool()

  • Returns True or False based on the value of the object.
  • Zero values → False
  • Non-zero values → True
print(bool(0))       # False
print(bool(5))       # True
print(bool(0.0))     # False
print(bool(2 + 0j))  # True

💡 Quick Practice

  1. Convert 9.8 into an integer.
  2. Convert 7 into a float.
  3. Make 4 into a complex number with imaginary part 3.
  4. Convert 0 into a boolean.
  5. Convert True into a float.
  6. Exercise