Python TypeCasting for Different Types

By | 2018-03-30T12:46:41+00:00 March 30th, 2018|Python|0 Comments

In this tutorials, we will see How Python Type Casting works.

Python TypeCasting :

In Python we can convert one type 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 string to int, the converted 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 above mentioned statements are invalid.

Converting any type to float:

  • The float() function is used to convert other type to float type.
  • We can convert any type value to float except complex types.
  • While converting String type to float the orresponding 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 above mentioned statements 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 be 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 complex number with 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 imaginary and real part :

complex(x,y)

  • We can use this function to convert x and y into complex number, so that x will be real part and y will be 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 evalutes 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 other wise 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() functon is used to convert any other 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 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment