Posted in

Python Type Casting (Part 2): What is Type Casting or Type Conversion in Python?

Python Type Casting Part 2
Python Type Casting Part 2

Click for Python Type Casting (Part 1)

Python Type Casting

float() function in Python

Syntax: float(value)

value = Only one argument is required; we can provide any value that could be converted into a float.
Code:
a = 10      
b = 20.0    
c = a + b
print(c)
print(type(c))

Output:
30.0
<class 'float'>
Code:
num1 = '22'
print(type(num1))
num2 = float(num1)
print(type(num2))
print(num2)

Output:
<class 'str'>
<class 'float'>
22.0
Code:
text = float('Python')
print(text)

Output:
ValueError: could not convert string to float: 'Python'
Code:
num1 = 10
num2 = 20
print(float(num1+num2))

Output:
30.0

complex() Function in Python

Syntax: complex (real, imaginary)
It took two arguments:
real (required) = first argument; we provide a number here; this is the real part of the complex number. So we provide a real number here. The number could be positive, negative, or zero. The default value is zero.
imaginary (optional) = second argument; here we provide an imaginary number. The default value is zero.
Code:
num1 = '4+5j'
print(type(num1))
num2 = complex(num1)
print(num2)
print(type(num2))

Output:
<class 'str'>
(4+5j)
<class 'complex'>
Code:
num1 = 4
num2 = complex(num1)
print(num2)

Output:
(4+0j)
Code:
num1 = 4
num2 = 5
num3 = complex(num1, num2)
print(num3)

Output:
(4+5j)
Code:
num1 = complex()
print(num1)

Output:
0j
Code:
num1 = 5
num2 = complex(imag=num1)
print(num2)

Output:
5j

str() function in Python

Syntax: str(object, encoding, errors)
It took three arguments. First is required and others are optional.
Object (required): First argument: Here we provide any data type, which we want to convert into a string data type.
Encoding (optional): Second argument. Here we provide the encoding method for the object. Default value is UTF-8. UTF means unicode transformation format, and 8 numbers indicate the 8-bit values are used for endcoding. We can change values to UTF-16, UTF-32, etc.
Errors (optional): Third argument. If decoding is failed, then how should the error be handled? We set value in this argument for error handling. Default value is 'strict'.
Code:
num1 = 15
string_int = str(num1)
print(string_int)
print(type(string_int))
num2 = True
string_bool = str(num2)
print(string_bool)
print(type(string_bool))
num3 = 4+5j
string_complex = str(num3)
print(string_complex)
print(type(string_complex))

Output:
15
<class 'str'>
True
<class 'str'>
(4+5j)
<class 'str'>

tuple() function in Python

Syntax: tuple(iterator)
Only one argument is required.
Iterator (required): Here we provide any iterable object.
Code:
data1 = "Python"
tuple_str = tuple(data1)
print(tuple_str)
print(type(tuple_str))
data2 = ['Hello', 'Python', 100]
tuple_list = tuple(data2)
print(tuple_list)
print(type(tuple_list))

Output:
('P', 'y', 't', 'h', 'o', 'n')
<class 'tuple'>
('Hello', 'Python', 100)
<class 'tuple'>
Code:
data1 = 100
tuple_int = tuple(data1)
print(tuple_int)

Output:
TypeError: 'int' object is not iterable

list() Function in Python

Syntax: list(iterable)
Only one mandatory argument.
Iterator (required): Here we provide any iterable object.
Code:
data1 = 'Python'
list_str = list(data1)
print(list_str)
print(type(list_str))
data2 = ('Hello', 'Python', 10)
list_tuple = list(data2)
print(list_tuple)
print(type(list_tuple))

Output:
['P', 'y', 't', 'h', 'o', 'n']
<class 'list'>
['Hello', 'Python', 10]
<class 'list'>

set() Function in Python

Syntax: set(iterable)
Only one mandatory argument.
Iterator (required): Here we provide any iterable object.
Code:
data1 = ('This', 'Time')
set_tuple = set(data1)
print(set_tuple)
print(type(set_tuple))
data2 = ['Hello', 'Python']
set_list = set(data2)
print(set_list)
print(type(set_list))

Output:
{'This', 'Time'}
<class 'set'>
{'Hello', 'Python'}
<class 'set'>

dict() Function in Python

Code:
new_dict = dict(name = 'A', age = 33, place = 'UK')
print(new_dict)

Output:
{'name': 'A', 'age': 33, 'place': 'UK'}
Code:
first_tuple = (['Fruit', 'Mango'], ['Number', 1], ['Name', 'A'])
print(type(first_tuple))
first_dict = dict(first_tuple)
print(first_dict)
print(type(first_dict))

Output:
<class 'tuple'>
{'Fruit': 'Mango', 'Number': 1, 'Name': 'A'}
<class 'dict'>
Code:
name = "Python"
name2 = dict(name)
print(name2)

Output:
ValueError: dictionary update sequence element #0 has length 1; 2 is required

frozenset() Function in Python

Code:
new_set = frozenset({'Apple', 'Mango'})
print(new_set)
print(type(new_set))

Output:
frozenset({'Apple', 'Mango'})
<class 'frozenset'>

chr() Function in Python

Syntax: chr(num)
Only one argument is required.
num (required): Here we provide a valid unicode number.
Code:
num1 = chr(65)
print(num1)

Output:
A
Code:
num1 = chr(83)
print(num1)

Output:
S
Code:
num1 = chr(100)
print(num1)
print(type(num1))

Output:
d
<class 'str'>

ord() Function in Python

Code:
num1 = ord('A')
print(num1)

Output:
65
Code:
num1 = ord('P')
print(num1)
print(type(num1))

Output:
80
<class 'int'>

Conclusion

Click for the Python input() function

2 thoughts on “Python Type Casting (Part 2): What is Type Casting or Type Conversion in Python?

Leave a Reply

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