- We can convert one type value to another type. This conversion is called Typecasting.
- The following are various inbuilt functions for type casting.
- int()
- float()
- complex()
- bool()
- str()
int()
- We can use this function to convert values from other types to int.
Example 1
Python
# float to int
num1 = 123.987
print(int(num1))
Output
PowerShell
123
Example 2
Python
# bool to int
bol1 = True
bol2 = False
print(int(bol1))
print(int(bol2))
Output
PowerShell
1
0
Example 3
Python
# str to int
str1 = "10"
print(int(str1))
Output
PowerShell
10
Example 4
Python
# complex to int
com1 = 10 + 5j
print(int(com1))
Output
PowerShell
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'complex'
Example 5
Python
# str to int
str1 = "10.5"
print(int(str1))
str2 = "ten"
print(int(str2))
str3 = "0B1111"
print(int(str3))
Output
PowerShell
# ValueError: invalid literal for int() with base 10: '10.5'
# ValueError: invalid literal for int() with base 10: 'ten'
# ValueError: invalid literal for int() with base 10: '0B1111'
Remark
- We can convert from any type to int except complex type.
- If we want to convert str type to int type, compulsory str should contain only integral value and should be specified in base-10.
float()
- We can use float() function to convert other type values to float type.
Example 1
Python
# int to float
num1 = 10
print(float(num1))
Output
PowerShell
10.0
Example 2
Python
# bool to float
bol1 = True
print(float(bol1))
bol2 = False
print(float(bol2))
Output
PowerShell
1.0
0.0
Example 3
Python
# str to float
str1 = "10"
print(float(str1))
str2 = "10.5"
print(float(str2))
Output
PowerShell
10.0
10.5
Example 4
Python
# complex to float
com1 = 10 + 5j
print(float(com1))
Output
PowerShell
TypeError: float() argument must be a string or a real number, not 'complex'
Example 5
Python
# str to float
str1 = "ten"
print(float(str1))
str2 = "0B1111"
print(float(str2))
Output
PowerShell
# ValueError: could not convert string to float: 'ten'
# ValueError: could not convert string to float: '0B1111'
Remark
- We can convert any type value to float type except complex type.
- Whenever we are trying to convert str type to float type compulsory str should be either integral or floating point literal and should be specified only in base-10.
complex()
- We can use complex() function to convert other types to complex type.
Form-1: complex(x)
- We can use this function to convert x into complex number with real part x and imaginary part 0.
Example 1
Python
num1 = 10
print(complex(num1))
num2 = 10.5
print(complex(num2))
bool1 = True
print(complex(bool1))
bool2 = False
print(complex(bool2))
str1 = "10"
print(complex(str1))
str2 = "10.5"
print(complex(str2))
Output
PowerShell
(10+0j)
(10.5+0j)
(1+0j)
0j
(10+0j)
(10.5+0j)
Example 2
Python
str1 = "ten"
print(complex(str1))
Output
PowerShell
ValueError: complex() arg is a malformed string
Form-2: complex(x,y)
- We can use this method to convert x and y into complex number such that x will be real part and y will be imaginary part.
Example 1
Python
num1 = 10
num2 = -2
print(complex(num1, num2))
bool1 = True
bool2 = False
print(complex(bool1, bool2))
Output
PowerShell
(10-2j)
(1+0j)
bool()
- We can use this function to convert other type values to bool type.
Example 1
Python
# int to bool
num1 = 0
print(bool(num1))
num2 = 1
print(bool(num2))
num3 = 10
print(bool(num3))
Output
PowerShell
False
True
True
Example 2
Python
# float to bool
float1 = 10.5
print(bool(float1))
float2 = 0.178
print(bool(float2))
float3 = 0.0
print(bool(float3))
Output
PowerShell
True
True
False
Example 3
Python
# complex to bool
com1 = 10-2j
print(bool(com1))
com2 = 0+1.5j
print(bool(com2))
com3 = 0+0j
print(bool(com3))
Output
PowerShell
True
True
False
Example 4
Python
# str to bool
str1 = "True"
print(bool(str1))
str2 = "False"
print(bool(str2))
str3 = ""
print(bool(str3))
Output
PowerShell
True
True
False
str()
- We can use this method to convert other type values to str type.
Example 1
Python
# int to str
num1 = 10
print(str(num1))
Output
PowerShell
"10"
Example 2
Python
# float to str
float1 = 10.5
print(str(float1))
Output
PowerShell
"10.5"
Example 3
Python
# complex to str
com1 = 10+5j
print(str(com1))
Output
PowerShell
"10+5j"
Example 4
Python
# bool to str
bool1 = True
print(str(bool1))
Output
PowerShell
"True"
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!