Python Type Casting

Introduction

Type casting, also known as type conversion, is the process of converting one data type into another. Python supports both implicit and explicit type casting. Implicit type casting is performed automatically by Python, while explicit type casting is done using built-in functions.

Implicit Type Casting

Implicit type casting is automatically performed by Python when a value is converted to another data type without the need for explicit conversion by the programmer.

Example

# Implicit type casting
num_int = 123   # Integer type
num_float = 1.23  # Float type

# Addition of integer and float
num_new = num_int + num_float

print("Datatype of num_int:", type(num_int))
print("Datatype of num_float:", type(num_float))
print("Value of num_new:", num_new)
print("Datatype of num_new:", type(num_new))

Output

Datatype of num_int: <class 'int'>
Datatype of num_float: <class 'float'>
Value of num_new: 124.23
Datatype of num_new: <class 'float'>

In this example, Python automatically converts the integer num_int to a float before performing the addition, resulting in a float.

Explicit Type Casting

Explicit type casting is performed by the programmer using built-in functions to convert one data type to another.

Built-in Type Casting Functions

  1. int(): Converts a value to an integer.
  2. float(): Converts a value to a float.
  3. str(): Converts a value to a string.
  4. list(): Converts a value to a list.
  5. tuple(): Converts a value to a tuple.
  6. set(): Converts a value to a set.
  7. dict(): Converts a value to a dictionary (requires a sequence of key-value pairs).

Built-in Type Casting Examples

Converting to Integer

# Converting float to integer
num_float = 1.99
num_int = int(num_float)

print("Value of num_float:", num_float)
print("Value of num_int:", num_int)
print("Datatype of num_int:", type(num_int))

Output

Value of num_float: 1.99
Value of num_int: 1
Datatype of num_int: <class 'int'>

Converting to Float

# Converting string to float
num_str = "1.23"
num_float = float(num_str)

print("Value of num_str:", num_str)
print("Value of num_float:", num_float)
print("Datatype of num_float:", type(num_float))

Output

Value of num_str: 1.23
Value of num_float: 1.23
Datatype of num_float: <class 'float'>

Converting to String

# Converting integer to string
num_int = 123
num_str = str(num_int)

print("Value of num_int:", num_int)
print("Value of num_str:", num_str)
print("Datatype of num_str:", type(num_str))

Output

Value of num_int: 123
Value of num_str: 123
Datatype of num_str: <class 'str'>

Converting to List

# Converting string to list
str_val = "hello"
list_val = list(str_val)

print("Value of str_val:", str_val)
print("Value of list_val:", list_val)
print("Datatype of list_val:", type(list_val))

Output

Value of str_val: hello
Value of list_val: ['h', 'e', 'l', 'l', 'o']
Datatype of list_val: <class 'list'>

Converting to Tuple

# Converting list to tuple
list_val = [1, 2, 3]
tuple_val = tuple(list_val)

print("Value of list_val:", list_val)
print("Value of tuple_val:", tuple_val)
print("Datatype of tuple_val:", type(tuple_val))

Output

Value of list_val: [1, 2, 3]
Value of tuple_val: (1, 2, 3)
Datatype of tuple_val: <class 'tuple'>

Converting to Set

# Converting list to set
list_val = [1, 2, 2, 3]
set_val = set(list_val)

print("Value of list_val:", list_val)
print("Value of set_val:", set_val)
print("Datatype of set_val:", type(set_val))

Output

Value of list_val: [1, 2, 2, 3]
Value of set_val: {1, 2, 3}
Datatype of set_val: <class 'set'>

Converting to Dictionary

# Converting list of tuples to dictionary
list_of_tuples = [("key1", "value1"), ("key2", "value2")]
dict_val = dict(list_of_tuples)

print("Value of list_of_tuples:", list_of_tuples)
print("Value of dict_val:", dict_val)
print("Datatype of dict_val:", type(dict_val))

Output

Value of list_of_tuples: [('key1', 'value1'), ('key2', 'value2')]
Value of dict_val: {'key1': 'value1', 'key2': 'value2'}
Datatype of dict_val: <class 'dict'>

Conclusion

Type casting in Python can be done implicitly by the interpreter or explicitly by the programmer using built-in functions. Implicit type casting occurs automatically when combining different data types, while explicit type casting requires the use of functions such as int(), float(), str(), list(), tuple(), set(), and dict(). Understanding type casting is essential for ensuring that your data is in the correct format for various operations and for preventing type errors in your programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top