Type conversion (also called type casting) is the process of converting one data type into another.
Python supports explicit type conversion using built-in functions like:
int()float()str()bool()list()tuple()set()dict()complex()
x = "10"
y = int(x) # string → intType conversion is commonly used when:
- Taking user input
- Parsing data
- Performing calculations
- Cleaning or transforming data
| Rule | Explanation |
|---|---|
| Explicit | Python does NOT auto-convert between most types |
| Data must be compatible | Invalid formats raise errors |
| Some data is lost | Example: int(3.9) → 3 |
| Containers behave differently | Dict → list gives keys only |
float_val = 3.14
int_val = int(float_val)
print(int_val)Output:
3
Decimal part is discarded, not rounded.
str_val = "42"
int_val = int(str_val)
print(int_val)Output:
42
Invalid string:
# int("abc")Error:
ValueError: invalid literal for int()
print(int(True))
print(int(False))Output:
1
0
int_val = 10
float_val = float(int_val)
print(float_val)Output:
10.0
str_val = "3.14"
float_val = float(str_val)
print(float_val)Output:
3.14
Invalid string:
# float("abc")Error:
ValueError: could not convert string to float
print(float(True))
print(float(False))Output:
1.0
0.0
print(str(123))
print(str(4.56))
print(str(True))Output:
123
4.56
True
list_val = [1, 2, 3]
print(str(list_val))Output:
[1, 2, 3]
This does not join elements — it creates a textual representation.
| Value | Result |
|---|---|
0, 0.0 |
False |
Empty string "" |
False |
Empty list [] |
False |
Empty tuple () |
False |
Empty dict {} |
False |
| Everything else | True |
print(bool(0))
print(bool(10))
print(bool(""))
print(bool("hello"))
print(bool([]))
print(bool([1, 2, 3]))Output:
False
True
False
True
False
True
str_val = "Hello"
list_val = list(str_val)
print(list_val)Output:
['H', 'e', 'l', 'l', 'o']
tuple_val = (1, 2, 3)
print(list(tuple_val))Output:
[1, 2, 3]
set_val = {4, 5, 6}
print(list(set_val))Output (order may vary):
[4, 5, 6]
dict_val = {'a': 1, 'b': 2}
print(list(dict_val))Output:
['a', 'b']
str_val = "Python"
print(tuple(str_val))Output:
('P', 'y', 't', 'h', 'o', 'n')
print(tuple([7, 8, 9]))
print(tuple({10, 11, 12}))
print(tuple({'x': 1, 'y': 2}))Output (order may vary):
(7, 8, 9)
(10, 11, 12)
('x', 'y')
str_val = "banana"
print(set(str_val))Output:
{'b', 'a', 'n'}
print(set([1, 2, 2, 3]))
print(set((4, 5, 5, 6)))
print(set({'apple': 1, 'orange': 2}))Output:
{1, 2, 3}
{4, 5, 6}
{'apple', 'orange'}
list_of_tuples = [('a', 1), ('b', 2)]
print(dict(list_of_tuples))Output:
{'a': 1, 'b': 2}
list_of_lists = [['x', 10], ['y', 20]]
print(dict(list_of_lists))Output:
{'x': 10, 'y': 20}
Invalid conversion:
# dict([1, 2, 3])Error:
ValueError: dictionary update sequence element #0 has length 1; 2 is required
print(complex(3))
print(complex(2.5))Output:
(3+0j)
(2.5+0j)
real = 4
imaginary = 5
print(complex(real, imaginary))Output:
(4+5j)
| Error | Cause | Example |
|---|---|---|
ValueError |
Invalid numeric string | int("abc") |
TypeError |
Unsupported conversion | dict([1,2,3]) |
| Data Loss | Truncation | int(3.9) → 3 |
| Order Loss | Set conversions | set([3,1,2]) |
| Conversion | Notes |
|---|---|
int() |
Truncates floats |
float() |
Supports numeric strings |
str() |
Converts anything to text |
bool() |
Uses truthy/falsy rules |
list() |
Breaks iterable into elements |
tuple() |
Immutable version of list |
set() |
Removes duplicates |
dict() |
Needs key-value pairs |
complex() |
Used for complex numbers |
- Convert user input
"25"into an integer and add 10. - Convert
[1, 2, 2, 3]into a set, then back into a list. - Convert
"Python"into a tuple and reverse it. - Try converting
"3.14"toint— observe the error. - Create a dictionary from
[("a",1), ("b",2)]and access value"b".