Type Conversion in Python refers to changing the data type of a variable from one type to another.
Python supports both implicit (automatic) and explicit (manual) type conversions.
πΉ Types of Type Conversion
| Type | Description |
|---|---|
| Implicit Type Conversion | Automatically performed by Python when it converts one data type to another (also called Type Casting). |
| Explicit Type Conversion | Done manually by the programmer using built-in type conversion functions. |
π§© 1. Implicit Type Conversion (Automatic)
Python automatically converts smaller data types to larger data types to prevent data loss.
x = 10 # int
y = 2.5 # float
z = x + y # int + float = float (automatic conversion)
print(z)
print(type(z))Output:
12.5
<class 'float'>Explanation:
Here, Python automatically converted the integer 10 into a float before performing the addition.
π§© 2. Explicit Type Conversion (Manual)
Explicit conversion is done using built-in Python functions like int(), float(), str(), etc.
πΉ Common Type Conversion Functions
| Function | Description | Example | Output |
|---|---|---|---|
int(x) | Converts x to an integer. | int(4.7) | 4 |
float(x) | Converts x to a float. | float(5) | 5.0 |
complex(x, y) | Converts to a complex number. | complex(3, 2) | (3+2j) |
str(x) | Converts to a string. | str(25) | '25' |
tuple(x) | Converts to a tuple. | tuple([1, 2, 3]) | (1, 2, 3) |
list(x) | Converts to a list. | list((1, 2, 3)) | [1, 2, 3] |
set(x) | Converts to a set (removes duplicates). | set([1, 2, 2, 3]) | {1, 2, 3} |
dict(x) | Converts to a dictionary (from key-value pairs). | dict([(1, 'a'), (2, 'b')]) | {1: 'a', 2: 'b'} |
ord(x) | Converts a character into its Unicode integer. | ord('A') | 65 |
chr(x) | Converts integer to a character. | chr(97) | 'a' |
hex(x) | Converts integer to hexadecimal string. | hex(255) | '0xff' |
oct(x) | Converts integer to octal string. | oct(10) | '0o12' |
bin(x) | Converts integer to binary string. | bin(5) | '0b101' |
bool(x) | Converts a value to True or False. | bool(0), bool(5) | False, True |
π§© Example 1: Converting Numbers
a = 10
b = 3.6
c = "20"
print(float(a)) # int β float
print(int(b)) # float β int
print(int(c)) # str β intOutput:
10.0
3
20π§© Example 2: Converting Characters
print(ord('A')) # Character to Unicode
print(chr(97)) # Unicode to CharacterOutput:
65
aπ§© Example 3: Converting Between Collections
list_data = [1, 2, 3, 3]
tuple_data = tuple(list_data)
set_data = set(list_data)
dict_data = dict([(1, 'a'), (2, 'b')])
print(tuple_data)
print(set_data)
print(dict_data)Output:
(1, 2, 3, 3)
{1, 2, 3}
{1: 'a', 2: 'b'}π§© Example 4: Binary, Octal, and Hexadecimal Conversions
num = 25
print(bin(num)) # Binary
print(oct(num)) # Octal
print(hex(num)) # HexadecimalOutput:
0b11001
0o31
0x19π§© Example 5: Using bool() Function
print(bool(0))
print(bool(15))
print(bool(""))
print(bool("Python"))Output:
False
True
False
TrueπΉ In Summary
- Implicit Conversion β Done automatically by Python.
- Explicit Conversion β Done manually using conversion functions.
- Common conversion functions include:
int(),float(),str(),tuple(),list(),set(),dict(),ord(),chr(),hex(),oct(),bin(), andbool(). - These conversions make Python flexible and type-safe when handling different data types.
β In short:
Type Conversion helps in changing variable types safely and efficiently to match operation requirements.
