TYPE CASTING

Convert to integer

float_num = 3.14

int_num = int(float_num)

print(int_num)  # Output: 3

convert to float

int_num = 5

float_num = float(int_num)

print(float_num)  # Output: 5.0

convert to string

number = 42

str_num = str(number)

print(str_num)  # Output: '42'

convert to boolean

num = 0

bool_value = bool(num)

print(bool_value)  # Output: False

convert to complex

int_num = 2

complex_num = complex(int_num)

print(complex_num)  # Output: (2+0j)

convert to list,tuple,set

string = "hello"

list_chars = list(string)

tuple_chars = tuple(string)

set_chars = set(string)

 

print(list_chars)  # Output: ['h', 'e', 'l', 'l', 'o']

print(tuple_chars) # Output: ('h', 'e', 'l', 'l', 'o')

print(set_chars)   # Output: {'h', 'e', 'l', 'o'}

convert to/from ascii

char = 'A'

ascii_value = ord(char)

char_again = chr(ascii_value)

 

print(ascii_value)  # Output: 65

print(char_again)   # Output: 'A'