[{"data":1,"prerenderedAt":1671},["ShallowReactive",2],{"doc-\u002Freference\u002Fpython-abs-function-explained":3},{"id":4,"title":5,"body":6,"description":1664,"extension":1665,"meta":1666,"navigation":316,"path":1667,"seo":1668,"stem":1669,"__hash__":1670},"content\u002Freference\u002Fpython-abs-function-explained.md","Python abs() Function Explained",{"type":7,"value":8,"toc":1636},"minimark",[9,13,22,25,38,41,46,128,131,151,157,161,166,169,185,188,214,217,221,224,242,245,272,275,341,343,358,368,380,384,387,441,443,463,466,490,498,502,507,513,515,554,556,565,572,590,593,605,609,614,619,622,643,645,703,705,713,716,720,752,754,762,765,769,774,822,824,832,836,896,898,906,909,913,917,920,946,955,972,1024,1026,1041,1052,1056,1059,1091,1098,1102,1107,1148,1150,1161,1164,1207,1209,1217,1221,1226,1248,1250,1259,1268,1272,1277,1299,1301,1372,1374,1426,1429,1432,1435,1517,1519,1550,1554,1558,1561,1565,1568,1572,1575,1579,1591,1595,1598,1602,1632],[10,11,5],"h1",{"id":12},"python-abs-function-explained",[14,15,16,17,21],"p",{},"The ",[18,19,20],"code",{},"abs()"," function returns the absolute value of a number.",[14,23,24],{},"Beginners usually use it when they want:",[26,27,28,32,35],"ul",{},[29,30,31],"li",{},"a number without its negative sign",[29,33,34],{},"the distance between two numbers",[29,36,37],{},"the size of a value without caring whether it is positive or negative",[14,39,40],{},"It works with common Python number types such as integers, floats, and complex numbers.",[42,43,45],"h2",{"id":44},"quick-example","Quick example",[47,48,53],"pre",{"className":49,"code":50,"language":51,"meta":52,"style":52},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","print(abs(-7))\nprint(abs(3.5))\nprint(abs(-2+3j))\n","python","",[18,54,55,84,100],{"__ignoreMap":52},[56,57,60,64,68,71,73,77,81],"span",{"class":58,"line":59},"line",1,[56,61,63],{"class":62},"sptTA","print",[56,65,67],{"class":66},"sP7_E","(",[56,69,70],{"class":62},"abs",[56,72,67],{"class":66},[56,74,76],{"class":75},"smGrS","-",[56,78,80],{"class":79},"srdBf","7",[56,82,83],{"class":66},"))\n",[56,85,87,89,91,93,95,98],{"class":58,"line":86},2,[56,88,63],{"class":62},[56,90,67],{"class":66},[56,92,70],{"class":62},[56,94,67],{"class":66},[56,96,97],{"class":79},"3.5",[56,99,83],{"class":66},[56,101,103,105,107,109,111,113,116,119,122,126],{"class":58,"line":102},3,[56,104,63],{"class":62},[56,106,67],{"class":66},[56,108,70],{"class":62},[56,110,67],{"class":66},[56,112,76],{"class":75},[56,114,115],{"class":79},"2",[56,117,118],{"class":75},"+",[56,120,121],{"class":79},"3",[56,123,125],{"class":124},"sbsja","j",[56,127,83],{"class":66},[14,129,130],{},"Output:",[47,132,134],{"className":49,"code":133,"language":51,"meta":52,"style":52},"7\n3.5\n3.605551275463989\n",[18,135,136,141,146],{"__ignoreMap":52},[56,137,138],{"class":58,"line":59},[56,139,140],{"class":79},"7\n",[56,142,143],{"class":58,"line":86},[56,144,145],{"class":79},"3.5\n",[56,147,148],{"class":58,"line":102},[56,149,150],{"class":79},"3.605551275463989\n",[14,152,153,154,156],{},"Use ",[18,155,20],{}," to get the absolute value of a number. For complex numbers, it returns the magnitude.",[42,158,160],{"id":159},"what-abs-does","What abs() does",[14,162,163,165],{},[18,164,20],{}," returns the absolute value of a number.",[14,167,168],{},"That means:",[26,170,171,174,177],{},[29,172,173],{},"negative integers and floats become positive",[29,175,176],{},"positive numbers stay the same",[29,178,179,182,183],{},[18,180,181],{},"0"," stays ",[18,184,181],{},[14,186,187],{},"Examples:",[26,189,190,199,207],{},[29,191,192,195,196],{},[18,193,194],{},"abs(-5)"," gives ",[18,197,198],{},"5",[29,200,201,195,204],{},[18,202,203],{},"abs(9)",[18,205,206],{},"9",[29,208,209,195,212],{},[18,210,211],{},"abs(0)",[18,213,181],{},[14,215,216],{},"This is useful when you care about how large a number is, but not whether it is positive or negative.",[42,218,220],{"id":219},"basic-syntax","Basic syntax",[14,222,223],{},"The syntax is:",[47,225,227],{"className":49,"code":226,"language":51,"meta":52,"style":52},"abs(x)\n",[18,228,229],{"__ignoreMap":52},[56,230,231,233,235,239],{"class":58,"line":59},[56,232,70],{"class":62},[56,234,67],{"class":66},[56,236,238],{"class":237},"slqww","x",[56,240,241],{"class":66},")\n",[14,243,244],{},"Here:",[26,246,247,264,269],{},[29,248,249,251,252,255,256,259,260,263],{},[18,250,238],{}," is usually an ",[18,253,254],{},"int",", ",[18,257,258],{},"float",", or ",[18,261,262],{},"complex"," number",[29,265,266,268],{},[18,267,20],{}," returns a new value",[29,270,271],{},"it does not change the original variable",[14,273,274],{},"Example:",[47,276,278],{"className":49,"code":277,"language":51,"meta":52,"style":52},"number = -12\nresult = abs(number)\n\nprint(number)\nprint(result)\n",[18,279,280,295,312,318,329],{"__ignoreMap":52},[56,281,282,286,289,292],{"class":58,"line":59},[56,283,285],{"class":284},"su5hD","number ",[56,287,288],{"class":75},"=",[56,290,291],{"class":75}," -",[56,293,294],{"class":79},"12\n",[56,296,297,300,302,305,307,310],{"class":58,"line":86},[56,298,299],{"class":284},"result ",[56,301,288],{"class":75},[56,303,304],{"class":62}," abs",[56,306,67],{"class":66},[56,308,309],{"class":237},"number",[56,311,241],{"class":66},[56,313,314],{"class":58,"line":102},[56,315,317],{"emptyLinePlaceholder":316},true,"\n",[56,319,321,323,325,327],{"class":58,"line":320},4,[56,322,63],{"class":62},[56,324,67],{"class":66},[56,326,309],{"class":237},[56,328,241],{"class":66},[56,330,332,334,336,339],{"class":58,"line":331},5,[56,333,63],{"class":62},[56,335,67],{"class":66},[56,337,338],{"class":237},"result",[56,340,241],{"class":66},[14,342,130],{},[47,344,346],{"className":49,"code":345,"language":51,"meta":52,"style":52},"-12\n12\n",[18,347,348,354],{"__ignoreMap":52},[56,349,350,352],{"class":58,"line":59},[56,351,76],{"class":75},[56,353,294],{"class":79},[56,355,356],{"class":58,"line":86},[56,357,294],{"class":79},[14,359,360,361,363,364,367],{},"Notice that ",[18,362,309],{}," is still ",[18,365,366],{},"-12",". If you want to keep the absolute value, you must store it in a variable.",[14,369,370,371,379],{},"If you are not sure what type a value has, the ",[372,373,375,378],"a",{"href":374},"\u002Freference\u002Fpython-type-function-explained\u002F",[18,376,377],{},"type()"," function"," can help.",[42,381,383],{"id":382},"examples-with-integers-and-floats","Examples with integers and floats",[14,385,386],{},"Here are some basic examples:",[47,388,390],{"className":49,"code":389,"language":51,"meta":52,"style":52},"print(abs(-10))\nprint(abs(8))\nprint(abs(-4.2))\n",[18,391,392,409,424],{"__ignoreMap":52},[56,393,394,396,398,400,402,404,407],{"class":58,"line":59},[56,395,63],{"class":62},[56,397,67],{"class":66},[56,399,70],{"class":62},[56,401,67],{"class":66},[56,403,76],{"class":75},[56,405,406],{"class":79},"10",[56,408,83],{"class":66},[56,410,411,413,415,417,419,422],{"class":58,"line":86},[56,412,63],{"class":62},[56,414,67],{"class":66},[56,416,70],{"class":62},[56,418,67],{"class":66},[56,420,421],{"class":79},"8",[56,423,83],{"class":66},[56,425,426,428,430,432,434,436,439],{"class":58,"line":102},[56,427,63],{"class":62},[56,429,67],{"class":66},[56,431,70],{"class":62},[56,433,67],{"class":66},[56,435,76],{"class":75},[56,437,438],{"class":79},"4.2",[56,440,83],{"class":66},[14,442,130],{},[47,444,446],{"className":49,"code":445,"language":51,"meta":52,"style":52},"10\n8\n4.2\n",[18,447,448,453,458],{"__ignoreMap":52},[56,449,450],{"class":58,"line":59},[56,451,452],{"class":79},"10\n",[56,454,455],{"class":58,"line":86},[56,456,457],{"class":79},"8\n",[56,459,460],{"class":58,"line":102},[56,461,462],{"class":79},"4.2\n",[14,464,465],{},"What each line does:",[26,467,468,476,483],{},[29,469,470,473,474],{},[18,471,472],{},"abs(-10)"," returns ",[18,475,406],{},[29,477,478,473,481],{},[18,479,480],{},"abs(8)",[18,482,421],{},[29,484,485,473,488],{},[18,486,487],{},"abs(-4.2)",[18,489,438],{},[14,491,492,493,497],{},"This works because integers and floats are standard numeric types in Python. If you want a refresher, see ",[372,494,496],{"href":495},"\u002Flearn\u002Fpython-numbers-explained-int-float-complex\u002F","Python numbers explained: int, float, complex",".",[42,499,501],{"id":500},"example-with-complex-numbers","Example with complex numbers",[14,503,504,506],{},[18,505,20],{}," also works with complex numbers.",[14,508,509,510,512],{},"For a complex number, it returns the distance from ",[18,511,181],{}," in the complex plane. You do not need advanced math to use this. In practice, it gives the size, or magnitude, of the complex number.",[14,514,274],{},[47,516,518],{"className":49,"code":517,"language":51,"meta":52,"style":52},"value = 3 + 4j\nprint(abs(value))\n",[18,519,520,539],{"__ignoreMap":52},[56,521,522,525,527,530,533,536],{"class":58,"line":59},[56,523,524],{"class":284},"value ",[56,526,288],{"class":75},[56,528,529],{"class":79}," 3",[56,531,532],{"class":75}," +",[56,534,535],{"class":79}," 4",[56,537,538],{"class":124},"j\n",[56,540,541,543,545,547,549,552],{"class":58,"line":86},[56,542,63],{"class":62},[56,544,67],{"class":66},[56,546,70],{"class":62},[56,548,67],{"class":66},[56,550,551],{"class":237},"value",[56,553,83],{"class":66},[14,555,130],{},[47,557,559],{"className":49,"code":558,"language":51,"meta":52,"style":52},"5.0\n",[18,560,561],{"__ignoreMap":52},[56,562,563],{"class":58,"line":59},[56,564,558],{"class":79},[14,566,567,568,571],{},"Why ",[18,569,570],{},"5.0","?",[26,573,574,579,585],{},[29,575,576,577],{},"the real part is ",[18,578,121],{},[29,580,581,582],{},"the imaginary part is ",[18,583,584],{},"4",[29,586,587,588],{},"the magnitude is ",[18,589,570],{},[14,591,592],{},"For beginners, the main thing to remember is simple:",[26,594,595,602],{},[29,596,597,599,600],{},[18,598,20],{}," on a complex number returns a ",[18,601,258],{},[29,603,604],{},"it gives the number's magnitude, not a complex number back",[42,606,608],{"id":607},"common-beginner-uses","Common beginner uses",[14,610,611,612,497],{},"Here are some common ways beginners use ",[18,613,20],{},[615,616,618],"h3",{"id":617},"finding-the-distance-between-two-numbers","Finding the distance between two numbers",[14,620,621],{},"A very common pattern is:",[47,623,625],{"className":49,"code":624,"language":51,"meta":52,"style":52},"abs(a - b)\n",[18,626,627],{"__ignoreMap":52},[56,628,629,631,633,636,638,641],{"class":58,"line":59},[56,630,70],{"class":62},[56,632,67],{"class":66},[56,634,635],{"class":237},"a ",[56,637,76],{"class":75},[56,639,640],{"class":237}," b",[56,642,241],{"class":66},[14,644,274],{},[47,646,648],{"className":49,"code":647,"language":51,"meta":52,"style":52},"a = 3\nb = 10\n\ndistance = abs(a - b)\nprint(distance)\n",[18,649,650,659,669,673,692],{"__ignoreMap":52},[56,651,652,654,656],{"class":58,"line":59},[56,653,635],{"class":284},[56,655,288],{"class":75},[56,657,658],{"class":79}," 3\n",[56,660,661,664,666],{"class":58,"line":86},[56,662,663],{"class":284},"b ",[56,665,288],{"class":75},[56,667,668],{"class":79}," 10\n",[56,670,671],{"class":58,"line":102},[56,672,317],{"emptyLinePlaceholder":316},[56,674,675,678,680,682,684,686,688,690],{"class":58,"line":320},[56,676,677],{"class":284},"distance ",[56,679,288],{"class":75},[56,681,304],{"class":62},[56,683,67],{"class":66},[56,685,635],{"class":237},[56,687,76],{"class":75},[56,689,640],{"class":237},[56,691,241],{"class":66},[56,693,694,696,698,701],{"class":58,"line":331},[56,695,63],{"class":62},[56,697,67],{"class":66},[56,699,700],{"class":237},"distance",[56,702,241],{"class":66},[14,704,130],{},[47,706,707],{"className":49,"code":140,"language":51,"meta":52,"style":52},[18,708,709],{"__ignoreMap":52},[56,710,711],{"class":58,"line":59},[56,712,140],{"class":79},[14,714,715],{},"This tells you how far apart two numbers are.",[615,717,719],{"id":718},"comparing-how-far-a-value-is-from-zero","Comparing how far a value is from zero",[47,721,723],{"className":49,"code":722,"language":51,"meta":52,"style":52},"temperature_change = -6\nprint(abs(temperature_change))\n",[18,724,725,737],{"__ignoreMap":52},[56,726,727,730,732,734],{"class":58,"line":59},[56,728,729],{"class":284},"temperature_change ",[56,731,288],{"class":75},[56,733,291],{"class":75},[56,735,736],{"class":79},"6\n",[56,738,739,741,743,745,747,750],{"class":58,"line":86},[56,740,63],{"class":62},[56,742,67],{"class":66},[56,744,70],{"class":62},[56,746,67],{"class":66},[56,748,749],{"class":237},"temperature_change",[56,751,83],{"class":66},[14,753,130],{},[47,755,756],{"className":49,"code":736,"language":51,"meta":52,"style":52},[18,757,758],{"__ignoreMap":52},[56,759,760],{"class":58,"line":59},[56,761,736],{"class":79},[14,763,764],{},"This is useful when the size of the change matters more than the direction.",[615,766,768],{"id":767},"working-with-negative-input-values-safely","Working with negative input values safely",[14,770,771,772,379],{},"If a user enters a negative number but your program needs a positive value, ",[18,773,20],{},[47,775,777],{"className":49,"code":776,"language":51,"meta":52,"style":52},"balance_change = -25\nsafe_value = abs(balance_change)\n\nprint(safe_value)\n",[18,778,779,791,807,811],{"__ignoreMap":52},[56,780,781,784,786,788],{"class":58,"line":59},[56,782,783],{"class":284},"balance_change ",[56,785,288],{"class":75},[56,787,291],{"class":75},[56,789,790],{"class":79},"25\n",[56,792,793,796,798,800,802,805],{"class":58,"line":86},[56,794,795],{"class":284},"safe_value ",[56,797,288],{"class":75},[56,799,304],{"class":62},[56,801,67],{"class":66},[56,803,804],{"class":237},"balance_change",[56,806,241],{"class":66},[56,808,809],{"class":58,"line":102},[56,810,317],{"emptyLinePlaceholder":316},[56,812,813,815,817,820],{"class":58,"line":320},[56,814,63],{"class":62},[56,816,67],{"class":66},[56,818,819],{"class":237},"safe_value",[56,821,241],{"class":66},[14,823,130],{},[47,825,826],{"className":49,"code":790,"language":51,"meta":52,"style":52},[18,827,828],{"__ignoreMap":52},[56,829,830],{"class":58,"line":59},[56,831,790],{"class":79},[615,833,835],{"id":834},"checking-the-absolute-difference-in-simple-programs","Checking the absolute difference in simple programs",[47,837,839],{"className":49,"code":838,"language":51,"meta":52,"style":52},"guess = 42\ntarget = 50\n\ndifference = abs(guess - target)\nprint(difference)\n",[18,840,841,851,861,865,885],{"__ignoreMap":52},[56,842,843,846,848],{"class":58,"line":59},[56,844,845],{"class":284},"guess ",[56,847,288],{"class":75},[56,849,850],{"class":79}," 42\n",[56,852,853,856,858],{"class":58,"line":86},[56,854,855],{"class":284},"target ",[56,857,288],{"class":75},[56,859,860],{"class":79}," 50\n",[56,862,863],{"class":58,"line":102},[56,864,317],{"emptyLinePlaceholder":316},[56,866,867,870,872,874,876,878,880,883],{"class":58,"line":320},[56,868,869],{"class":284},"difference ",[56,871,288],{"class":75},[56,873,304],{"class":62},[56,875,67],{"class":66},[56,877,845],{"class":237},[56,879,76],{"class":75},[56,881,882],{"class":237}," target",[56,884,241],{"class":66},[56,886,887,889,891,894],{"class":58,"line":331},[56,888,63],{"class":62},[56,890,67],{"class":66},[56,892,893],{"class":237},"difference",[56,895,241],{"class":66},[14,897,130],{},[47,899,900],{"className":49,"code":457,"language":51,"meta":52,"style":52},[18,901,902],{"__ignoreMap":52},[56,903,904],{"class":58,"line":59},[56,905,457],{"class":79},[14,907,908],{},"This is useful in guessing games, scoring programs, and simple comparisons.",[42,910,912],{"id":911},"common-mistakes","Common mistakes",[615,914,916],{"id":915},"passing-a-string-to-abs","Passing a string to abs()",[14,918,919],{},"This causes an error:",[47,921,923],{"className":49,"code":922,"language":51,"meta":52,"style":52},"print(abs(\"5\"))\n",[18,924,925],{"__ignoreMap":52},[56,926,927,929,931,933,935,939,942,944],{"class":58,"line":59},[56,928,63],{"class":62},[56,930,67],{"class":66},[56,932,70],{"class":62},[56,934,67],{"class":66},[56,936,938],{"class":937},"sjJ54","\"",[56,940,198],{"class":941},"s_sjI",[56,943,938],{"class":937},[56,945,83],{"class":66},[14,947,948,951,952,954],{},[18,949,950],{},"\"5\""," is a string, not a number. ",[18,953,20],{}," needs a numeric value.",[14,956,957,958,964,965,971],{},"Fix it by converting the string first with ",[372,959,961],{"href":960},"\u002Freference\u002Fpython-int-function-explained\u002F",[18,962,963],{},"int()"," or ",[372,966,968],{"href":967},"\u002Freference\u002Fpython-float-function-explained\u002F",[18,969,970],{},"float()",":",[47,973,975],{"className":49,"code":974,"language":51,"meta":52,"style":52},"print(abs(int(\"5\")))\nprint(abs(float(\"-3.2\")))\n",[18,976,977,1001],{"__ignoreMap":52},[56,978,979,981,983,985,987,990,992,994,996,998],{"class":58,"line":59},[56,980,63],{"class":62},[56,982,67],{"class":66},[56,984,70],{"class":62},[56,986,67],{"class":66},[56,988,254],{"class":989},"sZMiF",[56,991,67],{"class":66},[56,993,938],{"class":937},[56,995,198],{"class":941},[56,997,938],{"class":937},[56,999,1000],{"class":66},")))\n",[56,1002,1003,1005,1007,1009,1011,1013,1015,1017,1020,1022],{"class":58,"line":86},[56,1004,63],{"class":62},[56,1006,67],{"class":66},[56,1008,70],{"class":62},[56,1010,67],{"class":66},[56,1012,258],{"class":989},[56,1014,67],{"class":66},[56,1016,938],{"class":937},[56,1018,1019],{"class":941},"-3.2",[56,1021,938],{"class":937},[56,1023,1000],{"class":66},[14,1025,130],{},[47,1027,1029],{"className":49,"code":1028,"language":51,"meta":52,"style":52},"5\n3.2\n",[18,1030,1031,1036],{"__ignoreMap":52},[56,1032,1033],{"class":58,"line":59},[56,1034,1035],{"class":79},"5\n",[56,1037,1038],{"class":58,"line":86},[56,1039,1040],{"class":79},"3.2\n",[14,1042,1043,1044,1047,1048,497],{},"If you are working with keyboard input, this mistake often happens because ",[18,1045,1046],{},"input()"," always returns a string. See ",[372,1049,1051],{"href":1050},"\u002Fhow-to\u002Fhow-to-convert-string-to-int-in-python\u002F","how to convert a string to an int in Python",[615,1053,1055],{"id":1054},"passing-a-list-or-dictionary","Passing a list or dictionary",[14,1057,1058],{},"Unsupported types also cause an error:",[47,1060,1062],{"className":49,"code":1061,"language":51,"meta":52,"style":52},"print(abs([1, 2, 3]))\n",[18,1063,1064],{"__ignoreMap":52},[56,1065,1066,1068,1070,1072,1075,1078,1081,1084,1086,1088],{"class":58,"line":59},[56,1067,63],{"class":62},[56,1069,67],{"class":66},[56,1071,70],{"class":62},[56,1073,1074],{"class":66},"([",[56,1076,1077],{"class":79},"1",[56,1079,1080],{"class":66},",",[56,1082,1083],{"class":79}," 2",[56,1085,1080],{"class":66},[56,1087,529],{"class":79},[56,1089,1090],{"class":66},"]))\n",[14,1092,1093,1094,1097],{},"This raises a ",[18,1095,1096],{},"TypeError"," because lists do not have an absolute value.",[615,1099,1101],{"id":1100},"assuming-abs-changes-the-original-variable","Assuming abs() changes the original variable",[14,1103,1104,1106],{},[18,1105,20],{}," does not update the variable by itself.",[47,1108,1110],{"className":49,"code":1109,"language":51,"meta":52,"style":52},"x = -9\nabs(x)\n\nprint(x)\n",[18,1111,1112,1124,1134,1138],{"__ignoreMap":52},[56,1113,1114,1117,1119,1121],{"class":58,"line":59},[56,1115,1116],{"class":284},"x ",[56,1118,288],{"class":75},[56,1120,291],{"class":75},[56,1122,1123],{"class":79},"9\n",[56,1125,1126,1128,1130,1132],{"class":58,"line":86},[56,1127,70],{"class":62},[56,1129,67],{"class":66},[56,1131,238],{"class":237},[56,1133,241],{"class":66},[56,1135,1136],{"class":58,"line":102},[56,1137,317],{"emptyLinePlaceholder":316},[56,1139,1140,1142,1144,1146],{"class":58,"line":320},[56,1141,63],{"class":62},[56,1143,67],{"class":66},[56,1145,238],{"class":237},[56,1147,241],{"class":66},[14,1149,130],{},[47,1151,1153],{"className":49,"code":1152,"language":51,"meta":52,"style":52},"-9\n",[18,1154,1155],{"__ignoreMap":52},[56,1156,1157,1159],{"class":58,"line":59},[56,1158,76],{"class":75},[56,1160,1123],{"class":79},[14,1162,1163],{},"If you want to store the result:",[47,1165,1167],{"className":49,"code":1166,"language":51,"meta":52,"style":52},"x = -9\nx = abs(x)\n\nprint(x)\n",[18,1168,1169,1179,1193,1197],{"__ignoreMap":52},[56,1170,1171,1173,1175,1177],{"class":58,"line":59},[56,1172,1116],{"class":284},[56,1174,288],{"class":75},[56,1176,291],{"class":75},[56,1178,1123],{"class":79},[56,1180,1181,1183,1185,1187,1189,1191],{"class":58,"line":86},[56,1182,1116],{"class":284},[56,1184,288],{"class":75},[56,1186,304],{"class":62},[56,1188,67],{"class":66},[56,1190,238],{"class":237},[56,1192,241],{"class":66},[56,1194,1195],{"class":58,"line":102},[56,1196,317],{"emptyLinePlaceholder":316},[56,1198,1199,1201,1203,1205],{"class":58,"line":320},[56,1200,63],{"class":62},[56,1202,67],{"class":66},[56,1204,238],{"class":237},[56,1206,241],{"class":66},[14,1208,130],{},[47,1210,1211],{"className":49,"code":1123,"language":51,"meta":52,"style":52},[18,1212,1213],{"__ignoreMap":52},[56,1214,1215],{"class":58,"line":59},[56,1216,1123],{"class":79},[615,1218,1220],{"id":1219},"confusing-absolute-value-with-rounding","Confusing absolute value with rounding",[14,1222,1223,1225],{},[18,1224,20],{}," does not round numbers.",[47,1227,1229],{"className":49,"code":1228,"language":51,"meta":52,"style":52},"print(abs(-4.7))\n",[18,1230,1231],{"__ignoreMap":52},[56,1232,1233,1235,1237,1239,1241,1243,1246],{"class":58,"line":59},[56,1234,63],{"class":62},[56,1236,67],{"class":66},[56,1238,70],{"class":62},[56,1240,67],{"class":66},[56,1242,76],{"class":75},[56,1244,1245],{"class":79},"4.7",[56,1247,83],{"class":66},[14,1249,130],{},[47,1251,1253],{"className":49,"code":1252,"language":51,"meta":52,"style":52},"4.7\n",[18,1254,1255],{"__ignoreMap":52},[56,1256,1257],{"class":58,"line":59},[56,1258,1252],{"class":79},[14,1260,1261,1262,1264,1265,1267],{},"If you expected ",[18,1263,198],{},", that is a different operation. ",[18,1266,20],{}," removes the negative sign. It does not change the decimal part.",[42,1269,1271],{"id":1270},"return-values-and-types","Return values and types",[14,1273,1274,1276],{},[18,1275,20],{}," returns different types depending on the input:",[26,1278,1279,1286,1293],{},[29,1280,1281,1283,1284],{},[18,1282,254],{}," input → returns an ",[18,1285,254],{},[29,1287,1288,1290,1291],{},[18,1289,258],{}," input → returns a ",[18,1292,258],{},[29,1294,1295,1290,1297],{},[18,1296,262],{},[18,1298,258],{},[14,1300,274],{},[47,1302,1304],{"className":49,"code":1303,"language":51,"meta":52,"style":52},"print(type(abs(-5)))\nprint(type(abs(-3.2)))\nprint(type(abs(3 + 4j)))\n",[18,1305,1306,1327,1348],{"__ignoreMap":52},[56,1307,1308,1310,1312,1315,1317,1319,1321,1323,1325],{"class":58,"line":59},[56,1309,63],{"class":62},[56,1311,67],{"class":66},[56,1313,1314],{"class":989},"type",[56,1316,67],{"class":66},[56,1318,70],{"class":62},[56,1320,67],{"class":66},[56,1322,76],{"class":75},[56,1324,198],{"class":79},[56,1326,1000],{"class":66},[56,1328,1329,1331,1333,1335,1337,1339,1341,1343,1346],{"class":58,"line":86},[56,1330,63],{"class":62},[56,1332,67],{"class":66},[56,1334,1314],{"class":989},[56,1336,67],{"class":66},[56,1338,70],{"class":62},[56,1340,67],{"class":66},[56,1342,76],{"class":75},[56,1344,1345],{"class":79},"3.2",[56,1347,1000],{"class":66},[56,1349,1350,1352,1354,1356,1358,1360,1362,1364,1366,1368,1370],{"class":58,"line":102},[56,1351,63],{"class":62},[56,1353,67],{"class":66},[56,1355,1314],{"class":989},[56,1357,67],{"class":66},[56,1359,70],{"class":62},[56,1361,67],{"class":66},[56,1363,121],{"class":79},[56,1365,532],{"class":75},[56,1367,535],{"class":79},[56,1369,125],{"class":124},[56,1371,1000],{"class":66},[14,1373,130],{},[47,1375,1377],{"className":49,"code":1376,"language":51,"meta":52,"style":52},"\u003Cclass 'int'>\n\u003Cclass 'float'>\n\u003Cclass 'float'>\n",[18,1378,1379,1398,1412],{"__ignoreMap":52},[56,1380,1381,1384,1387,1390,1392,1395],{"class":58,"line":59},[56,1382,1383],{"class":75},"\u003C",[56,1385,1386],{"class":124},"class",[56,1388,1389],{"class":937}," '",[56,1391,254],{"class":941},[56,1393,1394],{"class":937},"'",[56,1396,1397],{"class":75},">\n",[56,1399,1400,1402,1404,1406,1408,1410],{"class":58,"line":86},[56,1401,1383],{"class":75},[56,1403,1386],{"class":124},[56,1405,1389],{"class":937},[56,1407,258],{"class":941},[56,1409,1394],{"class":937},[56,1411,1397],{"class":75},[56,1413,1414,1416,1418,1420,1422,1424],{"class":58,"line":102},[56,1415,1383],{"class":75},[56,1417,1386],{"class":124},[56,1419,1389],{"class":937},[56,1421,258],{"class":941},[56,1423,1394],{"class":937},[56,1425,1397],{"class":75},[14,1427,1428],{},"This matters because beginners often check value types while debugging.",[14,1430,1431],{},"If your value comes from user input, convert it first and then check the type if needed.",[14,1433,1434],{},"Useful debugging steps:",[47,1436,1438],{"className":49,"code":1437,"language":51,"meta":52,"style":52},"value = \"-5\"\nprint(value)\nprint(type(value))\n\nuser_input = \"-3.7\"\nprint(abs(float(user_input)))\n",[18,1439,1440,1455,1465,1479,1483,1497],{"__ignoreMap":52},[56,1441,1442,1444,1446,1449,1452],{"class":58,"line":59},[56,1443,524],{"class":284},[56,1445,288],{"class":75},[56,1447,1448],{"class":937}," \"",[56,1450,1451],{"class":941},"-5",[56,1453,1454],{"class":937},"\"\n",[56,1456,1457,1459,1461,1463],{"class":58,"line":86},[56,1458,63],{"class":62},[56,1460,67],{"class":66},[56,1462,551],{"class":237},[56,1464,241],{"class":66},[56,1466,1467,1469,1471,1473,1475,1477],{"class":58,"line":102},[56,1468,63],{"class":62},[56,1470,67],{"class":66},[56,1472,1314],{"class":989},[56,1474,67],{"class":66},[56,1476,551],{"class":237},[56,1478,83],{"class":66},[56,1480,1481],{"class":58,"line":320},[56,1482,317],{"emptyLinePlaceholder":316},[56,1484,1485,1488,1490,1492,1495],{"class":58,"line":331},[56,1486,1487],{"class":284},"user_input ",[56,1489,288],{"class":75},[56,1491,1448],{"class":937},[56,1493,1494],{"class":941},"-3.7",[56,1496,1454],{"class":937},[56,1498,1500,1502,1504,1506,1508,1510,1512,1515],{"class":58,"line":1499},6,[56,1501,63],{"class":62},[56,1503,67],{"class":66},[56,1505,70],{"class":62},[56,1507,67],{"class":66},[56,1509,258],{"class":989},[56,1511,67],{"class":66},[56,1513,1514],{"class":237},"user_input",[56,1516,1000],{"class":66},[14,1518,130],{},[47,1520,1522],{"className":49,"code":1521,"language":51,"meta":52,"style":52},"-5\n\u003Cclass 'str'>\n3.7\n",[18,1523,1524,1530,1545],{"__ignoreMap":52},[56,1525,1526,1528],{"class":58,"line":59},[56,1527,76],{"class":75},[56,1529,1035],{"class":79},[56,1531,1532,1534,1536,1538,1541,1543],{"class":58,"line":86},[56,1533,1383],{"class":75},[56,1535,1386],{"class":124},[56,1537,1389],{"class":937},[56,1539,1540],{"class":941},"str",[56,1542,1394],{"class":937},[56,1544,1397],{"class":75},[56,1546,1547],{"class":58,"line":102},[56,1548,1549],{"class":79},"3.7\n",[42,1551,1553],{"id":1552},"faq","FAQ",[615,1555,1557],{"id":1556},"what-does-abs-mean-in-python","What does abs() mean in Python?",[14,1559,1560],{},"It returns the absolute value of a number. This means the value without a negative sign.",[615,1562,1564],{"id":1563},"does-abs-work-with-floats","Does abs() work with floats?",[14,1566,1567],{},"Yes. It works with integers, floats, and complex numbers.",[615,1569,1571],{"id":1570},"does-abs-change-the-original-variable","Does abs() change the original variable?",[14,1573,1574],{},"No. It returns a new value. You must assign it if you want to store the result.",[615,1576,1578],{"id":1577},"why-does-absinput-give-an-error","Why does abs(input()) give an error?",[14,1580,1581,1583,1584,964,1586,1588,1589,497],{},[18,1582,1046],{}," returns a string. Convert it first with ",[18,1585,963],{},[18,1587,970],{}," before using ",[18,1590,20],{},[615,1592,1594],{"id":1593},"what-does-abs-return-for-a-complex-number","What does abs() return for a complex number?",[14,1596,1597],{},"It returns the magnitude as a float.",[42,1599,1601],{"id":1600},"see-also","See also",[26,1603,1604,1611,1617,1623,1628],{},[29,1605,1606],{},[372,1607,1608,1610],{"href":960},[18,1609,963],{}," function explained",[29,1612,1613],{},[372,1614,1615,1610],{"href":967},[18,1616,970],{},[29,1618,1619],{},[372,1620,1621,1610],{"href":374},[18,1622,377],{},[29,1624,1625],{},[372,1626,1627],{"href":1050},"How to convert a string to an int in Python",[29,1629,1630],{},[372,1631,496],{"href":495},[1633,1634,1635],"style",{},"html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":52,"searchDepth":86,"depth":86,"links":1637},[1638,1639,1640,1641,1642,1643,1649,1655,1656,1663],{"id":44,"depth":86,"text":45},{"id":159,"depth":86,"text":160},{"id":219,"depth":86,"text":220},{"id":382,"depth":86,"text":383},{"id":500,"depth":86,"text":501},{"id":607,"depth":86,"text":608,"children":1644},[1645,1646,1647,1648],{"id":617,"depth":102,"text":618},{"id":718,"depth":102,"text":719},{"id":767,"depth":102,"text":768},{"id":834,"depth":102,"text":835},{"id":911,"depth":86,"text":912,"children":1650},[1651,1652,1653,1654],{"id":915,"depth":102,"text":916},{"id":1054,"depth":102,"text":1055},{"id":1100,"depth":102,"text":1101},{"id":1219,"depth":102,"text":1220},{"id":1270,"depth":86,"text":1271},{"id":1552,"depth":86,"text":1553,"children":1657},[1658,1659,1660,1661,1662],{"id":1556,"depth":102,"text":1557},{"id":1563,"depth":102,"text":1564},{"id":1570,"depth":102,"text":1571},{"id":1577,"depth":102,"text":1578},{"id":1593,"depth":102,"text":1594},{"id":1600,"depth":86,"text":1601},"Master python abs function explained in our comprehensive Python beginner guide.","md",{},"\u002Freference\u002Fpython-abs-function-explained",{"title":5,"description":1664},"reference\u002Fpython-abs-function-explained","xZAUASskbMCeUoHg_mObxnwl-ISSbujkBvQOGfQBzh4",1777585502612]