Type Conversion in Python

Type Conversion in Python allows users to convert int type to another, for example, float to int, int to complex, int to float, etc. To convert a number from one type to another, for example, into float, we have the following methods in Python:

  • float(num): To convert from num int to float type.
  • int(num): To convert num to int type.
  • long(num): To convert num to long type.
  • complex(num): To convert from num int to complex type.
  • Complex (Num1, num2): To convert num1 and num2 to complex type, wherein num1 will be the real part, whereas num2 will be the imaginary part with j.

Let us now see some examples of Type Conversion and convert one datatype to another:

Convert int to float in Python

Let us see an example of float() in Python to convert type int into float:

The output is as follows:

Convert from float to int in Python

Let us see an example of the int() in Python to convert from type float to int:

The output is as follows:

Convert int to complex in Python

Let us see an example of the complex() in Python to typecast int to complex type:

The output is as follows:

Video Tutorial (English)

Video Tutorial (Hindi)

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Python Numbers with Examples
Python IDLE
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

No Comments

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading