πŸ”„ Python Type Casting β€” Converting Between Data Types

Introduction 🌟

Type casting is the process of converting one data type into another. Python makes type conversion simple and powerful using built-in functions likeint(), float(), str(), list(), and more.

Note

πŸ’‘ Type casting is essential when taking user input, doing math operations, or formatting output.

1. Why Do We Need Type Casting? πŸ€”

Python’s input() function always returns a string. If you want to perform arithmetic operations or convert data formats, you must convert (cast) the value to another type.

need_casting.py

age = input("Enter age: ")   # returns "25"
# print(age + 1)  # ❌ Error: can't add string and int
age = int(age)    # βœ”οΈ Convert to integer
print(age + 1)

2. Types of Type Casting 🎯

πŸ“Œ 1. Implicit Type Casting

Python automatically converts one type to another when safe. You don’t need to do anything manually.

implicit_casting.py

x = 5      # int
y = 3.2    # float

result = x + y  # int is converted to float
print(result)   # Output: 8.2

Note

🧠 Implicit casting usually happens only with numeric types.

πŸ“Œ 2. Explicit Type Casting

You manually convert a value from one type to another using functions like:

  • int() – convert to integer
  • float() – convert to float
  • str() – convert to string
  • bool() – convert to boolean
  • list() – convert to list
  • tuple() – convert to tuple
  • set() – convert to set

3. Converting to Integer (int) πŸ”’

to_int.py

x = "10"
y = int(x)  # "10" β†’ 10
print(y, type(y))

Note

⚠️ int() only works on valid numbers like "10", not "abc".

4. Converting to Float (float) πŸ’§

to_float.py

x = "3.14"
y = float(x)
print(y, type(y))

5. Converting to String (str) πŸ“

to_string.py

x = 100
y = str(x)
print(y, type(y))

6. Converting to Boolean (bool) πŸ”₯

bool() follows these rules:

  • 0, "", [], , None β†’ False
  • Anything else β†’ True

to_bool.py

print(bool(0))       # False
print(bool(5))       # True
print(bool(""))      # False
print(bool("hi"))    # True

7. Converting to List (list) πŸ“¦

to_list.py

x = "Python"
y = list(x)
print(y)   # ['P', 'y', 't', 'h', 'o', 'n']

8. Converting to Tuple (tuple) πŸ”’

to_tuple.py

x = ["a", "b", "c"]
y = tuple(x)
print(y)

9. Converting to Set (set) 🎯

to_set.py

x = [1, 2, 2, 3]
y = set(x)
print(y)  # {1, 2, 3}

Note

🧹 Casting to set removes duplicates automatically.

10. Real-World Example πŸ§ͺ

real_world.py

name = input("Enter your name: ")
age = int(input("Enter your age: "))

print(f"Hello {name}, next year you will be {age + 1}!")

11. Common Errors ⚠️

  • Trying to cast letters into numbers (e.g., int("abc"))
  • Forgetting that input() gives a string
  • Converting incorrectly formatted numbers like "10.5" β†’ int ❌

Note

πŸ’‘ Always validate or use try/except when casting user input.

Conclusion πŸŽ‰

>>β€œType casting transforms data into the form your program needs β€” mastering it gives you true control.” πŸ”₯

You're now ready to work with inputs, calculations, and complex data transformations. Want the next topic? Try Operators, Expressions, Conditional Statements, Loops, or Functions. Just ask! 😊