Typecasting, or type conversion, is the process of converting a variable from one data type to another. Python provides a variety of built-in functions for explicit typecasting, enabling programmers to ensure data integrity and compatibility. Below is an exhaustive list of typecasting functions available in Python, along with detailed explanations and examples for each.
int( )
The int( ) function converts a value to an integer.
Example:
# String to integer
str_num = "123"
int_num = int(str_num)
print(int_num) # Output: 123
# Float to integer (truncates decimal part)
float_num = 123.45
int_num = int(float_num)
print(int_num) # Output: 123
float( )
The float( ) function converts a value to a floating-point number.
Example:
# String to float
str_num = "123.45"
float_num = float(str_num)
print(float_num) # Output: 123.45
# Integer to float
int_num = 123
float_num = float(int_num)
print(float_num) # Output: 123.0
str( )
The str( ) function converts a value to a string.
Example:
# Integer to string
int_num = 123
str_num = str(int_num)
print(str_num) # Output: '123'
# Float to string
float_num = 123.45
str_num = str(float_num)
print(str_num) # Output: '123.45'
list( )
The list( ) function converts a value to a list.
Example:
# String to list of characters
str_data = "hello"
list_data = list(str_data)
print(list_data) # Output: ['h', 'e', 'l', 'l', 'o']
# Tuple to list
tuple_data = (1, 2, 3)
list_data = list(tuple_data)
print(list_data) # Output: [1, 2, 3]
tuple( )
The tuple( ) function converts a value to a tuple.
Example:
# List to tuple
list_data = [1, 2, 3]
tuple_data = tuple(list_data)
print(tuple_data) # Output: (1, 2, 3)
# String to tuple of characters
str_data = "hello"
tuple_data = tuple(str_data)
print(tuple_data) # Output: ('h', 'e', 'l', 'l', 'o')
dict( )
The dict( ) function converts a value to a dictionary.
Example:
# List of tuples to dictionary
list_data = [('a', 1), ('b', 2), ('c', 3)]
dict_data = dict(list_data)
print(dict_data) # Output: {'a': 1, 'b': 2, 'c': 3}
# List of two-item tuples to dictionary
tuple_data = (('x', 24), ('y', 25))
dict_data = dict(tuple_data)
print(dict_data) # Output: {'x': 24, 'y': 25}
set( )
The set( ) function converts a value to a set.
Example:
# List to set
list_data = [1, 2, 3, 2, 1]
set_data = set(list_data)
print(set_data) # Output: {1, 2, 3}
# String to set of characters
str_data = "hello"
set_data = set(str_data)
print(set_data) # Output: {'h', 'e', 'l', 'o'}
frozenset( )
The frozenset( ) function converts a value to a frozenset, which is an immutable version of a set.
Example:
# List to frozenset
list_data = [1, 2, 3, 2, 1]
frozenset_data = frozenset(list_data)
print(frozenset_data) # Output: frozenset({1, 2, 3})
# String to frozenset of characters
str_data = "hello"
frozenset_data = frozenset(str_data)
print(frozenset_data) # Output: frozenset({'h', 'e', 'l', 'o'})
bool( )
The bool( ) function converts a value to a boolean.
Example:
# Integer to boolean
int_data = 0
bool_data = bool(int_data)
print(bool_data) # Output: False
# String to boolean
str_data = ""
bool_data = bool(str_data)
print(bool_data) # Output: False
complex( )
The complex( ) function converts a value to a complex number.
Example:
# Two integers to complex number
real = 3
imag = 4
complex_num = complex(real, imag)
print(complex_num) # Output: (3+4j)
# String to complex number
str_data = "3+4j"
complex_num = complex(str_data)
print(complex_num) # Output: (3+4j)
bytes( )
The bytes( ) function converts a value to a bytes object.
Example:
# String to bytes
str_data = "hello"
bytes_data = bytes(str_data, 'utf-8')
print(bytes_data) # Output: b'hello'
# List of integers to bytes
list_data = [104, 101, 108, 108, 111]
bytes_data = bytes(list_data)
print(bytes_data) # Output: b'hello'
bytearray( )
The bytearray( ) function converts a value to a bytearray object, which is a mutable sequence of bytes.
Example:
# String to bytearray
str_data = "hello"
bytearray_data = bytearray(str_data, 'utf-8')
print(bytearray_data) # Output: bytearray(b'hello')
# List of integers to bytearray
list_data = [104, 101, 108, 108, 111]
bytearray_data = bytearray(list_data)
print(bytearray_data) # Output: bytearray(b'hello')
memoryview( )
The memoryview( ) function returns a memory view object of the given argument, which must be a bytes-like object.
Example:
# Bytes to memoryview
bytes_data = b'hello'
memview_data = memoryview(bytes_data)
print(memview_data) # Output: <memory at 0x...>
# Accessing memoryview data
print(memview_data[0]) # Output: 104
Conclusion
Python’s built-in typecasting functions provide a robust way to ensure data is in the correct format for any operation. By understanding and utilizing these functions, you can write more efficient, error-free code and maintain data integrity across your applications.
Discover more from lounge coder
Subscribe to get the latest posts sent to your email.