Type Casting

In Python, you can explicitly set (force) a specific data type for a variable using type casting. This is useful when working with user input, data conversion, calculations, or formatting output.
Python provides built-in functions to convert one data type into another.

What Is Type Casting?

Type casting means converting one data type into another using built-in functions.
Common casting functions:
  • int()
  • float()
  • str()
  • bool()
  • list()
  • tuple()
  • set()
  • dict()
Definition

Type casting allows you to control the data type of a variable explicitly.


Setting Integer Type (int)

Used to convert values into integers.
python
x = int(10)
print(x)
python
y = int(9.8)
print(y)
python
z = int("100")
print(z)
Note

Strings must contain only numbers to convert to int.


Setting Float Type (float)

Used to convert values into floating-point numbers.
python
x = float(10)
print(x)
python
y = float("3.14")
print(y)
python
z = float(5)
print(z)

Setting String Type (str)

Used to convert values into strings.
python
x = str(10)
print(x)
python
y = str(3.14)
print(y)
python
z = str(True)
print(z)

Setting Boolean Type (bool)

The bool() function converts values to True or False.
python
print(bool(1))
python
print(bool(0))
python
print(bool(""))
python
print(bool("Python"))
Rule
  • 0, None, empty string → False
  • Everything else → True

Setting List Type (list)

Used to convert iterable objects into a list.
python
x = list((1, 2, 3))
print(x)
python
y = list("Python")
print(y)
python
z = list(range(5))
print(z)

Setting Tuple Type (tuple)

Used to convert iterable objects into a tuple.
python
x = tuple([1, 2, 3])
print(x)
python
y = tuple("Python")
print(y)
python
z = tuple(range(3))
print(z)

Setting Set Type (set)

Used to convert iterable objects into a set (unique values only).
python
x = set([1, 2, 2, 3])
print(x)
python
y = set("Python")
print(y)
python
z = set((10, 20, 20, 30))
print(z)

Setting Dictionary Type (dict)

Used to create dictionaries from key-value pairs.
python
x = dict(name="Python", version=3)
print(x)
python
y = dict([(1, "one"), (2, "two")])
print(y)
python
z = dict(zip(["a", "b"], [1, 2]))
print(z)

Setting Complex Type (complex)

Used to create complex numbers.
python
x = complex(3)
print(x)
python
y = complex(2, 5)
print(y)
python
z = complex("3+4j")
print(z)

Casting User Input (Very Important)

User input is always treated as a string.
python
age = int(input("Enter age: "))
print(age)
python
price = float(input("Enter price: "))
print(price)
python
name = str(input("Enter name: "))
print(name)

Common Casting Errors

Invalid Conversion

python
# int("Python")  # Error

Safe Conversion Example

python
num = "10"
result = int(num)
print(result)
Caution

Always validate input before casting to avoid runtime errors.


Why Setting Data Types Is Important

Setting specific data types helps to:
  • Avoid runtime errors
  • Perform correct calculations
  • Handle user input properly
  • Write predictable and clean code
Key Takeaway

Type casting gives you full control over how data is stored and processed.


Summary

  • Python automatically assigns data types
  • Type casting sets a specific data type manually
  • Use int(), float(), str(), bool() commonly
  • Iterables can be converted to list, tuple, set
  • User input always needs casting

Exercise

  • Convert a string number into integer
  • Convert an integer into float
  • Convert a word into a list
  • Check boolean value of empty and non-empty strings