Type Conversions

In Python, type conversion refers to the process of converting a value from one data type to another. There are two main types of type conversion: implicit (automatic) and explicit (manual).

1. Implicit Type Conversion (Automatic)

Implicit type conversion, also known as automatic type conversion, is performed by the Python interpreter without the programmer's explicit request. Occurs when an operation between two variables of different data types occurs, and Python automatically converts one of the operands to the data type of the other operand.

Example:

# Implicit conversion from int to float
result = 5 + 2.0
print(result)  # Output: 7.0

2. Explicit Type Conversion (Manual)

Explicit type conversion, also known as manual type conversion or casting, is performed by the programmer using specific functions to change the data type of a variable. Python provides built-in functions for explicit type conversion, such as int(), float(), str(), etc.

Example:

# Explicit conversion from float to int
value = 10.5
result = int(value)
print(result)  # Output: 10

Built-in functions for explicit type conversion:

Powered by Forestry.md