{"id":12,"date":"2022-08-23T07:29:15","date_gmt":"2022-08-23T07:29:15","guid":{"rendered":"https:\/\/python.freelinuxtutorials.com\/?p=12"},"modified":"2022-08-23T10:06:12","modified_gmt":"2022-08-23T10:06:12","slug":"python-data-type-integer","status":"publish","type":"post","link":"https:\/\/python.freelinuxtutorials.com\/index.php\/2022\/08\/23\/python-data-type-integer\/","title":{"rendered":"Python>>Data Type:Integer"},"content":{"rendered":"<p class=\"post-title entry-title\"><span style=\"font-size: 16px;\">Integers or Int are whole numbers without the decimal point or fractional part. It can be positive or negative or zero.It can be binary, octal or hexadecimal values.<\/span><\/p>\n<div id=\"post-body-5044982677007989879\" class=\"post-body entry-content\">\n<p>Let&#8217;s do mathematical calculation:<\/p>\n<p>Arithmetic operators:<\/p>\n<p>&nbsp;<\/p>\n<div>addition = +<\/div>\n<div>subtraction = &#8211;<\/div>\n<div>multiplication = *<\/div>\n<div>division = \/<\/div>\n<div><\/div>\n<div>modulus = %<br \/>\nexponent = **<br \/>\nfloor division = \/\/<\/div>\n<div><\/div>\n<div>-modulus will output the remainder of the division<\/div>\n<div>-exponent will output the exponent<\/div>\n<div>-floor division will\u00a0\u00a0output\u00a0quotient in which the digits after the decimal point are removed.<\/div>\n<div><\/div>\n<div>Examples:<\/div>\n<p>&nbsp;<\/p>\n<blockquote>\n<div>&gt;&gt;&gt; 4+2<br \/>\n6<br \/>\n&gt;&gt;&gt; 4-2<br \/>\n2<br \/>\n&gt;&gt;&gt; 4*2<br \/>\n8<br \/>\n&gt;&gt;&gt; 4\/2<br \/>\n2.0<br \/>\n&gt;&gt;&gt; 9\/4<br \/>\n2.25<br \/>\n&gt;&gt;&gt; 9%4<br \/>\n1<br \/>\n&gt;&gt;&gt; 9\/\/4<br \/>\n2<\/div>\n<\/blockquote>\n<div><\/div>\n<div>So is like 9 divided by 4 = 2 remainder 1.So the quotient without the remainder is the floor division.<\/div>\n<div><\/div>\n<div>So from what I learned, if you use division , the quotient of two whole numbers (int) becomes float because it has decimal point.<\/div>\n<div><\/div>\n<div>Example:<\/div>\n<div>\n<blockquote>\n<div>&gt;&gt;&gt; 20\/2<\/div>\n<div>10.0<\/div>\n<div>&gt;&gt;&gt; div = 20\/2<\/div>\n<div>&gt;&gt;&gt; print (div)<\/div>\n<div>10.0<\/div>\n<div>&gt;&gt;&gt; print (type(div))<\/div>\n<div>&lt;class &#8216;float&#8217;&gt;<\/div>\n<\/blockquote>\n<\/div>\n<p>&nbsp;<\/p>\n<blockquote>\n<div>&gt;&gt;&gt; x = 8+8<\/div>\n<\/blockquote>\n<div>\n<blockquote>\n<div>&gt;&gt;&gt; print (x)<\/div>\n<div>16<\/div>\n<div>&gt;&gt;&gt; print (type(x))<br \/>\n&lt;class &#8216;int&#8217;&gt;<\/div>\n<\/blockquote>\n<\/div>\n<div><\/div>\n<div>Some key things to take note:<\/div>\n<div><\/div>\n<div>1. Leading zeroes are not allowed to represent integeer or whole numbers<\/div>\n<div>e.g.<\/div>\n<div>\n<blockquote>\n<div>&gt;&gt;&gt; 012345<\/div>\n<div>\u00a0 File &#8220;&lt;stdin&gt;&#8221;, line 1<\/div>\n<div>\u00a0 \u00a0 012345<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0^<\/div>\n<\/blockquote>\n<div>SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers<\/div>\n<\/div>\n<div><\/div>\n<div>2. It does not allow comma to use as delimeter, use the symbol underscore (_)<\/div>\n<div>e.g.<\/div>\n<div>\n<blockquote>\n<div>&gt;&gt;&gt; 1,2,3,4<\/div>\n<div>(1, 2, 3, 4)<\/div>\n<div>&gt;&gt;&gt; 1_2_3_4<\/div>\n<div>1234<\/div>\n<\/blockquote>\n<\/div>\n<div>3. Do not use decimal point, else it will become a float data type<\/div>\n<div>e.g.<\/div>\n<div>\n<blockquote>\n<div>&gt;&gt;&gt; x = 100<\/div>\n<div>&gt;&gt;&gt; print (type(x))<\/div>\n<div>&lt;class &#8216;int&#8217;&gt;<\/div>\n<div>&gt;&gt;&gt; x = 100.0<\/div>\n<div>&gt;&gt;&gt; print (type(x))<\/div>\n<div>&lt;class &#8216;float&#8217;&gt;<\/div>\n<\/blockquote>\n<\/div>\n<div><\/div>\n<div>4. As mentioned above, integers\u00a0 can be binary, octal or hexadecimal values, but need to use these characters in front of it<\/div>\n<div>binary = <u>0b<\/u><\/div>\n<div>hexadecimal = <u>0x <\/u>(or 0X)<\/div>\n<div>octal = <u>0o <\/u>(or 0O)<\/div>\n<div>e.g.<\/div>\n<div>decimal= 100<\/div>\n<div>if convert becomes:<\/div>\n<div>binary =\u00a01100100<\/div>\n<div>hexadecimal =\u00a064<br \/>\noctal =\u00a0144<\/div>\n<div><\/div>\n<div>\n<blockquote>\n<div>&gt;&gt;&gt; 100<\/div>\n<div>100<\/div>\n<div>&gt;&gt;&gt; 0b1100100<\/div>\n<div>100<\/div>\n<div>&gt;&gt;&gt; 0x64<\/div>\n<div>100<\/div>\n<div>&gt;&gt;&gt; 0o144<\/div>\n<div>100<\/div>\n<\/blockquote>\n<\/div>\n<div><\/div>\n<div><\/div>\n<div>5. there&#8217;s a function called int() to convert strings to int<\/div>\n<div><\/div>\n<div>\n<blockquote>\n<div>&gt;&gt;&gt; x = 12.34<\/div>\n<div>&gt;&gt;&gt; int(x)<\/div>\n<div>12<\/div>\n<\/blockquote>\n<div><\/div>\n<\/div>\n<div>\n<blockquote>\n<div>&gt;&gt;&gt; x = 777.888<\/div>\n<div>&gt;&gt;&gt; int (x)<\/div>\n<div>777<\/div>\n<\/blockquote>\n<\/div>\n<blockquote>\n<div><\/div>\n<\/blockquote>\n<div>\n<blockquote>\n<div>&gt;&gt;&gt; int(&#8216;100&#8217;)<\/div>\n<div>100<\/div>\n<\/blockquote>\n<\/div>\n<blockquote>\n<div><\/div>\n<\/blockquote>\n<div><\/div>\n<div><\/div>\n<div>If you directly put it inside the int function, you will get something like this error:<\/div>\n<div><\/div>\n<div>\n<blockquote>\n<div>&gt;&gt;&gt; int (&#8216;777.888&#8217;)<\/div>\n<div>Traceback (most recent call last):<\/div>\n<div>\u00a0 File &#8220;&lt;stdin&gt;&#8221;, line 1, in &lt;module&gt;<\/div>\n<div>ValueError: invalid literal for int() with base 10: &#8216;777.888&#8217;<\/div>\n<\/blockquote>\n<\/div>\n<div><\/div>\n<div>Since it is a float, need to do something like this:<\/div>\n<div><\/div>\n<div>\n<blockquote>\n<div>&gt;&gt;&gt; int(float(&#8216;777.888&#8217;))<\/div>\n<div>777<\/div>\n<\/blockquote>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Integers or Int are whole numbers without the decimal point or fractional part. It can be positive or negative or zero.It can be binary, octal or hexadecimal values. Let&#8217;s do mathematical calculation: Arithmetic operators: &nbsp; addition = + subtraction = &#8211; multiplication = * division = \/ modulus = % exponent = ** floor division&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","category-data-types"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":2,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"predecessor-version":[{"id":14,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/posts\/12\/revisions\/14"}],"wp:attachment":[{"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python.freelinuxtutorials.com\/index.php\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}