[{"data":1,"prerenderedAt":856},["ShallowReactive",2],{"blog-\u002Fblog\u002Fpython-truthiness-type-conversion-explained":3},{"id":4,"title":5,"body":6,"description":842,"difficulty":843,"extension":844,"framework":845,"frameworkSlug":56,"meta":846,"navigation":213,"order":133,"path":847,"qaPath":848,"seo":849,"stem":850,"subtopic":851,"topic":852,"topicSlug":853,"updated":854,"__hash__":855},"blog\u002Fblog\u002Fpython-truthiness-type-conversion-explained.md","Python Truthiness and Type Conversion Explained — Falsy Values, and\u002For, and __bool__",{"type":7,"value":8,"toc":833},"minimark",[9,14,40,44,51,162,169,173,176,241,254,308,312,323,475,479,498,551,559,582,596,600,611,741,767,771,829],[10,11,13],"h2",{"id":12},"python-truthiness-explained","Python truthiness, explained",[15,16,17,21,22,26,27,30,31,34,35,30,37,39],"p",{},[18,19,20],"code",{},"if my_list:"," reads naturally, but it relies on Python's ",[23,24,25],"strong",{},"truthiness"," rules — and those\nrules trip people up around empty containers, ",[18,28,29],{},"and","\u002F",[18,32,33],{},"or"," return values, and custom objects.\nThis guide covers what's falsy, how ",[18,36,29],{},[18,38,33],{}," actually behave, and how explicit conversion\nworks.",[10,41,43],{"id":42},"what-counts-as-falsy","What counts as falsy",[15,45,46,47,50],{},"Any object can be tested in a boolean context. Python treats these as ",[23,48,49],{},"falsy",":",[52,53,58],"pre",{"className":54,"code":55,"language":56,"meta":57,"style":57},"language-python shiki shiki-themes github-light github-dark","bool(None)     # False\nbool(False)    # False\nbool(0)        # False — also 0.0, 0j\nbool(\"\")       # False — empty string\nbool([])       # False — empty list, also (), {}, set()\nbool(range(0)) # False — empty range\n","python","",[18,59,60,83,98,114,131,142],{"__ignoreMap":57},[61,62,65,69,73,76,79],"span",{"class":63,"line":64},"line",1,[61,66,68],{"class":67},"sj4cs","bool",[61,70,72],{"class":71},"sVt8B","(",[61,74,75],{"class":67},"None",[61,77,78],{"class":71},")     ",[61,80,82],{"class":81},"sJ8bj","# False\n",[61,84,86,88,90,93,96],{"class":63,"line":85},2,[61,87,68],{"class":67},[61,89,72],{"class":71},[61,91,92],{"class":67},"False",[61,94,95],{"class":71},")    ",[61,97,82],{"class":81},[61,99,101,103,105,108,111],{"class":63,"line":100},3,[61,102,68],{"class":67},[61,104,72],{"class":71},[61,106,107],{"class":67},"0",[61,109,110],{"class":71},")        ",[61,112,113],{"class":81},"# False — also 0.0, 0j\n",[61,115,117,119,121,125,128],{"class":63,"line":116},4,[61,118,68],{"class":67},[61,120,72],{"class":71},[61,122,124],{"class":123},"sZZnC","\"\"",[61,126,127],{"class":71},")       ",[61,129,130],{"class":81},"# False — empty string\n",[61,132,134,136,139],{"class":63,"line":133},5,[61,135,68],{"class":67},[61,137,138],{"class":71},"([])       ",[61,140,141],{"class":81},"# False — empty list, also (), {}, set()\n",[61,143,145,147,149,152,154,156,159],{"class":63,"line":144},6,[61,146,68],{"class":67},[61,148,72],{"class":71},[61,150,151],{"class":67},"range",[61,153,72],{"class":71},[61,155,107],{"class":67},[61,157,158],{"class":71},")) ",[61,160,161],{"class":81},"# False — empty range\n",[15,163,164,165,168],{},"Everything else is ",[23,166,167],{},"truthy",": non-empty containers, non-zero numbers, and any object that\ndoesn't say otherwise.",[10,170,172],{"id":171},"prefer-truthiness-over-explicit-comparisons","Prefer truthiness over explicit comparisons",[15,174,175],{},"Because empty containers are falsy, the Pythonic way to check for \"has items\" is the object\nitself:",[52,177,179],{"className":54,"code":178,"language":56,"meta":57,"style":57},"items = []\nif items:            # idiomatic — checks \"non-empty\"\n    process(items)\n\nif len(items) > 0:   # works, but verbose and un-Pythonic\n    process(items)\n",[18,180,181,193,204,209,215,237],{"__ignoreMap":57},[61,182,183,186,190],{"class":63,"line":64},[61,184,185],{"class":71},"items ",[61,187,189],{"class":188},"szBVR","=",[61,191,192],{"class":71}," []\n",[61,194,195,198,201],{"class":63,"line":85},[61,196,197],{"class":188},"if",[61,199,200],{"class":71}," items:            ",[61,202,203],{"class":81},"# idiomatic — checks \"non-empty\"\n",[61,205,206],{"class":63,"line":100},[61,207,208],{"class":71},"    process(items)\n",[61,210,211],{"class":63,"line":116},[61,212,214],{"emptyLinePlaceholder":213},true,"\n",[61,216,217,219,222,225,228,231,234],{"class":63,"line":133},[61,218,197],{"class":188},[61,220,221],{"class":67}," len",[61,223,224],{"class":71},"(items) ",[61,226,227],{"class":188},">",[61,229,230],{"class":67}," 0",[61,232,233],{"class":71},":   ",[61,235,236],{"class":81},"# works, but verbose and un-Pythonic\n",[61,238,239],{"class":63,"line":144},[61,240,208],{"class":71},[15,242,243,244,246,247,249,250,253],{},"One important exception: when a value could legitimately be ",[18,245,107],{}," or ",[18,248,124],{}," and you need to\ndistinguish it from \"missing\", check ",[18,251,252],{},"is None"," explicitly:",[52,255,257],{"className":54,"code":256,"language":56,"meta":57,"style":57},"def f(timeout=None):\n    if timeout is None:    # NOT `if not timeout:` — 0 is a valid timeout!\n        timeout = 30\n",[18,258,259,278,298],{"__ignoreMap":57},[61,260,261,264,268,271,273,275],{"class":63,"line":64},[61,262,263],{"class":188},"def",[61,265,267],{"class":266},"sScJk"," f",[61,269,270],{"class":71},"(timeout",[61,272,189],{"class":188},[61,274,75],{"class":67},[61,276,277],{"class":71},"):\n",[61,279,280,283,286,289,292,295],{"class":63,"line":85},[61,281,282],{"class":188},"    if",[61,284,285],{"class":71}," timeout ",[61,287,288],{"class":188},"is",[61,290,291],{"class":67}," None",[61,293,294],{"class":71},":    ",[61,296,297],{"class":81},"# NOT `if not timeout:` — 0 is a valid timeout!\n",[61,299,300,303,305],{"class":63,"line":100},[61,301,302],{"class":71},"        timeout ",[61,304,189],{"class":188},[61,306,307],{"class":67}," 30\n",[10,309,311],{"id":310},"how-objects-decide-their-own-truthiness","How objects decide their own truthiness",[15,313,314,315,318,319,322],{},"For a custom class, Python checks ",[18,316,317],{},"__bool__"," first; if it's absent, it falls back to\n",[18,320,321],{},"__len__"," (zero length = falsy); if neither exists, the object is always truthy.",[52,324,326],{"className":54,"code":325,"language":56,"meta":57,"style":57},"class Cart:\n    def __init__(self, items):\n        self.items = items\n    def __len__(self):\n        return len(self.items)\n\nbool(Cart([]))       # False — falls back to __len__ == 0\nbool(Cart([1, 2]))   # True\n\nclass Always:\n    def __bool__(self):\n        return False\n\nbool(Always())       # False — __bool__ wins\n",[18,327,328,339,350,363,373,388,392,403,426,431,441,451,459,464],{"__ignoreMap":57},[61,329,330,333,336],{"class":63,"line":64},[61,331,332],{"class":188},"class",[61,334,335],{"class":266}," Cart",[61,337,338],{"class":71},":\n",[61,340,341,344,347],{"class":63,"line":85},[61,342,343],{"class":188},"    def",[61,345,346],{"class":67}," __init__",[61,348,349],{"class":71},"(self, items):\n",[61,351,352,355,358,360],{"class":63,"line":100},[61,353,354],{"class":67},"        self",[61,356,357],{"class":71},".items ",[61,359,189],{"class":188},[61,361,362],{"class":71}," items\n",[61,364,365,367,370],{"class":63,"line":116},[61,366,343],{"class":188},[61,368,369],{"class":67}," __len__",[61,371,372],{"class":71},"(self):\n",[61,374,375,378,380,382,385],{"class":63,"line":133},[61,376,377],{"class":188},"        return",[61,379,221],{"class":67},[61,381,72],{"class":71},[61,383,384],{"class":67},"self",[61,386,387],{"class":71},".items)\n",[61,389,390],{"class":63,"line":144},[61,391,214],{"emptyLinePlaceholder":213},[61,393,395,397,400],{"class":63,"line":394},7,[61,396,68],{"class":67},[61,398,399],{"class":71},"(Cart([]))       ",[61,401,402],{"class":81},"# False — falls back to __len__ == 0\n",[61,404,406,408,411,414,417,420,423],{"class":63,"line":405},8,[61,407,68],{"class":67},[61,409,410],{"class":71},"(Cart([",[61,412,413],{"class":67},"1",[61,415,416],{"class":71},", ",[61,418,419],{"class":67},"2",[61,421,422],{"class":71},"]))   ",[61,424,425],{"class":81},"# True\n",[61,427,429],{"class":63,"line":428},9,[61,430,214],{"emptyLinePlaceholder":213},[61,432,434,436,439],{"class":63,"line":433},10,[61,435,332],{"class":188},[61,437,438],{"class":266}," Always",[61,440,338],{"class":71},[61,442,444,446,449],{"class":63,"line":443},11,[61,445,343],{"class":188},[61,447,448],{"class":67}," __bool__",[61,450,372],{"class":71},[61,452,454,456],{"class":63,"line":453},12,[61,455,377],{"class":188},[61,457,458],{"class":67}," False\n",[61,460,462],{"class":63,"line":461},13,[61,463,214],{"emptyLinePlaceholder":213},[61,465,467,469,472],{"class":63,"line":466},14,[61,468,68],{"class":67},[61,470,471],{"class":71},"(Always())       ",[61,473,474],{"class":81},"# False — __bool__ wins\n",[10,476,478],{"id":477},"and-or-return-operands-not-booleans","and \u002F or return operands, not booleans",[15,480,481,482,484,485,487,488,491,492,30,495,497],{},"This surprises people: ",[18,483,29],{}," and ",[18,486,33],{}," return ",[23,489,490],{},"one of the operands",", not ",[18,493,494],{},"True",[18,496,92],{},".\nThey short-circuit:",[52,499,501],{"className":54,"code":500,"language":56,"meta":57,"style":57},"\"a\" and \"b\"     # 'b'  — and returns the last value if all truthy\n\"\" and \"b\"      # ''   — and returns the first falsy value\n\"a\" or \"b\"      # 'a'  — or returns the first truthy value\n\"\" or \"b\"       # 'b'  — or returns the last if all falsy\n",[18,502,503,517,528,540],{"__ignoreMap":57},[61,504,505,508,511,514],{"class":63,"line":64},[61,506,507],{"class":123},"\"a\"",[61,509,510],{"class":188}," and",[61,512,513],{"class":123}," \"b\"",[61,515,516],{"class":81},"     # 'b'  — and returns the last value if all truthy\n",[61,518,519,521,523,525],{"class":63,"line":85},[61,520,124],{"class":123},[61,522,510],{"class":188},[61,524,513],{"class":123},[61,526,527],{"class":81},"      # ''   — and returns the first falsy value\n",[61,529,530,532,535,537],{"class":63,"line":100},[61,531,507],{"class":123},[61,533,534],{"class":188}," or",[61,536,513],{"class":123},[61,538,539],{"class":81},"      # 'a'  — or returns the first truthy value\n",[61,541,542,544,546,548],{"class":63,"line":116},[61,543,124],{"class":123},[61,545,534],{"class":188},[61,547,513],{"class":123},[61,549,550],{"class":81},"       # 'b'  — or returns the last if all falsy\n",[15,552,553,554,30,556,558],{},"This enables the classic default-value idiom (with the same ",[18,555,107],{},[18,557,124],{}," caveat as above):",[52,560,562],{"className":54,"code":561,"language":56,"meta":57,"style":57},"name = user_input or \"Anonymous\"   # use input, or fall back if empty\n",[18,563,564],{"__ignoreMap":57},[61,565,566,569,571,574,576,579],{"class":63,"line":64},[61,567,568],{"class":71},"name ",[61,570,189],{"class":188},[61,572,573],{"class":71}," user_input ",[61,575,33],{"class":188},[61,577,578],{"class":123}," \"Anonymous\"",[61,580,581],{"class":81},"   # use input, or fall back if empty\n",[15,583,584,585,588,589,592,593,595],{},"For a true boolean negation, ",[18,586,587],{},"not"," always returns an actual bool: ",[18,590,591],{},"not \"\""," → ",[18,594,494],{},".",[10,597,599],{"id":598},"explicit-type-conversion","Explicit type conversion",[15,601,602,603,606,607,610],{},"Python doesn't do implicit numeric→string coercion (",[18,604,605],{},"\"x\" + 1"," is a ",[18,608,609],{},"TypeError","), so you\nconvert explicitly with the type constructors:",[52,612,614],{"className":54,"code":613,"language":56,"meta":57,"style":57},"int(\"42\")          # 42\nint(\"42\", 16)      # 66  — parse as base 16\nint(3.9)           # 3   — truncates toward zero (not rounding!)\nfloat(\"3.14\")      # 3.14\nstr(42)            # '42'\nlist(\"abc\")        # ['a', 'b', 'c']\ndict([(\"a\", 1)])   # {'a': 1}\nbool([])           # False\n",[18,615,616,632,651,666,681,697,712,732],{"__ignoreMap":57},[61,617,618,621,623,626,629],{"class":63,"line":64},[61,619,620],{"class":67},"int",[61,622,72],{"class":71},[61,624,625],{"class":123},"\"42\"",[61,627,628],{"class":71},")          ",[61,630,631],{"class":81},"# 42\n",[61,633,634,636,638,640,642,645,648],{"class":63,"line":85},[61,635,620],{"class":67},[61,637,72],{"class":71},[61,639,625],{"class":123},[61,641,416],{"class":71},[61,643,644],{"class":67},"16",[61,646,647],{"class":71},")      ",[61,649,650],{"class":81},"# 66  — parse as base 16\n",[61,652,653,655,657,660,663],{"class":63,"line":100},[61,654,620],{"class":67},[61,656,72],{"class":71},[61,658,659],{"class":67},"3.9",[61,661,662],{"class":71},")           ",[61,664,665],{"class":81},"# 3   — truncates toward zero (not rounding!)\n",[61,667,668,671,673,676,678],{"class":63,"line":116},[61,669,670],{"class":67},"float",[61,672,72],{"class":71},[61,674,675],{"class":123},"\"3.14\"",[61,677,647],{"class":71},[61,679,680],{"class":81},"# 3.14\n",[61,682,683,686,688,691,694],{"class":63,"line":133},[61,684,685],{"class":67},"str",[61,687,72],{"class":71},[61,689,690],{"class":67},"42",[61,692,693],{"class":71},")            ",[61,695,696],{"class":81},"# '42'\n",[61,698,699,702,704,707,709],{"class":63,"line":144},[61,700,701],{"class":67},"list",[61,703,72],{"class":71},[61,705,706],{"class":123},"\"abc\"",[61,708,110],{"class":71},[61,710,711],{"class":81},"# ['a', 'b', 'c']\n",[61,713,714,717,720,722,724,726,729],{"class":63,"line":394},[61,715,716],{"class":67},"dict",[61,718,719],{"class":71},"([(",[61,721,507],{"class":123},[61,723,416],{"class":71},[61,725,413],{"class":67},[61,727,728],{"class":71},")])   ",[61,730,731],{"class":81},"# {'a': 1}\n",[61,733,734,736,739],{"class":63,"line":405},[61,735,68],{"class":67},[61,737,738],{"class":71},"([])           ",[61,740,82],{"class":81},[15,742,743,746,747,750,751,754,755,758,759,762,763,766],{},[18,744,745],{},"int()"," on a float ",[23,748,749],{},"truncates"," toward zero, so ",[18,752,753],{},"int(-3.9)"," is ",[18,756,757],{},"-3",". Bad input raises\n",[18,760,761],{},"ValueError"," (",[18,764,765],{},"int(\"abc\")","), which is what you catch when validating user data.",[10,768,770],{"id":769},"recap","Recap",[15,772,773,774,416,776,778,779,782,783,786,787,789,790,246,792,794,795,797,798,800,801,30,803,805,806,809,810,813,814,416,816,416,819,416,822,825,826,828],{},"Falsy values are ",[18,775,75],{},[18,777,92],{},", numeric zero, and empty containers\u002Fstrings; everything\nelse is truthy. Write ",[18,780,781],{},"if items:"," rather than ",[18,784,785],{},"if len(items) > 0:",", but use ",[18,788,252],{},"\nexplicitly when ",[18,791,107],{},[18,793,124],{}," are valid values you must distinguish from \"missing\". Custom\nobjects control truthiness via ",[18,796,317],{},", falling back to ",[18,799,321],{},". Remember that\n",[18,802,29],{},[18,804,33],{}," ",[23,807,808],{},"return an operand"," and short-circuit, powering the ",[18,811,812],{},"value or default"," idiom.\nConversions are explicit — ",[18,815,745],{},[18,817,818],{},"float()",[18,820,821],{},"str()",[18,823,824],{},"list()"," — and ",[18,827,745],{}," on a float\ntruncates toward zero.",[830,831,832],"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 .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}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 .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}",{"title":57,"searchDepth":85,"depth":85,"links":834},[835,836,837,838,839,840,841],{"id":12,"depth":85,"text":13},{"id":42,"depth":85,"text":43},{"id":171,"depth":85,"text":172},{"id":310,"depth":85,"text":311},{"id":477,"depth":85,"text":478},{"id":598,"depth":85,"text":599},{"id":769,"depth":85,"text":770},"Which Python values are falsy, how custom objects decide their truthiness with __bool__ and __len__, what and\u002For actually return, and how explicit conversions like int() and list() work.","easy","md","Python",{},"\u002Fblog\u002Fpython-truthiness-type-conversion-explained","\u002Fpython\u002Ffundamentals\u002Ftruthiness-conversion",{"title":5,"description":842},"blog\u002Fpython-truthiness-type-conversion-explained","Truthiness & Type Conversion","Fundamentals","fundamentals","2026-06-19","86WgerOuZpKivDAXeVyx92YaOQrXVsZ30gZT1h9F2U0",1782244094187]