PYTHON Tutorial



TYPE CASTING IN PYTHON


🔁 Type Casting in Python

Type Casting means converting the data type of a value into another data type. Python supports both implicit and explicit type casting.

🔸 Implicit Type Casting

Python automatically converts one data type to another during operations:

x = 5
y = 2.0
z = x + y  # x is automatically converted to float
print(z)   # Output: 7.0
  

🔸 Explicit Type Casting

You can manually convert data types using built-in functions:

a = int(3.9)     # Converts float to int (Output: 3)
b = float("4")   # Converts string to float
c = str(10)      # Converts int to string
print(a, b, c)
  

📌 Common Type Casting Functions

  • int() – converts to integer
  • float() – converts to float
  • str() – converts to string
  • bool() – converts to boolean
  • list(), tuple(), set() – converts to list, tuple, set
💡 Tip: When converting strings to numbers, make sure the string contains valid numeric characters, or Python will raise an error.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review