{"id":909,"date":"2020-11-01T08:47:41","date_gmt":"2020-11-01T08:47:41","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=909"},"modified":"2025-03-27T15:10:19","modified_gmt":"2025-03-27T15:10:19","slug":"python-floor-division","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/advanced-python\/python-floor-division\/","title":{"rendered":"Python Floor Division"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about Python floor division operator (\/\/) or mod.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-python-floor-division'>Introduction to Python floor division <a href=\"#introduction-to-python-floor-division\" class=\"anchor\" id=\"introduction-to-python-floor-division\" title=\"Anchor for Introduction to Python floor division\">#<\/a><\/h2>\n\n\n\n<p>Suppose you have a division of two integers:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">101 \/ 4<\/code><\/span><\/pre>\n\n\n<p>In this division, 101 is called a numerator (<code>N<\/code>) and 4 is called a denominator (<code>D<\/code>).<\/p>\n\n\n\n<p>The integer division 101 \/ 4 returns 25 with the remainder 1. In other words:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">101 \/ 4 = 25 with remainder 1<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Or put it in another way:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">101 = 4 * 25 + 1<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Python uses two operators <code>\/\/<\/code> and <code>% <\/code>that returns the result of the division:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">101 \/\/ 4 = 25\n101 % 4 = 1<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>\/\/<\/code> is called the <strong>floor division<\/strong> operator or div. And the <code>%<\/code> is called the <strong>modulo<\/strong> operator or mod.<\/p>\n\n\n\n<p>This tutorial focuses on the floor division operator. You&#8217;ll learn about the <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-modulo\/\">modulo operator in the following tutorial<\/a>.<\/p>\n\n\n\n<p>Both floor division and modulo operators satisfy the following equation:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">101 = 4 * (101 \/\/ 4) + (101 % 4)\n101 = 4 * 25         + 1<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Generally, if <code>N<\/code> is the numerator and <code>D<\/code> is the denominator, then the floor division and modulo operators always satisfy the following equation:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">N = D * ( N \/\/ D) + (N % D)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='the-floor-division-in-python'>The floor division in Python <a href=\"#the-floor-division-in-python\" class=\"anchor\" id=\"the-floor-division-in-python\" title=\"Anchor for The floor division in Python\">#<\/a><\/h2>\n\n\n\n<p>To understand the floor division, you first need to understand the floor of a real number.<\/p>\n\n\n\n<p>The floor of a real number is the largest integer less than or equal to the number. In other words:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">floor(r) = n, n is an integer and n &lt;= r <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the floor of 3.4 is 3 because 3 is the largest integer less than or equal to 3.4. The floor of 3.9 is also 3.  And the floor of 3 is 3 obviously:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">floor(3.4) = 4\nfloor(3.9) = 3\nfloor(3) = 3<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For the positive numbers, it would be easy to understand the definition. However, you should pay attention when it comes to negative numbers.<\/p>\n\n\n\n<p>For example, the floor of <code>-3.4<\/code> returns <code>-4<\/code>, not <code>-3<\/code> based on the floor definition. Similarly, the floor of <code>-3.9<\/code> also returns <code>-4<\/code> .<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">floor(-3.4) = -4\nfloor(-3.9) = -4\nfloor(-3) = -3<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The floor division can be defined as:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">n \/\/ d = floor(n\/d)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Notice that the floor division of a number is not always the same as truncation. The floor division is the same as truncation only when the numbers are positive.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-floor-division-operator-examples'>Python floor division operator examples <a href=\"#python-floor-division-operator-examples\" class=\"anchor\" id=\"python-floor-division-operator-examples\" title=\"Anchor for Python floor division operator examples\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the floor division operators with positive and negative integers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">a = 10\nb = 3\n\nprint(a\/\/b)  # 3\n\na = -10\nb = -3\n\nprint(a\/\/b)  # 3\n\na = 10\nb = -3\nprint(a\/\/b)  # -4\n\na = -10\nb = 3\nprint(a\/\/b)  # -4\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=YSA9IDEwCmIgPSAzCgpwcmludChhLy9iKSAgIyAzCgphID0gLTEwCmIgPSAtMwoKcHJpbnQoYS8vYikgICMgMwoKYSA9IDEwCmIgPSAtMwpwcmludChhLy9iKSAgIyAtNAoKYSA9IC0xMApiID0gMwpwcmludChhLy9iKSAgIyAtNA%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">3\n3\n-4\n-4<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following table illustrates the floor division of two integers <code>a<\/code> and <code>b<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>a<\/th><th>b<\/th><th>a \/\/ b<\/th><\/tr><\/thead><tbody><tr><td>10<\/td><td>3<\/td><td>3<\/td><\/tr><tr><td>-10<\/td><td>-3<\/td><td>3<\/td><\/tr><tr><td>10<\/td><td>-3<\/td><td>-4<\/td><\/tr><tr><td>-10<\/td><td>3<\/td><td>-4<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='python-math-floor-function'>Python math.floor() function <a href=\"#python-math-floor-function\" class=\"anchor\" id=\"python-math-floor-function\" title=\"Anchor for Python math.floor() function\">#<\/a><\/h2>\n\n\n\n<p>The <code>floor()<\/code> function of the <code>math<\/code> module returns the floor division of two integers. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> math <span class=\"hljs-keyword\">import<\/span> floor\n\na = <span class=\"hljs-number\">10<\/span>\nb = <span class=\"hljs-number\">3<\/span>\n\nprint(a\/\/b)  <span class=\"hljs-comment\"># 3<\/span>\nprint(floor(a\/b))  <span class=\"hljs-comment\"># 3<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZnJvbSBtYXRoIGltcG9ydCBmbG9vcgoKYSA9IDEwCmIgPSAzCgpwcmludChhLy9iKSAgIyAzCnByaW50KGZsb29yKGEvYikpICAjIDM%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">3\n3<\/code><\/span><\/pre>\n\n\n<p>The <code>floor()<\/code> function returns the same result as the floor division operator (<code>\/\/<\/code>). It&#8217;s also true for the negative numbers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> math <span class=\"hljs-keyword\">import<\/span> floor\n\na = <span class=\"hljs-number\">10<\/span>\nb = <span class=\"hljs-number\">-3<\/span>\n\nprint(a\/\/b)  <span class=\"hljs-comment\"># -4<\/span>\nprint(floor(a\/b))  <span class=\"hljs-comment\"># -4<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=ZnJvbSBtYXRoIGltcG9ydCBmbG9vcgoKYSA9IDEwCmIgPSAtMwoKcHJpbnQoYS8vYikgICMgLTQKcHJpbnQoZmxvb3IoYS9iKSkgICMgLTQ%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">-4\n-4<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python uses \/\/ as the floor division operator and <code>%<\/code> as the modulo operator.<\/li>\n\n\n\n<li>If the numerator is N and the denominator D, then this equation <code>N = D * ( N \/\/ D) + (N % D)<\/code> is always satisfied.<\/li>\n\n\n\n<li>Use floor division operator <code>\/\/<\/code> or the <code>floor()<\/code> function of the <code>math<\/code> module to get the floor division of two integers.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"909\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-floor-division\/\"\n\t\t\t\tdata-post-title=\"Python Floor Division\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"909\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-floor-division\/\"\n\t\t\t\tdata-post-title=\"Python Floor Division\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn about Python floor division operator (\/\/) or mod. Introduction to Python floor division # Suppose you have a division of two integers: In this division, 101 is called a numerator (N) and 4 is called a denominator (D). The integer division 101 \/ 4 returns 25 with the remainder [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":757,"menu_order":25,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-909","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/909","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=909"}],"version-history":[{"count":1,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/909\/revisions"}],"predecessor-version":[{"id":7136,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/909\/revisions\/7136"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/757"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}