[{"data":1,"prerenderedAt":733},["ShallowReactive",2],{"blog-\u002Fblog\u002Fpython-numbers-operators-explained":3},{"id":4,"title":5,"body":6,"description":718,"difficulty":719,"extension":720,"framework":721,"frameworkSlug":39,"meta":722,"navigation":723,"order":84,"path":724,"qaPath":725,"seo":726,"stem":727,"subtopic":728,"topic":729,"topicSlug":730,"updated":731,"__hash__":732},"blog\u002Fblog\u002Fpython-numbers-operators-explained.md","Python Numbers and Operators Explained — int, float, Floor Division, and Floats",{"type":7,"value":8,"toc":708},"minimark",[9,14,27,31,34,108,119,123,139,179,182,186,189,232,244,290,308,312,319,354,361,394,401,445,459,463,466,548,551,555,633,645,649,704],[10,11,13],"h2",{"id":12},"python-numbers-explained","Python numbers, explained",[15,16,17,18,22,23,26],"p",{},"Numbers feel simple until an interviewer asks why ",[19,20,21],"code",{},"0.1 + 0.2 != 0.3",", or what ",[19,24,25],{},"-7 \u002F\u002F 2","\nreturns. Python's numeric model has a few deliberate design choices — arbitrary-precision\nintegers, floor division, IEEE-754 floats — that are worth understanding properly.",[10,28,30],{"id":29},"the-built-in-numeric-types","The built-in numeric types",[15,32,33],{},"Python ships three numeric types:",[35,36,41],"pre",{"className":37,"code":38,"language":39,"meta":40,"style":40},"language-python shiki shiki-themes github-light github-dark","type(42)         # \u003Cclass 'int'>    — whole numbers, unbounded\ntype(3.14)       # \u003Cclass 'float'>  — IEEE-754 double precision\ntype(2 + 3j)     # \u003Cclass 'complex'> — real + imaginary parts\n","python","",[19,42,43,66,82],{"__ignoreMap":40},[44,45,48,52,56,59,62],"span",{"class":46,"line":47},"line",1,[44,49,51],{"class":50},"sj4cs","type",[44,53,55],{"class":54},"sVt8B","(",[44,57,58],{"class":50},"42",[44,60,61],{"class":54},")         ",[44,63,65],{"class":64},"sJ8bj","# \u003Cclass 'int'>    — whole numbers, unbounded\n",[44,67,69,71,73,76,79],{"class":46,"line":68},2,[44,70,51],{"class":50},[44,72,55],{"class":54},[44,74,75],{"class":50},"3.14",[44,77,78],{"class":54},")       ",[44,80,81],{"class":64},"# \u003Cclass 'float'>  — IEEE-754 double precision\n",[44,83,85,87,89,92,96,99,102,105],{"class":46,"line":84},3,[44,86,51],{"class":50},[44,88,55],{"class":54},[44,90,91],{"class":50},"2",[44,93,95],{"class":94},"szBVR"," +",[44,97,98],{"class":50}," 3",[44,100,101],{"class":94},"j",[44,103,104],{"class":54},")     ",[44,106,107],{"class":64},"# \u003Cclass 'complex'> — real + imaginary parts\n",[15,109,110,111,114,115,118],{},"There's also ",[19,112,113],{},"decimal.Decimal"," and ",[19,116,117],{},"fractions.Fraction"," in the standard library for exact\ndecimal and rational arithmetic when floats aren't precise enough.",[10,120,122],{"id":121},"integers-never-overflow","Integers never overflow",[15,124,125,126,129,130,134,135,138],{},"Unlike C or Java, Python ",[19,127,128],{},"int"," has ",[131,132,133],"strong",{},"arbitrary precision"," — it grows to fit the value,\nlimited only by memory. There is no ",[19,136,137],{},"INT_MAX",".",[35,140,142],{"className":37,"code":141,"language":39,"meta":40,"style":40},"2 ** 1000        # a 302-digit number, computed exactly\nimport math\nmath.factorial(50)   # 30414093201713378043612608166064768844377641568960512000000000000\n",[19,143,144,157,165],{"__ignoreMap":40},[44,145,146,148,151,154],{"class":46,"line":47},[44,147,91],{"class":50},[44,149,150],{"class":94}," **",[44,152,153],{"class":50}," 1000",[44,155,156],{"class":64},"        # a 302-digit number, computed exactly\n",[44,158,159,162],{"class":46,"line":68},[44,160,161],{"class":94},"import",[44,163,164],{"class":54}," math\n",[44,166,167,170,173,176],{"class":46,"line":84},[44,168,169],{"class":54},"math.factorial(",[44,171,172],{"class":50},"50",[44,174,175],{"class":54},")   ",[44,177,178],{"class":64},"# 30414093201713378043612608166064768844377641568960512000000000000\n",[15,180,181],{},"This is why Python is comfortable for cryptography and big-number math without special\nlibraries.",[10,183,185],{"id":184},"division-and","Division: \u002F, \u002F\u002F, and %",[15,187,188],{},"Python has three division-related operators, and the distinction matters:",[35,190,192],{"className":37,"code":191,"language":39,"meta":40,"style":40},"7 \u002F 2        # 3.5   — true division, always returns a float\n7 \u002F\u002F 2       # 3     — floor division, rounds toward negative infinity\n7 % 2        # 1     — modulo (remainder)\n",[19,193,194,208,220],{"__ignoreMap":40},[44,195,196,199,202,205],{"class":46,"line":47},[44,197,198],{"class":50},"7",[44,200,201],{"class":94}," \u002F",[44,203,204],{"class":50}," 2",[44,206,207],{"class":64},"        # 3.5   — true division, always returns a float\n",[44,209,210,212,215,217],{"class":46,"line":68},[44,211,198],{"class":50},[44,213,214],{"class":94}," \u002F\u002F",[44,216,204],{"class":50},[44,218,219],{"class":64},"       # 3     — floor division, rounds toward negative infinity\n",[44,221,222,224,227,229],{"class":46,"line":84},[44,223,198],{"class":50},[44,225,226],{"class":94}," %",[44,228,204],{"class":50},[44,230,231],{"class":64},"        # 1     — modulo (remainder)\n",[15,233,234,235,238,239,243],{},"The subtle part is ",[131,236,237],{},"negatives",". Floor division rounds ",[240,241,242],"em",{},"down"," (toward −∞), not toward\nzero:",[35,245,247],{"className":37,"code":246,"language":39,"meta":40,"style":40},"-7 \u002F\u002F 2      # -4   (not -3) — floored\n-7 % 2       # 1    — the sign follows the divisor\n7 % -2       # -1\n",[19,248,249,263,276],{"__ignoreMap":40},[44,250,251,254,256,258,260],{"class":46,"line":47},[44,252,253],{"class":94},"-",[44,255,198],{"class":50},[44,257,214],{"class":94},[44,259,204],{"class":50},[44,261,262],{"class":64},"      # -4   (not -3) — floored\n",[44,264,265,267,269,271,273],{"class":46,"line":68},[44,266,253],{"class":94},[44,268,198],{"class":50},[44,270,226],{"class":94},[44,272,204],{"class":50},[44,274,275],{"class":64},"       # 1    — the sign follows the divisor\n",[44,277,278,280,282,285,287],{"class":46,"line":84},[44,279,198],{"class":50},[44,281,226],{"class":94},[44,283,284],{"class":94}," -",[44,286,91],{"class":50},[44,288,289],{"class":64},"       # -1\n",[15,291,292,293,296,297,300,301,304,305,138],{},"The invariant always holds: ",[19,294,295],{},"(a \u002F\u002F b) * b + (a % b) == a",". ",[19,298,299],{},"divmod(a, b)"," returns both at\nonce: ",[19,302,303],{},"divmod(-7, 2)"," → ",[19,306,307],{},"(-4, 1)",[10,309,311],{"id":310},"floats-and-the-01-02-problem","Floats and the 0.1 + 0.2 problem",[15,313,314,315,318],{},"Floats are ",[131,316,317],{},"binary"," approximations (IEEE-754 doubles). Many decimal fractions can't be\nrepresented exactly in binary, so small errors creep in:",[35,320,322],{"className":37,"code":321,"language":39,"meta":40,"style":40},"0.1 + 0.2            # 0.30000000000000004\n0.1 + 0.2 == 0.3     # False\n",[19,323,324,337],{"__ignoreMap":40},[44,325,326,329,331,334],{"class":46,"line":47},[44,327,328],{"class":50},"0.1",[44,330,95],{"class":94},[44,332,333],{"class":50}," 0.2",[44,335,336],{"class":64},"            # 0.30000000000000004\n",[44,338,339,341,343,345,348,351],{"class":46,"line":68},[44,340,328],{"class":50},[44,342,95],{"class":94},[44,344,333],{"class":50},[44,346,347],{"class":94}," ==",[44,349,350],{"class":50}," 0.3",[44,352,353],{"class":64},"     # False\n",[15,355,356,357,360],{},"This isn't a Python bug — it's how floating point works everywhere. Don't compare floats\nwith ",[19,358,359],{},"==",". Instead use a tolerance:",[35,362,364],{"className":37,"code":363,"language":39,"meta":40,"style":40},"import math\nmath.isclose(0.1 + 0.2, 0.3)   # True\n",[19,365,366,372],{"__ignoreMap":40},[44,367,368,370],{"class":46,"line":47},[44,369,161],{"class":94},[44,371,164],{"class":54},[44,373,374,377,379,381,383,386,389,391],{"class":46,"line":68},[44,375,376],{"class":54},"math.isclose(",[44,378,328],{"class":50},[44,380,95],{"class":94},[44,382,333],{"class":50},[44,384,385],{"class":54},", ",[44,387,388],{"class":50},"0.3",[44,390,175],{"class":54},[44,392,393],{"class":64},"# True\n",[15,395,396,397,400],{},"For money and other exact-decimal needs, use ",[19,398,399],{},"Decimal",":",[35,402,404],{"className":37,"code":403,"language":39,"meta":40,"style":40},"from decimal import Decimal\nDecimal(\"0.1\") + Decimal(\"0.2\")   # Decimal('0.3')  — exact\n",[19,405,406,419],{"__ignoreMap":40},[44,407,408,411,414,416],{"class":46,"line":47},[44,409,410],{"class":94},"from",[44,412,413],{"class":54}," decimal ",[44,415,161],{"class":94},[44,417,418],{"class":54}," Decimal\n",[44,420,421,424,428,431,434,437,440,442],{"class":46,"line":68},[44,422,423],{"class":54},"Decimal(",[44,425,427],{"class":426},"sZZnC","\"0.1\"",[44,429,430],{"class":54},") ",[44,432,433],{"class":94},"+",[44,435,436],{"class":54}," Decimal(",[44,438,439],{"class":426},"\"0.2\"",[44,441,175],{"class":54},[44,443,444],{"class":64},"# Decimal('0.3')  — exact\n",[15,446,447,448,451,452,454,455,458],{},"Note you pass a ",[131,449,450],{},"string"," to ",[19,453,399],{},"; ",[19,456,457],{},"Decimal(0.1)"," would inherit the float's error.",[10,460,462],{"id":461},"bitwise-operators","Bitwise operators",[15,464,465],{},"For integers, Python provides the usual bit-level operators:",[35,467,469],{"className":37,"code":468,"language":39,"meta":40,"style":40},"5 & 3        # 1   — AND\n5 | 3        # 7   — OR\n5 ^ 3        # 6   — XOR\n~5           # -6  — NOT (two's complement: ~x == -x - 1)\n1 \u003C\u003C 4       # 16  — left shift (multiply by 2**4)\n32 >> 2      # 8   — right shift\n",[19,470,471,484,496,508,519,534],{"__ignoreMap":40},[44,472,473,476,479,481],{"class":46,"line":47},[44,474,475],{"class":50},"5",[44,477,478],{"class":94}," &",[44,480,98],{"class":50},[44,482,483],{"class":64},"        # 1   — AND\n",[44,485,486,488,491,493],{"class":46,"line":68},[44,487,475],{"class":50},[44,489,490],{"class":94}," |",[44,492,98],{"class":50},[44,494,495],{"class":64},"        # 7   — OR\n",[44,497,498,500,503,505],{"class":46,"line":84},[44,499,475],{"class":50},[44,501,502],{"class":94}," ^",[44,504,98],{"class":50},[44,506,507],{"class":64},"        # 6   — XOR\n",[44,509,511,514,516],{"class":46,"line":510},4,[44,512,513],{"class":94},"~",[44,515,475],{"class":50},[44,517,518],{"class":64},"           # -6  — NOT (two's complement: ~x == -x - 1)\n",[44,520,522,525,528,531],{"class":46,"line":521},5,[44,523,524],{"class":50},"1",[44,526,527],{"class":94}," \u003C\u003C",[44,529,530],{"class":50}," 4",[44,532,533],{"class":64},"       # 16  — left shift (multiply by 2**4)\n",[44,535,537,540,543,545],{"class":46,"line":536},6,[44,538,539],{"class":50},"32",[44,541,542],{"class":94}," >>",[44,544,204],{"class":50},[44,546,547],{"class":64},"      # 8   — right shift\n",[15,549,550],{},"These work on Python's arbitrary-precision integers too, so shifting never overflows.",[10,552,554],{"id":553},"exponentiation-and-other-helpers","Exponentiation and other helpers",[35,556,558],{"className":37,"code":557,"language":39,"meta":40,"style":40},"2 ** 10          # 1024 — power\npow(2, 10, 1000) # 24   — (2**10) % 1000, computed efficiently\nabs(-5)          # 5\nround(2.675, 2)  # 2.67 — banker's rounding + float imprecision, careful!\n",[19,559,560,572,596,613],{"__ignoreMap":40},[44,561,562,564,566,569],{"class":46,"line":47},[44,563,91],{"class":50},[44,565,150],{"class":94},[44,567,568],{"class":50}," 10",[44,570,571],{"class":64},"          # 1024 — power\n",[44,573,574,577,579,581,583,586,588,591,593],{"class":46,"line":68},[44,575,576],{"class":50},"pow",[44,578,55],{"class":54},[44,580,91],{"class":50},[44,582,385],{"class":54},[44,584,585],{"class":50},"10",[44,587,385],{"class":54},[44,589,590],{"class":50},"1000",[44,592,430],{"class":54},[44,594,595],{"class":64},"# 24   — (2**10) % 1000, computed efficiently\n",[44,597,598,601,603,605,607,610],{"class":46,"line":84},[44,599,600],{"class":50},"abs",[44,602,55],{"class":54},[44,604,253],{"class":94},[44,606,475],{"class":50},[44,608,609],{"class":54},")          ",[44,611,612],{"class":64},"# 5\n",[44,614,615,618,620,623,625,627,630],{"class":46,"line":510},[44,616,617],{"class":50},"round",[44,619,55],{"class":54},[44,621,622],{"class":50},"2.675",[44,624,385],{"class":54},[44,626,91],{"class":50},[44,628,629],{"class":54},")  ",[44,631,632],{"class":64},"# 2.67 — banker's rounding + float imprecision, careful!\n",[15,634,635,637,638,641,642,644],{},[19,636,617],{}," uses ",[131,639,640],{},"banker's rounding"," (round-half-to-even), and combined with float\nimprecision it can surprise you — another reason to reach for ",[19,643,399],{}," when exactness\nmatters.",[10,646,648],{"id":647},"recap","Recap",[15,650,651,652,654,655,658,659,662,663,666,667,670,671,674,675,677,678,681,682,685,686,688,689,692,693,696,697,385,700,703],{},"Python has ",[19,653,128],{}," (arbitrary precision — ",[131,656,657],{},"no overflow","), ",[19,660,661],{},"float"," (IEEE-754 doubles), and\n",[19,664,665],{},"complex",". True division ",[19,668,669],{},"\u002F"," always yields a float; floor division ",[19,672,673],{},"\u002F\u002F"," rounds toward\nnegative infinity, so ",[19,676,25],{}," is ",[19,679,680],{},"-4",", and ",[19,683,684],{},"%"," takes the sign of the divisor. Floats are\nbinary approximations, so ",[19,687,21],{}," — compare with ",[19,690,691],{},"math.isclose"," and use\n",[19,694,695],{},"decimal.Decimal(\"...\")"," for exact decimal arithmetic. ",[19,698,699],{},"divmod",[19,701,702],{},"pow(x, y, mod)",", and the\nbitwise operators round out the toolkit.",[705,706,707],"style",{},"html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":40,"searchDepth":68,"depth":68,"links":709},[710,711,712,713,714,715,716,717],{"id":12,"depth":68,"text":13},{"id":29,"depth":68,"text":30},{"id":121,"depth":68,"text":122},{"id":184,"depth":68,"text":185},{"id":310,"depth":68,"text":311},{"id":461,"depth":68,"text":462},{"id":553,"depth":68,"text":554},{"id":647,"depth":68,"text":648},"Python's numeric types and operators — why integers never overflow, how floor division and modulo behave with negatives, and why 0.1 + 0.2 isn't 0.3.","easy","md","Python",{},true,"\u002Fblog\u002Fpython-numbers-operators-explained","\u002Fpython\u002Ffundamentals\u002Fnumbers-operators",{"title":5,"description":718},"blog\u002Fpython-numbers-operators-explained","Numbers & Operators","Fundamentals","fundamentals","2026-06-19","NUsZ5kyXlRgHm3sG1VhWG6j7wRMHeTqJHX9-kjdIVDQ",1782244093551]