In this tutorial, we will see How Python Type Casting works.
Python TypeCasting :
In Python, we can convert one type of value to another type. This conversion is called typecasting or type coercion.
Python provides various inbuilt functions for typecasting.
- int()
- float()
- conplex()
- bool()
- str()
Converting any type to int :
- Python int() function is used to covert any type to int type.
- int() fucntion doesn’t convert complex types.
- If we want to convert a string to int, the translated string should contain integral values and should be specified in base-10.
Valid int conversion Example :
x= int(156.23)
print(x)
y = int(True)
print(y)
p = int(False)
print(p)
Output :
156
1
0
Invalid int Conversions :
x= int("10.23")
y = int("Hello")
p = int("0B1010")
q = int(10+5p)
The statements mentioned above are fallacious.
Converting any type to float:
- The float() function is used to convert the given type to float type.
- We can convert any type of value to float except complex types.
- While converting String type to float the corresponding string should be either integral or floating point literal and should be specified only in base-10
Valid float conversion Example :
x= float(10)
print(x)
y = float(True)
print(y)
z = float("10.5")
print(z)
p = float(False)
print(p)
Output :
10.0
1.0
10.5
0.0
Invalid float Conversions :
x= float("ten")
print(x)
y = float(10+5x)
print(y)
z = float("0B1111")
print(z)
The statements mentioned above are invalid.
Converting any type to complex:
- The complex() function is used to convert other types to complex types.
- complex() type is a combination of real and imaginary parts.
- The conversion can happen only on single – either imaginary or real part, or both imaginary and real parts.
Single either imaginary or complex :
complex(x) :
- We can use this function to convert x into a complex number with the real part as x and imaginary part as 0.
Example :
x= complex(10)
print(x)
y = complex(True)
print(y)
z = complex("20")
print(z)
a = complex("10.75")
print(a)
Output :
(10+0j)
(1+0j)
(20+0j)
(10.75+0j)
Both the imaginary and real part :
complex(x,y)
- We can use this function to convert x and y into a complex number so that x will be the real part and y will be the imaginary part.
x= complex(10,-2)
print(x)
y = complex(True,False)
print(y)
z = complex(20,1)
print(z)
Output :
(10-2j)
(1+0j)
(20+1j)
Converting any type to boolean:
- The bool() function is used to convert other types to boolean type.
- bool() consider 0 as False and 1 as True
- bool() function evaluates the expression; if the given value total (sum) is 0 then it will return False. Otherwise, it will return True.
- If we pass complex number to bool(): If both imaginary and real parts are 0, i.e. 0+0j then the result is False; otherwise the result is True.
- If the given string is empty, the result is False; otherwise, it will return True.
Example :
x= bool(0)
print(x)
y = bool(1)
print(y)
z = bool(1.5)
print(z)
a = bool(0.5)
print(a)
b = bool(0.0)
print(b)
c= bool(10-20j)
print(c)
d= bool(1+5j)
print(d)
e= bool(0+0j)
print(e)
f= bool("True")
print(f)
g= bool("False")
print(g)
h= bool("")
print(h)
Output :
False
True
True
True
False
True
True
False
True
True
False
Converting any type to string:
- str() function is used to convert the given type to string type.
Example :
x= str(20)
print(type(x))
y = str(10.5)
print(type(y))
z = str(1.5j)
print(type(z))
a = str(True)
print(type(a))
Output:
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
Happy Learning 🙂