

{"id":5404,"date":"2017-12-23T06:37:49","date_gmt":"2017-12-23T06:37:49","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=5404"},"modified":"2026-04-28T10:56:41","modified_gmt":"2026-04-28T05:26:41","slug":"python-number-types-conversion","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/","title":{"rendered":"Python Numeric Data Types &#8211; Int, Float, Complex"},"content":{"rendered":"<p>Let us dig into Python numeric data types.<\/p>\n<p>As we have seen, a Python number can be- Python int, a Python float, or even a Python complex number. Long is no longer supported by Python 3.x.<\/p>\n<p>So, let\u2019s begin with the Python number types tutorial.<\/p>\n<div id=\"attachment_35642\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Number-Types-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-35642\" class=\"wp-image-35642 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Number-Types-01.jpg\" alt=\"Python Number Types - Python Int, Float, Complex Numbers\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Number-Types-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Number-Types-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Number-Types-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Number-Types-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Number-Types-01-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Number-Types-01-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-35642\" class=\"wp-caption-text\">Python Number Types &#8211; Python Int, Float, Complex Numbers<\/p><\/div>\n<h3>Python Numeric Data Types<\/h3>\n<p>A number is an arithmetic entity that lets us measure something.<\/p>\n<p>Python handles integers, floating-point numbers, and even very large numbers with ease. You write an integer like 42 and a float like 3.14. Python picks the right size automatically, so beginners don\u2019t need to worry about overflow.<\/p>\n<p>Numeric Data Types in Python perform arithmetic operations. It includes operations like:<\/p>\n<ul>\n<li>Addition (+)<\/li>\n<li>Subtraction(-)<\/li>\n<li>Multiplication(*)<\/li>\n<li>Division (\/)<\/li>\n<li>Floor division (\/\/)<\/li>\n<li>Modules (%)<\/li>\n<li>Exponential (**)<\/li>\n<\/ul>\n<p>So now let&#8217;s start with Python number types.<\/p>\n<ul>\n<li><strong>None-<\/strong> The None keyword indicates the absence of a value.<\/li>\n<\/ul>\n<h4>Python int<\/h4>\n<p>Python can hold signed integers.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=7\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">7<\/div>\n<p>It can hold a value of any length, the only limitation being the amount of memory available.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=9999999999999999999999999999999999999\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">9999999999999999999999999999999999999<\/div>\n<p>There are three int types of Python number types:<\/p>\n<div id=\"attachment_35643\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-int-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-35643\" class=\"size-full wp-image-35643\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-int-01.jpg\" alt=\"Python Number Types - Python Int, Float, Complete Numbers\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-int-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-int-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-int-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-int-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-int-01-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-int-01-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-35643\" class=\"wp-caption-text\">Python Number Types &#8211; Python Int<\/p><\/div>\n<p><strong>1. Python type() function<\/strong><\/p>\n<p>The type() function in Python takes one argument and returns the class it belongs to.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=9999999999999999999999999999999999999\r\n&gt;&gt;&gt; type(a)\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class &#8216;int&#8217;&gt;<\/div>\n<p><strong>2. Python isinstance() function<\/strong><\/p>\n<p>The isinstance() function in Python takes two arguments. The first is the construct(ex- a variable or a list), and the second is a class.<\/p>\n<p>It returns True or False based on whether the construct belongs to that class.<\/p>\n<p>Suppose we want to check if \u2018a\u2019 belongs to class bool. We write the following code for the same.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; isinstance(a,bool)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>Since it belongs to the class \u2018int\u2019 instead, it returns False.<\/p>\n<p><strong>3. Python Exponential numbers<\/strong><\/p>\n<p>You can write an exponential number using the letter \u2018e\u2019 between the mantissa and the exponent.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(2e5)<\/pre>\n<p>200000.0<\/p>\n<p>Remember that this is power of 10. To raise a number to another\u2019s power, we use the ** operator.<\/p>\n<h4>Python float<\/h4>\n<p>Python also supports floating-point real values. An int cannot store the value of the mathematical constant pi, but a float can.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; from math import pi\r\n&gt;&gt;&gt; pi<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3.141592653589793<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; type(pi)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class &#8216;float&#8217;&gt;<\/div>\n<p>A float value is only accurate up to 15 decimal places. After that, it rounds the number off.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=1.1111111111111111119\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1.1111111111111112<\/div>\n<p>Note that division results in floats.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 2\/2<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1.0<\/div>\n<h4>Python Complex Numbers<\/h4>\n<p>A complex number is a Python number type made of real and imaginary parts. It is represented as a+bj.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=2+3j\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(2+3j)<\/div>\n<p><strong>1. Coefficient to the imaginary part<\/strong><\/p>\n<p>Here, 2 is the real part, and 3j is the imaginary part.<\/p>\n<p>To denote the irrational part, however, you can\u2019t use the letter \u2018i\u2019, like you would do on paper.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=2+3i<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">SyntaxError: invalid syntax<\/div>\n<p>Also, it is mandatory to provide a coefficient to the imaginary part.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=2+j<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#33&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>a=2+j<\/p>\n<p>NameError: name &#8216;j&#8217; is not defined<\/p>\n<\/div>\n<p>In this case, a coefficient of 1 will do.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=2+1j\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(2+1j)<\/div>\n<p><strong>2. Operations on complex numbers<\/strong><\/p>\n<p>Finally, you can perform the basic operations on complex numbers too.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a=2+3j\r\n&gt;&gt;&gt; b=2+5j\r\n&gt;&gt;&gt; a+b<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(4+8j)<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a*=2\r\n&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(4+6j)<\/div>\n<p>Here, *= is an in-place assignment operator.<\/p>\n<p>Any Doubt yet in Python number Type? Please Comment.<\/p>\n<h3>Writing numbers in binary, octal, and hexadecimal in Python<\/h3>\n<p>More often than not, programmers need to deal with numbers other than decimal. To do this, you can use appropriate prefixes.<\/p>\n<table>\n<tbody>\n<tr>\n<td width=\"312\"><strong>Number System<\/strong><\/td>\n<td width=\"312\"><strong>Prefix<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"312\">Binary<\/td>\n<td width=\"312\">0b or 0B<\/td>\n<\/tr>\n<tr>\n<td width=\"312\">Octal<\/td>\n<td width=\"312\">0o or 0O<\/td>\n<\/tr>\n<tr>\n<td width=\"312\">Hexadecimal<\/td>\n<td width=\"312\">0x or 0X<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>1. Binary Numbers in Python<\/strong><\/p>\n<p>When you want to write a binary number, use the prefix 0b or 0B. For example, we know that the binary for 7 is 111.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(0b111)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">7<\/div>\n<p>You can also apply conversion functions on these numbers.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; int(0b10)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<p><strong>2. Octal Numbers in Python<\/strong><\/p>\n<p>The prefix for octal is 0o or 0O.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(0O10)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">8<\/div>\n<p>The following code causes an error. This is because the octal number system does not have the number 8. It has the numbers 0-7.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(0O8)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">SyntaxError: invalid token<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; float(0B10)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2.0<\/div>\n<p><strong>3. Hexadecimal Numbers in Python<\/strong><\/p>\n<p>The hexadecimal number system has numbers 0-9 and then A-F. For that, use the prefix 0x or 0X.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(0xFF)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">255<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(0xFE)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">254<\/div>\n<h3>Python Conversion Functions<\/h3>\n<p>Although most times Python does the conversion as needed, you can do it explicitly if you want.<\/p>\n<p>These functions allow us to convert one numeric type into another python numeric data types.<\/p>\n<div id=\"attachment_35644\" style=\"width: 1090px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Conversion-Functions-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-35644\" class=\"size-full wp-image-35644\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Conversion-Functions-01.jpg\" alt=\"Python Number Types - Python Int, Float, Complete Numbers\" width=\"1080\" height=\"1080\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Conversion-Functions-01.jpg 1080w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Conversion-Functions-01-150x150.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Conversion-Functions-01-300x300.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Conversion-Functions-01-768x768.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Conversion-Functions-01-1024x1024.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Conversion-Functions-01-160x160.jpg 160w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Conversion-Functions-01-320x320.jpg 320w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Conversion-Functions-01-520x520.jpg 520w\" sizes=\"auto, (max-width: 1080px) 100vw, 1080px\" \/><\/a><p id=\"caption-attachment-35644\" class=\"wp-caption-text\">Python Number Types &#8211; Python Conversion Functions<\/p><\/div>\n<h4>1. int() in Python<\/h4>\n<p>Python int() function can convert another numeric type into an int.<\/p>\n<p>It can also convert other types into an int, but in this tutorial, we focus on numeric types.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; int(7)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">7<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; int(7.7)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">7<\/div>\n<p>As you can see, it does not round the number 7.7 up to 8; it truncates the 0.7.<\/p>\n<p>However, you cannot convert a complex number into an int.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; int(2+3j)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#22&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>int(2+3j)<\/p>\n<p>TypeError: can&#8217;t convert complex to int<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; int(3j)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#23&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>int(3j)<\/p>\n<p>TypeError: can&#8217;t convert complex to int<\/p>\n<\/div>\n<p>We can also apply this function on representations other than decimal, i.e., binary, octal, and hexadecimal.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; int(0b10)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; int(0xF)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">15<\/div>\n<h4>2. float() in Python<\/h4>\n<p>The float() function in Python converts another numeric type into a float.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; float(110)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">110.0<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; float(110.0)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">110.0<\/div>\n<p>Like int(), float() can\u2019t convert a complex either.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; float(3j)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#26&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>float(3j)<\/p>\n<p>TypeError: can&#8217;t convert complex to float<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; float(0o10)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">8.0<\/div>\n<p>Here, we applied it to an octal number.<\/p>\n<h4>3. complex() in Python<\/h4>\n<p>The complex() function in Python converts another numeric type into a complex number.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; complex(2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(2+0j)<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; complex(2.3)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(2.3+0j)<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; complex(2+3.0j)\r\n\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(2+3j)<\/div>\n<h4>4. bin() in Python<\/h4>\n<p>The bin() function in Python returns the binary value of a number.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bin(2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;0b10&#8217;<\/div>\n<p>However, you can\u2019t apply it to a float value or a complex value. The same is true for oct() and hex() functions too.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bin(2.3)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#49&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>bin(2.3)<\/p>\n<p>TypeError: &#8216;float&#8217; object cannot be interpreted as an integer<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bin(2+3j)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#50&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>bin(2+3j)<\/p>\n<p>TypeError: &#8216;complex&#8217; object cannot be interpreted as an integer<\/p>\n<\/div>\n<h4>5. oct() in Python<\/h4>\n<p>The oct() function in Python returns the octal value of a number.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; oct(8)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;0o10&#8217;<\/div>\n<p>We know that 8.0 is the same as 8, but the function doesn\u2019t think the same. It is a float, so it cannot convert it into an oct.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; oct(8.0)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#59&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>oct(8.0)<\/p>\n<p>TypeError: &#8216;float&#8217; object cannot be interpreted as an integer<\/p>\n<\/div>\n<h4>6. hex() in Python<\/h4>\n<p>The hex() function in Python returns the hexadecimal value of a number.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; hex(255)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;0xff&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; hex(0)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;0x0&#8217;<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; hex(0)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;0x0&#8217;<\/div>\n<h4>Python Decimal Module<\/h4>\n<p>Let\u2019s try adding 1.1 and 2.2 in the shell, and let\u2019s compare it with 3.3.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; (1.1+2.2)==3.3<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>Why did it return False? Let\u2019s try printing the sum.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 1.1+2.2<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3.3000000000000003<\/div>\n<p>Woah, how did this happen? Well, this is duly attributed to hardware limitations and is not a flaw of Python.<\/p>\n<p>Because the hardware stores decimals as binary fractions, it isn\u2019t possible to store it very accurately.<\/p>\n<p>Let\u2019s take an example.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 1\/3<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">0.3333333333333333<\/div>\n<p>When we divide 1 by 3, it doesn\u2019t return the full value, which is 0.3333333333333333\u2026 Python does provide a solution to this problem.<\/p>\n<p>It has the \u2018decimal\u2019 module, which lets us choose precision. We will learn about modules in a later lesson.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import decimal\r\n&gt;&gt;&gt; print(decimal.Decimal(0.1))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">0.1000000000000000055511151231257827021181583404541015625<\/div>\n<p>Did you see what happened here? The Decimal() function preserved the significance.<\/p>\n<p>This was the Decimal Function Python number type.<\/p>\n<h4>Fractions Module in Python<\/h4>\n<p>Another module that Python provides, the fractions module lets you deal with fractions.<\/p>\n<p>The Fraction() function in Python returns the value in the form of a numerator and denominator.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; from fractions import Fraction\r\n&gt;&gt;&gt; print(Fraction(1.5))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3\/2<\/div>\n<p>It can also take two arguments.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; print(Fraction(1,3))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1\/3<\/div>\n<h4>Math Module in Python<\/h4>\n<p>Another essential module in Python is the math module.<\/p>\n<p>It has all important mathematical functions like exp, trigonometric functions, logarithmic functions, factorial, and more.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import math\r\n&gt;&gt;&gt; math.factorial(5)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">120<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; math.exp(3)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">20.085536923187668<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; math.tan(90)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">-1.995200412208242<\/div>\n<p>This was all about the Python number types tutorial.<\/p>\n<h3>Conclusion<\/h3>\n<p>In this lesson, we learned about Python numeric data types.<\/p>\n<p>We looked at int, float, and complex numbers. We also looked at how to write numbers in binary, octal, and hexadecimal representations.<\/p>\n<p>Then we looked at how to convert one numeric type into another in Python.<\/p>\n<p>We also looked at some important modules- decimal, fractions, and math. Hope you like the Python Number Type Tutorial.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let us dig into Python numeric data types. As we have seen, a Python number can be- Python int, a Python float, or even a Python complex number. Long is no longer supported by&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":35642,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[9160,36634,10723,10725,23689,23688,15081],"class_list":["post-5404","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-numbers-in-python","tag-python-conversion-functions","tag-python-number-type","tag-python-numbers","tag-python-numeric-data-types","tag-python-numeric-types","tag-types-of-numbers-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Numeric Data Types - Int, Float, Complex - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn python numeric data types like - int, float, complex number, conversion functions, decimal module, fraction and math module in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Numeric Data Types - Int, Float, Complex - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn python numeric data types like - int, float, complex number, conversion functions, decimal module, fraction and math module in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-12-23T06:37:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-28T05:26:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Number-Types-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Numeric Data Types - Int, Float, Complex - DataFlair","description":"Learn python numeric data types like - int, float, complex number, conversion functions, decimal module, fraction and math module in Python.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/","og_locale":"en_US","og_type":"article","og_title":"Python Numeric Data Types - Int, Float, Complex - DataFlair","og_description":"Learn python numeric data types like - int, float, complex number, conversion functions, decimal module, fraction and math module in Python.","og_url":"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2017-12-23T06:37:49+00:00","article_modified_time":"2026-04-28T05:26:41+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Number-Types-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Numeric Data Types &#8211; Int, Float, Complex","datePublished":"2017-12-23T06:37:49+00:00","dateModified":"2026-04-28T05:26:41+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/"},"wordCount":1409,"commentCount":21,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Number-Types-01.jpg","keywords":["numbers in python","python conversion functions","python number type","python numbers","Python Numeric data types","Python Numeric types","types of numbers in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/","url":"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/","name":"Python Numeric Data Types - Int, Float, Complex - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Number-Types-01.jpg","datePublished":"2017-12-23T06:37:49+00:00","dateModified":"2026-04-28T05:26:41+00:00","description":"Learn python numeric data types like - int, float, complex number, conversion functions, decimal module, fraction and math module in Python.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Number-Types-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2017\/12\/Python-Number-Types-01.jpg","width":1200,"height":628,"caption":"Python Number Types - Python Int, Float, Complete Numbers"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-number-types-conversion\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Numeric Data Types &#8211; Int, Float, Complex"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5404","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=5404"}],"version-history":[{"count":17,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5404\/revisions"}],"predecessor-version":[{"id":147971,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5404\/revisions\/147971"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/35642"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=5404"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=5404"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=5404"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}