[{"data":1,"prerenderedAt":2105},["ShallowReactive",2],{"doc-\u002Flearn\u002Fpython-if-statements-explained":3},{"id":4,"title":5,"body":6,"description":2098,"extension":2099,"meta":2100,"navigation":72,"path":2101,"seo":2102,"stem":2103,"__hash__":2104},"content\u002Flearn\u002Fpython-if-statements-explained.md","Python if Statements Explained",{"type":7,"value":8,"toc":2067},"minimark",[9,13,22,33,36,41,116,125,128,147,151,156,159,175,178,184,187,236,239,255,266,270,276,294,296,344,347,364,367,382,390,394,402,408,411,447,449,483,485,495,508,552,555,566,569,626,639,643,646,651,699,703,758,762,810,814,859,862,873,877,884,890,892,935,942,945,964,967,980,983,1026,1034,1038,1043,1046,1048,1114,1116,1125,1132,1138,1145,1149,1152,1158,1161,1204,1207,1250,1256,1259,1272,1279,1283,1288,1292,1295,1336,1341,1344,1386,1393,1402,1405,1408,1460,1462,1513,1516,1528,1532,1534,1576,1578,1620,1627,1631,1638,1641,1644,1726,1729,1790,1793,1804,1808,1811,1813,1861,1868,1871,1902,1906,1910,1915,1919,1925,1929,1937,1941,1947,1949,2007,2011,2020,2024,2047,2063],[10,11,5],"h1",{"id":12},"python-if-statements-explained",[14,15,16,17,21],"p",{},"An ",[18,19,20],"code",{},"if"," statement lets Python make a decision.",[14,23,24,25,28,29,32],{},"It checks a condition. If that condition is ",[18,26,27],{},"True",", Python runs the indented code underneath it. If the condition is ",[18,30,31],{},"False",", Python skips that code.",[14,34,35],{},"This is one of the most important tools in Python because it controls the flow of your program.",[37,38,40],"h2",{"id":39},"quick-example","Quick example",[42,43,48],"pre",{"className":44,"code":45,"language":46,"meta":47,"style":47},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","age = 18\n\nif age >= 18:\n    print(\"You are an adult\")\n","python","",[18,49,50,67,74,93],{"__ignoreMap":47},[51,52,55,59,63],"span",{"class":53,"line":54},"line",1,[51,56,58],{"class":57},"su5hD","age ",[51,60,62],{"class":61},"smGrS","=",[51,64,66],{"class":65},"srdBf"," 18\n",[51,68,70],{"class":53,"line":69},2,[51,71,73],{"emptyLinePlaceholder":72},true,"\n",[51,75,77,80,83,86,89],{"class":53,"line":76},3,[51,78,20],{"class":79},"sVHd0",[51,81,82],{"class":57}," age ",[51,84,85],{"class":61},">=",[51,87,88],{"class":65}," 18",[51,90,92],{"class":91},"sP7_E",":\n",[51,94,96,100,103,107,111,113],{"class":53,"line":95},4,[51,97,99],{"class":98},"sptTA","    print",[51,101,102],{"class":91},"(",[51,104,106],{"class":105},"sjJ54","\"",[51,108,110],{"class":109},"s_sjI","You are an adult",[51,112,106],{"class":105},[51,114,115],{"class":91},")\n",[14,117,118,119,121,122,124],{},"Use ",[18,120,20],{}," when you want code to run only when a condition is ",[18,123,27],{},".",[14,126,127],{},"Two important rules:",[129,130,131,141],"ul",{},[132,133,134,135,137,138],"li",{},"End the ",[18,136,20],{}," line with a colon ",[18,139,140],{},":",[132,142,143,144,146],{},"Indent the code inside the ",[18,145,20],{}," block",[37,148,150],{"id":149},"what-an-if-statement-does","What an if statement does",[14,152,16,153,155],{},[18,154,20],{}," statement helps your program choose whether to do something.",[14,157,158],{},"It works like this:",[129,160,161,164,170],{},[132,162,163],{},"Python checks a condition",[132,165,166,167,169],{},"If the condition is ",[18,168,27],{},", the block runs",[132,171,166,172,174],{},[18,173,31],{},", the block is skipped",[14,176,177],{},"You can think of it as:",[179,180,181],"blockquote",{},[14,182,183],{},"“If this is true, do this.”",[14,185,186],{},"Example:",[42,188,190],{"className":44,"code":189,"language":46,"meta":47,"style":47},"temperature = 30\n\nif temperature > 25:\n    print(\"It is a warm day\")\n",[18,191,192,202,206,221],{"__ignoreMap":47},[51,193,194,197,199],{"class":53,"line":54},[51,195,196],{"class":57},"temperature ",[51,198,62],{"class":61},[51,200,201],{"class":65}," 30\n",[51,203,204],{"class":53,"line":69},[51,205,73],{"emptyLinePlaceholder":72},[51,207,208,210,213,216,219],{"class":53,"line":76},[51,209,20],{"class":79},[51,211,212],{"class":57}," temperature ",[51,214,215],{"class":61},">",[51,217,218],{"class":65}," 25",[51,220,92],{"class":91},[51,222,223,225,227,229,232,234],{"class":53,"line":95},[51,224,99],{"class":98},[51,226,102],{"class":91},[51,228,106],{"class":105},[51,230,231],{"class":109},"It is a warm day",[51,233,106],{"class":105},[51,235,115],{"class":91},[14,237,238],{},"Output:",[42,240,242],{"className":44,"code":241,"language":46,"meta":47,"style":47},"It is a warm day\n",[18,243,244],{"__ignoreMap":47},[51,245,246,249,252],{"class":53,"line":54},[51,247,248],{"class":57},"It ",[51,250,251],{"class":61},"is",[51,253,254],{"class":57}," a warm day\n",[14,256,257,258,261,262,265],{},"Because ",[18,259,260],{},"temperature > 25"," is true, Python runs the ",[18,263,264],{},"print()"," line.",[37,267,269],{"id":268},"basic-if-statement-syntax","Basic if statement syntax",[14,271,272,273,275],{},"A basic ",[18,274,20],{}," statement has four parts:",[129,277,278,283,286,291],{},[132,279,280,281],{},"The keyword ",[18,282,20],{},[132,284,285],{},"A condition",[132,287,288,289],{},"A colon ",[18,290,140],{},[132,292,293],{},"An indented block of code",[14,295,186],{},[42,297,299],{"className":44,"code":298,"language":46,"meta":47,"style":47},"score = 80\n\nif score >= 50:\n    print(\"You passed\")\n",[18,300,301,311,315,329],{"__ignoreMap":47},[51,302,303,306,308],{"class":53,"line":54},[51,304,305],{"class":57},"score ",[51,307,62],{"class":61},[51,309,310],{"class":65}," 80\n",[51,312,313],{"class":53,"line":69},[51,314,73],{"emptyLinePlaceholder":72},[51,316,317,319,322,324,327],{"class":53,"line":76},[51,318,20],{"class":79},[51,320,321],{"class":57}," score ",[51,323,85],{"class":61},[51,325,326],{"class":65}," 50",[51,328,92],{"class":91},[51,330,331,333,335,337,340,342],{"class":53,"line":95},[51,332,99],{"class":98},[51,334,102],{"class":91},[51,336,106],{"class":105},[51,338,339],{"class":109},"You passed",[51,341,106],{"class":105},[51,343,115],{"class":91},[14,345,346],{},"The key line is:",[42,348,350],{"className":44,"code":349,"language":46,"meta":47,"style":47},"if score >= 50:\n",[18,351,352],{"__ignoreMap":47},[51,353,354,356,358,360,362],{"class":53,"line":54},[51,355,20],{"class":79},[51,357,321],{"class":57},[51,359,85],{"class":61},[51,361,326],{"class":65},[51,363,92],{"class":91},[14,365,366],{},"This means:",[129,368,369,379],{},[132,370,371,372,375,376],{},"check whether ",[18,373,374],{},"score"," is greater than or equal to ",[18,377,378],{},"50",[132,380,381],{},"if it is, run the indented line below it",[14,383,384,385,124],{},"If you are still getting used to variables, see ",[386,387,389],"a",{"href":388},"\u002Flearn\u002Fpython-variables-explained-for-beginners\u002F","Python variables explained for beginners",[37,391,393],{"id":392},"how-conditions-work","How conditions work",[14,395,396,397,399,400,124],{},"A condition is an expression that becomes either ",[18,398,27],{}," or ",[18,401,31],{},[14,403,404,405,407],{},"Python uses that result to decide whether to run the ",[18,406,20],{}," block.",[14,409,410],{},"Common comparison operators:",[129,412,413,419,425,430,436,441],{},[132,414,415,418],{},[18,416,417],{},"=="," equal to",[132,420,421,424],{},[18,422,423],{},"!="," not equal to",[132,426,427,429],{},[18,428,215],{}," greater than",[132,431,432,435],{},[18,433,434],{},"\u003C"," less than",[132,437,438,440],{},[18,439,85],{}," greater than or equal to",[132,442,443,446],{},[18,444,445],{},"\u003C="," less than or equal to",[14,448,186],{},[42,450,452],{"className":44,"code":451,"language":46,"meta":47,"style":47},"age = 16\n\nprint(age >= 18)\n",[18,453,454,463,467],{"__ignoreMap":47},[51,455,456,458,460],{"class":53,"line":54},[51,457,58],{"class":57},[51,459,62],{"class":61},[51,461,462],{"class":65}," 16\n",[51,464,465],{"class":53,"line":69},[51,466,73],{"emptyLinePlaceholder":72},[51,468,469,472,474,477,479,481],{"class":53,"line":76},[51,470,471],{"class":98},"print",[51,473,102],{"class":91},[51,475,58],{"class":476},"slqww",[51,478,85],{"class":61},[51,480,88],{"class":65},[51,482,115],{"class":91},[14,484,238],{},[42,486,488],{"className":44,"code":487,"language":46,"meta":47,"style":47},"False\n",[18,489,490],{"__ignoreMap":47},[51,491,492],{"class":53,"line":54},[51,493,487],{"class":494},"s39Yj",[14,496,497,498,501,502,504,505,507],{},"Since ",[18,499,500],{},"age >= 18"," is ",[18,503,31],{},", this ",[18,506,20],{}," block will not run:",[42,509,511],{"className":44,"code":510,"language":46,"meta":47,"style":47},"age = 16\n\nif age >= 18:\n    print(\"You can vote\")\n",[18,512,513,521,525,537],{"__ignoreMap":47},[51,514,515,517,519],{"class":53,"line":54},[51,516,58],{"class":57},[51,518,62],{"class":61},[51,520,462],{"class":65},[51,522,523],{"class":53,"line":69},[51,524,73],{"emptyLinePlaceholder":72},[51,526,527,529,531,533,535],{"class":53,"line":76},[51,528,20],{"class":79},[51,530,82],{"class":57},[51,532,85],{"class":61},[51,534,88],{"class":65},[51,536,92],{"class":91},[51,538,539,541,543,545,548,550],{"class":53,"line":95},[51,540,99],{"class":98},[51,542,102],{"class":91},[51,544,106],{"class":105},[51,546,547],{"class":109},"You can vote",[51,549,106],{"class":105},[51,551,115],{"class":91},[14,553,554],{},"Conditions often compare:",[129,556,557,560,563],{},[132,558,559],{},"numbers",[132,561,562],{},"strings",[132,564,565],{},"variables",[14,567,568],{},"String example:",[42,570,572],{"className":44,"code":571,"language":46,"meta":47,"style":47},"username = \"admin\"\n\nif username == \"admin\":\n    print(\"Welcome, admin\")\n",[18,573,574,590,594,611],{"__ignoreMap":47},[51,575,576,579,581,584,587],{"class":53,"line":54},[51,577,578],{"class":57},"username ",[51,580,62],{"class":61},[51,582,583],{"class":105}," \"",[51,585,586],{"class":109},"admin",[51,588,589],{"class":105},"\"\n",[51,591,592],{"class":53,"line":69},[51,593,73],{"emptyLinePlaceholder":72},[51,595,596,598,601,603,605,607,609],{"class":53,"line":76},[51,597,20],{"class":79},[51,599,600],{"class":57}," username ",[51,602,417],{"class":61},[51,604,583],{"class":105},[51,606,586],{"class":109},[51,608,106],{"class":105},[51,610,92],{"class":91},[51,612,613,615,617,619,622,624],{"class":53,"line":95},[51,614,99],{"class":98},[51,616,102],{"class":91},[51,618,106],{"class":105},[51,620,621],{"class":109},"Welcome, admin",[51,623,106],{"class":105},[51,625,115],{"class":91},[14,627,628,629,631,632,634,635,124],{},"If you want to understand ",[18,630,27],{}," and ",[18,633,31],{}," better, read ",[386,636,638],{"href":637},"\u002Flearn\u002Fpython-booleans-explained-true-and-false\u002F","Python booleans explained: True and False",[37,640,642],{"id":641},"simple-examples-beginners-understand","Simple examples beginners understand",[14,644,645],{},"Here are a few common beginner examples.",[647,648,650],"h3",{"id":649},"check-if-a-number-is-positive","Check if a number is positive",[42,652,654],{"className":44,"code":653,"language":46,"meta":47,"style":47},"number = 7\n\nif number > 0:\n    print(\"The number is positive\")\n",[18,655,656,666,670,684],{"__ignoreMap":47},[51,657,658,661,663],{"class":53,"line":54},[51,659,660],{"class":57},"number ",[51,662,62],{"class":61},[51,664,665],{"class":65}," 7\n",[51,667,668],{"class":53,"line":69},[51,669,73],{"emptyLinePlaceholder":72},[51,671,672,674,677,679,682],{"class":53,"line":76},[51,673,20],{"class":79},[51,675,676],{"class":57}," number ",[51,678,215],{"class":61},[51,680,681],{"class":65}," 0",[51,683,92],{"class":91},[51,685,686,688,690,692,695,697],{"class":53,"line":95},[51,687,99],{"class":98},[51,689,102],{"class":91},[51,691,106],{"class":105},[51,693,694],{"class":109},"The number is positive",[51,696,106],{"class":105},[51,698,115],{"class":91},[647,700,702],{"id":701},"check-if-a-password-matches","Check if a password matches",[42,704,706],{"className":44,"code":705,"language":46,"meta":47,"style":47},"password = \"python123\"\n\nif password == \"python123\":\n    print(\"Access granted\")\n",[18,707,708,722,726,743],{"__ignoreMap":47},[51,709,710,713,715,717,720],{"class":53,"line":54},[51,711,712],{"class":57},"password ",[51,714,62],{"class":61},[51,716,583],{"class":105},[51,718,719],{"class":109},"python123",[51,721,589],{"class":105},[51,723,724],{"class":53,"line":69},[51,725,73],{"emptyLinePlaceholder":72},[51,727,728,730,733,735,737,739,741],{"class":53,"line":76},[51,729,20],{"class":79},[51,731,732],{"class":57}," password ",[51,734,417],{"class":61},[51,736,583],{"class":105},[51,738,719],{"class":109},[51,740,106],{"class":105},[51,742,92],{"class":91},[51,744,745,747,749,751,754,756],{"class":53,"line":95},[51,746,99],{"class":98},[51,748,102],{"class":91},[51,750,106],{"class":105},[51,752,753],{"class":109},"Access granted",[51,755,106],{"class":105},[51,757,115],{"class":91},[647,759,761],{"id":760},"check-if-a-string-is-empty","Check if a string is empty",[42,763,765],{"className":44,"code":764,"language":46,"meta":47,"style":47},"name = \"\"\n\nif name == \"\":\n    print(\"The string is empty\")\n",[18,766,767,777,781,795],{"__ignoreMap":47},[51,768,769,772,774],{"class":53,"line":54},[51,770,771],{"class":57},"name ",[51,773,62],{"class":61},[51,775,776],{"class":105}," \"\"\n",[51,778,779],{"class":53,"line":69},[51,780,73],{"emptyLinePlaceholder":72},[51,782,783,785,788,790,793],{"class":53,"line":76},[51,784,20],{"class":79},[51,786,787],{"class":57}," name ",[51,789,417],{"class":61},[51,791,792],{"class":105}," \"\"",[51,794,92],{"class":91},[51,796,797,799,801,803,806,808],{"class":53,"line":95},[51,798,99],{"class":98},[51,800,102],{"class":91},[51,802,106],{"class":105},[51,804,805],{"class":109},"The string is empty",[51,807,106],{"class":105},[51,809,115],{"class":91},[647,811,813],{"id":812},"check-if-a-user-is-old-enough","Check if a user is old enough",[42,815,817],{"className":44,"code":816,"language":46,"meta":47,"style":47},"age = 20\n\nif age >= 18:\n    print(\"You are old enough\")\n",[18,818,819,828,832,844],{"__ignoreMap":47},[51,820,821,823,825],{"class":53,"line":54},[51,822,58],{"class":57},[51,824,62],{"class":61},[51,826,827],{"class":65}," 20\n",[51,829,830],{"class":53,"line":69},[51,831,73],{"emptyLinePlaceholder":72},[51,833,834,836,838,840,842],{"class":53,"line":76},[51,835,20],{"class":79},[51,837,82],{"class":57},[51,839,85],{"class":61},[51,841,88],{"class":65},[51,843,92],{"class":91},[51,845,846,848,850,852,855,857],{"class":53,"line":95},[51,847,99],{"class":98},[51,849,102],{"class":91},[51,851,106],{"class":105},[51,853,854],{"class":109},"You are old enough",[51,856,106],{"class":105},[51,858,115],{"class":91},[14,860,861],{},"These examples all follow the same pattern:",[129,863,864,867,870],{},[132,865,866],{},"write a condition",[132,868,869],{},"let Python check it",[132,871,872],{},"run code only if it is true",[37,874,876],{"id":875},"using-if-with-boolean-values","Using if with boolean values",[14,878,879,880,399,882,124],{},"A boolean value is either ",[18,881,27],{},[18,883,31],{},[14,885,886,887,889],{},"You can use a boolean variable directly in an ",[18,888,20],{}," statement.",[14,891,186],{},[42,893,895],{"className":44,"code":894,"language":46,"meta":47,"style":47},"is_logged_in = True\n\nif is_logged_in:\n    print(\"Welcome back\")\n",[18,896,897,907,911,920],{"__ignoreMap":47},[51,898,899,902,904],{"class":53,"line":54},[51,900,901],{"class":57},"is_logged_in ",[51,903,62],{"class":61},[51,905,906],{"class":494}," True\n",[51,908,909],{"class":53,"line":69},[51,910,73],{"emptyLinePlaceholder":72},[51,912,913,915,918],{"class":53,"line":76},[51,914,20],{"class":79},[51,916,917],{"class":57}," is_logged_in",[51,919,92],{"class":91},[51,921,922,924,926,928,931,933],{"class":53,"line":95},[51,923,99],{"class":98},[51,925,102],{"class":91},[51,927,106],{"class":105},[51,929,930],{"class":109},"Welcome back",[51,932,106],{"class":105},[51,934,115],{"class":91},[14,936,937,938,941],{},"This works because ",[18,939,940],{},"is_logged_in"," already holds a boolean value.",[14,943,944],{},"You do not need to write:",[42,946,948],{"className":44,"code":947,"language":46,"meta":47,"style":47},"if is_logged_in == True:\n",[18,949,950],{"__ignoreMap":47},[51,951,952,954,957,959,962],{"class":53,"line":54},[51,953,20],{"class":79},[51,955,956],{"class":57}," is_logged_in ",[51,958,417],{"class":61},[51,960,961],{"class":494}," True",[51,963,92],{"class":91},[14,965,966],{},"That works, but this is cleaner:",[42,968,970],{"className":44,"code":969,"language":46,"meta":47,"style":47},"if is_logged_in:\n",[18,971,972],{"__ignoreMap":47},[51,973,974,976,978],{"class":53,"line":54},[51,975,20],{"class":79},[51,977,917],{"class":57},[51,979,92],{"class":91},[14,981,982],{},"Another example:",[42,984,986],{"className":44,"code":985,"language":46,"meta":47,"style":47},"has_email = False\n\nif has_email:\n    print(\"Email found\")\n",[18,987,988,998,1002,1011],{"__ignoreMap":47},[51,989,990,993,995],{"class":53,"line":54},[51,991,992],{"class":57},"has_email ",[51,994,62],{"class":61},[51,996,997],{"class":494}," False\n",[51,999,1000],{"class":53,"line":69},[51,1001,73],{"emptyLinePlaceholder":72},[51,1003,1004,1006,1009],{"class":53,"line":76},[51,1005,20],{"class":79},[51,1007,1008],{"class":57}," has_email",[51,1010,92],{"class":91},[51,1012,1013,1015,1017,1019,1022,1024],{"class":53,"line":95},[51,1014,99],{"class":98},[51,1016,102],{"class":91},[51,1018,106],{"class":105},[51,1020,1021],{"class":109},"Email found",[51,1023,106],{"class":105},[51,1025,115],{"class":91},[14,1027,497,1028,501,1031,1033],{},[18,1029,1030],{},"has_email",[18,1032,31],{},", nothing is printed.",[37,1035,1037],{"id":1036},"what-happens-when-the-condition-is-false","What happens when the condition is False",[14,1039,166,1040,1042],{},[18,1041,31],{},", Python skips the indented block.",[14,1044,1045],{},"Then the program continues with the next line after the block.",[14,1047,186],{},[42,1049,1051],{"className":44,"code":1050,"language":46,"meta":47,"style":47},"age = 15\n\nif age >= 18:\n    print(\"Adult\")\n\nprint(\"Program finished\")\n",[18,1052,1053,1062,1066,1078,1093,1098],{"__ignoreMap":47},[51,1054,1055,1057,1059],{"class":53,"line":54},[51,1056,58],{"class":57},[51,1058,62],{"class":61},[51,1060,1061],{"class":65}," 15\n",[51,1063,1064],{"class":53,"line":69},[51,1065,73],{"emptyLinePlaceholder":72},[51,1067,1068,1070,1072,1074,1076],{"class":53,"line":76},[51,1069,20],{"class":79},[51,1071,82],{"class":57},[51,1073,85],{"class":61},[51,1075,88],{"class":65},[51,1077,92],{"class":91},[51,1079,1080,1082,1084,1086,1089,1091],{"class":53,"line":95},[51,1081,99],{"class":98},[51,1083,102],{"class":91},[51,1085,106],{"class":105},[51,1087,1088],{"class":109},"Adult",[51,1090,106],{"class":105},[51,1092,115],{"class":91},[51,1094,1096],{"class":53,"line":1095},5,[51,1097,73],{"emptyLinePlaceholder":72},[51,1099,1101,1103,1105,1107,1110,1112],{"class":53,"line":1100},6,[51,1102,471],{"class":98},[51,1104,102],{"class":91},[51,1106,106],{"class":105},[51,1108,1109],{"class":109},"Program finished",[51,1111,106],{"class":105},[51,1113,115],{"class":91},[14,1115,238],{},[42,1117,1119],{"className":44,"code":1118,"language":46,"meta":47,"style":47},"Program finished\n",[18,1120,1121],{"__ignoreMap":47},[51,1122,1123],{"class":53,"line":54},[51,1124,1118],{"class":57},[14,1126,1127,1128,1131],{},"The line ",[18,1129,1130],{},"print(\"Adult\")"," does not run because the condition is false.",[14,1133,1134,1135,1137],{},"This page focuses only on plain ",[18,1136,20],{}," statements.",[14,1139,1140,1141,124],{},"If you want code to run when the condition is false, learn ",[386,1142,1144],{"href":1143},"\u002Flearn\u002Fpython-if-else-and-elif-explained\u002F","Python if-else and elif explained",[37,1146,1148],{"id":1147},"indentation-rules-to-remember","Indentation rules to remember",[14,1150,1151],{},"Indentation is very important in Python.",[14,1153,1154,1155,1157],{},"The code inside an ",[18,1156,20],{}," statement must be indented. This tells Python which lines belong to the block.",[14,1159,1160],{},"Correct:",[42,1162,1164],{"className":44,"code":1163,"language":46,"meta":47,"style":47},"age = 18\n\nif age >= 18:\n    print(\"Adult\")\n",[18,1165,1166,1174,1178,1190],{"__ignoreMap":47},[51,1167,1168,1170,1172],{"class":53,"line":54},[51,1169,58],{"class":57},[51,1171,62],{"class":61},[51,1173,66],{"class":65},[51,1175,1176],{"class":53,"line":69},[51,1177,73],{"emptyLinePlaceholder":72},[51,1179,1180,1182,1184,1186,1188],{"class":53,"line":76},[51,1181,20],{"class":79},[51,1183,82],{"class":57},[51,1185,85],{"class":61},[51,1187,88],{"class":65},[51,1189,92],{"class":91},[51,1191,1192,1194,1196,1198,1200,1202],{"class":53,"line":95},[51,1193,99],{"class":98},[51,1195,102],{"class":91},[51,1197,106],{"class":105},[51,1199,1088],{"class":109},[51,1201,106],{"class":105},[51,1203,115],{"class":91},[14,1205,1206],{},"Incorrect:",[42,1208,1210],{"className":44,"code":1209,"language":46,"meta":47,"style":47},"age = 18\n\nif age >= 18:\nprint(\"Adult\")\n",[18,1211,1212,1220,1224,1236],{"__ignoreMap":47},[51,1213,1214,1216,1218],{"class":53,"line":54},[51,1215,58],{"class":57},[51,1217,62],{"class":61},[51,1219,66],{"class":65},[51,1221,1222],{"class":53,"line":69},[51,1223,73],{"emptyLinePlaceholder":72},[51,1225,1226,1228,1230,1232,1234],{"class":53,"line":76},[51,1227,20],{"class":79},[51,1229,82],{"class":57},[51,1231,85],{"class":61},[51,1233,88],{"class":65},[51,1235,92],{"class":91},[51,1237,1238,1240,1242,1244,1246,1248],{"class":53,"line":95},[51,1239,471],{"class":98},[51,1241,102],{"class":91},[51,1243,106],{"class":105},[51,1245,1088],{"class":109},[51,1247,106],{"class":105},[51,1249,115],{"class":91},[14,1251,1252,1253,1255],{},"The second example causes an error because the ",[18,1254,264],{}," line is not indented.",[14,1257,1258],{},"Rules to remember:",[129,1260,1261,1266,1269],{},[132,1262,1263,1264,146],{},"indent every line inside the ",[18,1265,20],{},[132,1267,1268],{},"use consistent spaces",[132,1270,1271],{},"do not mix indentation styles in the same file",[14,1273,1274,1275,124],{},"If indentation is confusing, see ",[386,1276,1278],{"href":1277},"\u002Flearn\u002Fpython-indentation-rules-and-why-they-matter\u002F","Python indentation rules and why they matter",[37,1280,1282],{"id":1281},"common-beginner-mistakes","Common beginner mistakes",[14,1284,1285,1286,1137],{},"Here are some common problems when writing ",[18,1287,20],{},[647,1289,1291],{"id":1290},"forgetting-the-colon","Forgetting the colon",[14,1293,1294],{},"This is wrong:",[42,1296,1298],{"className":44,"code":1297,"language":46,"meta":47,"style":47},"age = 18\n\nif age >= 18\n    print(\"Adult\")\n",[18,1299,1300,1308,1312,1322],{"__ignoreMap":47},[51,1301,1302,1304,1306],{"class":53,"line":54},[51,1303,58],{"class":57},[51,1305,62],{"class":61},[51,1307,66],{"class":65},[51,1309,1310],{"class":53,"line":69},[51,1311,73],{"emptyLinePlaceholder":72},[51,1313,1314,1316,1318,1320],{"class":53,"line":76},[51,1315,20],{"class":79},[51,1317,82],{"class":57},[51,1319,85],{"class":61},[51,1321,66],{"class":65},[51,1323,1324,1326,1328,1330,1332,1334],{"class":53,"line":95},[51,1325,99],{"class":98},[51,1327,102],{"class":91},[51,1329,106],{"class":105},[51,1331,1088],{"class":109},[51,1333,106],{"class":105},[51,1335,115],{"class":91},[14,1337,1338,1339,265],{},"Python expects a colon at the end of the ",[18,1340,20],{},[14,1342,1343],{},"Correct version:",[42,1345,1346],{"className":44,"code":1163,"language":46,"meta":47,"style":47},[18,1347,1348,1356,1360,1372],{"__ignoreMap":47},[51,1349,1350,1352,1354],{"class":53,"line":54},[51,1351,58],{"class":57},[51,1353,62],{"class":61},[51,1355,66],{"class":65},[51,1357,1358],{"class":53,"line":69},[51,1359,73],{"emptyLinePlaceholder":72},[51,1361,1362,1364,1366,1368,1370],{"class":53,"line":76},[51,1363,20],{"class":79},[51,1365,82],{"class":57},[51,1367,85],{"class":61},[51,1369,88],{"class":65},[51,1371,92],{"class":91},[51,1373,1374,1376,1378,1380,1382,1384],{"class":53,"line":95},[51,1375,99],{"class":98},[51,1377,102],{"class":91},[51,1379,106],{"class":105},[51,1381,1088],{"class":109},[51,1383,106],{"class":105},[51,1385,115],{"class":91},[14,1387,1388,1389,124],{},"If you see this error, read ",[386,1390,1392],{"href":1391},"\u002Ferrors\u002Fsyntaxerror-missing-colon-fix\u002F","SyntaxError: missing colon fix",[647,1394,1396,1397,1399,1400],{"id":1395},"using-instead-of","Using ",[18,1398,62],{}," instead of ",[18,1401,417],{},[14,1403,1404],{},"This is a very common mistake.",[14,1406,1407],{},"Wrong:",[42,1409,1411],{"className":44,"code":1410,"language":46,"meta":47,"style":47},"username = \"admin\"\n\nif username = \"admin\":\n    print(\"Welcome\")\n",[18,1412,1413,1425,1429,1445],{"__ignoreMap":47},[51,1414,1415,1417,1419,1421,1423],{"class":53,"line":54},[51,1416,578],{"class":57},[51,1418,62],{"class":61},[51,1420,583],{"class":105},[51,1422,586],{"class":109},[51,1424,589],{"class":105},[51,1426,1427],{"class":53,"line":69},[51,1428,73],{"emptyLinePlaceholder":72},[51,1430,1431,1433,1435,1437,1439,1441,1443],{"class":53,"line":76},[51,1432,20],{"class":79},[51,1434,600],{"class":57},[51,1436,62],{"class":61},[51,1438,583],{"class":105},[51,1440,586],{"class":109},[51,1442,106],{"class":105},[51,1444,92],{"class":91},[51,1446,1447,1449,1451,1453,1456,1458],{"class":53,"line":95},[51,1448,99],{"class":98},[51,1450,102],{"class":91},[51,1452,106],{"class":105},[51,1454,1455],{"class":109},"Welcome",[51,1457,106],{"class":105},[51,1459,115],{"class":91},[14,1461,1160],{},[42,1463,1465],{"className":44,"code":1464,"language":46,"meta":47,"style":47},"username = \"admin\"\n\nif username == \"admin\":\n    print(\"Welcome\")\n",[18,1466,1467,1479,1483,1499],{"__ignoreMap":47},[51,1468,1469,1471,1473,1475,1477],{"class":53,"line":54},[51,1470,578],{"class":57},[51,1472,62],{"class":61},[51,1474,583],{"class":105},[51,1476,586],{"class":109},[51,1478,589],{"class":105},[51,1480,1481],{"class":53,"line":69},[51,1482,73],{"emptyLinePlaceholder":72},[51,1484,1485,1487,1489,1491,1493,1495,1497],{"class":53,"line":76},[51,1486,20],{"class":79},[51,1488,600],{"class":57},[51,1490,417],{"class":61},[51,1492,583],{"class":105},[51,1494,586],{"class":109},[51,1496,106],{"class":105},[51,1498,92],{"class":91},[51,1500,1501,1503,1505,1507,1509,1511],{"class":53,"line":95},[51,1502,99],{"class":98},[51,1504,102],{"class":91},[51,1506,106],{"class":105},[51,1508,1455],{"class":109},[51,1510,106],{"class":105},[51,1512,115],{"class":91},[14,1514,1515],{},"Remember:",[129,1517,1518,1523],{},[132,1519,1520,1522],{},[18,1521,62],{}," assigns a value",[132,1524,1525,1527],{},[18,1526,417],{}," compares two values",[647,1529,1531],{"id":1530},"not-indenting-the-block","Not indenting the block",[14,1533,1407],{},[42,1535,1536],{"className":44,"code":1209,"language":46,"meta":47,"style":47},[18,1537,1538,1546,1550,1562],{"__ignoreMap":47},[51,1539,1540,1542,1544],{"class":53,"line":54},[51,1541,58],{"class":57},[51,1543,62],{"class":61},[51,1545,66],{"class":65},[51,1547,1548],{"class":53,"line":69},[51,1549,73],{"emptyLinePlaceholder":72},[51,1551,1552,1554,1556,1558,1560],{"class":53,"line":76},[51,1553,20],{"class":79},[51,1555,82],{"class":57},[51,1557,85],{"class":61},[51,1559,88],{"class":65},[51,1561,92],{"class":91},[51,1563,1564,1566,1568,1570,1572,1574],{"class":53,"line":95},[51,1565,471],{"class":98},[51,1567,102],{"class":91},[51,1569,106],{"class":105},[51,1571,1088],{"class":109},[51,1573,106],{"class":105},[51,1575,115],{"class":91},[14,1577,1160],{},[42,1579,1580],{"className":44,"code":1163,"language":46,"meta":47,"style":47},[18,1581,1582,1590,1594,1606],{"__ignoreMap":47},[51,1583,1584,1586,1588],{"class":53,"line":54},[51,1585,58],{"class":57},[51,1587,62],{"class":61},[51,1589,66],{"class":65},[51,1591,1592],{"class":53,"line":69},[51,1593,73],{"emptyLinePlaceholder":72},[51,1595,1596,1598,1600,1602,1604],{"class":53,"line":76},[51,1597,20],{"class":79},[51,1599,82],{"class":57},[51,1601,85],{"class":61},[51,1603,88],{"class":65},[51,1605,92],{"class":91},[51,1607,1608,1610,1612,1614,1616,1618],{"class":53,"line":95},[51,1609,99],{"class":98},[51,1611,102],{"class":91},[51,1613,106],{"class":105},[51,1615,1088],{"class":109},[51,1617,106],{"class":105},[51,1619,115],{"class":91},[14,1621,1622,1623,124],{},"If Python says it expected an indented block, see ",[386,1624,1626],{"href":1625},"\u002Ferrors\u002Findentationerror-expected-an-indented-block-fix\u002F","IndentationError: expected an indented block fix",[647,1628,1630],{"id":1629},"expecting-the-block-to-run-when-the-condition-is-false","Expecting the block to run when the condition is False",[14,1632,16,1633,1635,1636,124],{},[18,1634,20],{}," block only runs when the condition is ",[18,1637,27],{},[14,1639,1640],{},"If the condition is false, Python skips it.",[14,1642,1643],{},"To check what is happening, print the value and the condition result:",[42,1645,1647],{"className":44,"code":1646,"language":46,"meta":47,"style":47},"age = 16\n\nprint(age)\nprint(type(age))\nprint(age >= 18)\nprint(\"admin\" == \"admin\")\n",[18,1648,1649,1657,1661,1672,1689,1703],{"__ignoreMap":47},[51,1650,1651,1653,1655],{"class":53,"line":54},[51,1652,58],{"class":57},[51,1654,62],{"class":61},[51,1656,462],{"class":65},[51,1658,1659],{"class":53,"line":69},[51,1660,73],{"emptyLinePlaceholder":72},[51,1662,1663,1665,1667,1670],{"class":53,"line":76},[51,1664,471],{"class":98},[51,1666,102],{"class":91},[51,1668,1669],{"class":476},"age",[51,1671,115],{"class":91},[51,1673,1674,1676,1678,1682,1684,1686],{"class":53,"line":95},[51,1675,471],{"class":98},[51,1677,102],{"class":91},[51,1679,1681],{"class":1680},"sZMiF","type",[51,1683,102],{"class":91},[51,1685,1669],{"class":476},[51,1687,1688],{"class":91},"))\n",[51,1690,1691,1693,1695,1697,1699,1701],{"class":53,"line":1095},[51,1692,471],{"class":98},[51,1694,102],{"class":91},[51,1696,58],{"class":476},[51,1698,85],{"class":61},[51,1700,88],{"class":65},[51,1702,115],{"class":91},[51,1704,1705,1707,1709,1711,1713,1715,1718,1720,1722,1724],{"class":53,"line":1100},[51,1706,471],{"class":98},[51,1708,102],{"class":91},[51,1710,106],{"class":105},[51,1712,586],{"class":109},[51,1714,106],{"class":105},[51,1716,1717],{"class":61}," ==",[51,1719,583],{"class":105},[51,1721,586],{"class":109},[51,1723,106],{"class":105},[51,1725,115],{"class":91},[14,1727,1728],{},"Useful debugging checks:",[42,1730,1732],{"className":44,"code":1731,"language":46,"meta":47,"style":47},"print(age)\nprint(type(age))\nprint(age >= 18)\nprint(username == \"admin\")\n",[18,1733,1734,1744,1758,1772],{"__ignoreMap":47},[51,1735,1736,1738,1740,1742],{"class":53,"line":54},[51,1737,471],{"class":98},[51,1739,102],{"class":91},[51,1741,1669],{"class":476},[51,1743,115],{"class":91},[51,1745,1746,1748,1750,1752,1754,1756],{"class":53,"line":69},[51,1747,471],{"class":98},[51,1749,102],{"class":91},[51,1751,1681],{"class":1680},[51,1753,102],{"class":91},[51,1755,1669],{"class":476},[51,1757,1688],{"class":91},[51,1759,1760,1762,1764,1766,1768,1770],{"class":53,"line":76},[51,1761,471],{"class":98},[51,1763,102],{"class":91},[51,1765,58],{"class":476},[51,1767,85],{"class":61},[51,1769,88],{"class":65},[51,1771,115],{"class":91},[51,1773,1774,1776,1778,1780,1782,1784,1786,1788],{"class":53,"line":95},[51,1775,471],{"class":98},[51,1777,102],{"class":91},[51,1779,578],{"class":476},[51,1781,417],{"class":61},[51,1783,583],{"class":105},[51,1785,586],{"class":109},[51,1787,106],{"class":105},[51,1789,115],{"class":91},[14,1791,1792],{},"These help you confirm:",[129,1794,1795,1798,1801],{},[132,1796,1797],{},"the actual value",[132,1799,1800],{},"the data type",[132,1802,1803],{},"whether the condition is true or false",[647,1805,1807],{"id":1806},"comparing-unexpected-types","Comparing unexpected types",[14,1809,1810],{},"Sometimes a value looks correct but has the wrong type.",[14,1812,186],{},[42,1814,1816],{"className":44,"code":1815,"language":46,"meta":47,"style":47},"age = \"18\"\n\nif age >= 18:\n    print(\"Adult\")\n",[18,1817,1818,1831,1835,1847],{"__ignoreMap":47},[51,1819,1820,1822,1824,1826,1829],{"class":53,"line":54},[51,1821,58],{"class":57},[51,1823,62],{"class":61},[51,1825,583],{"class":105},[51,1827,1828],{"class":109},"18",[51,1830,589],{"class":105},[51,1832,1833],{"class":53,"line":69},[51,1834,73],{"emptyLinePlaceholder":72},[51,1836,1837,1839,1841,1843,1845],{"class":53,"line":76},[51,1838,20],{"class":79},[51,1840,82],{"class":57},[51,1842,85],{"class":61},[51,1844,88],{"class":65},[51,1846,92],{"class":91},[51,1848,1849,1851,1853,1855,1857,1859],{"class":53,"line":95},[51,1850,99],{"class":98},[51,1852,102],{"class":91},[51,1854,106],{"class":105},[51,1856,1088],{"class":109},[51,1858,106],{"class":105},[51,1860,115],{"class":91},[14,1862,1863,1864,1867],{},"This does not work because ",[18,1865,1866],{},"\"18\""," is a string, not a number.",[14,1869,1870],{},"You can inspect the type with:",[42,1872,1874],{"className":44,"code":1873,"language":46,"meta":47,"style":47},"age = \"18\"\nprint(type(age))\n",[18,1875,1876,1888],{"__ignoreMap":47},[51,1877,1878,1880,1882,1884,1886],{"class":53,"line":54},[51,1879,58],{"class":57},[51,1881,62],{"class":61},[51,1883,583],{"class":105},[51,1885,1828],{"class":109},[51,1887,589],{"class":105},[51,1889,1890,1892,1894,1896,1898,1900],{"class":53,"line":69},[51,1891,471],{"class":98},[51,1893,102],{"class":91},[51,1895,1681],{"class":1680},[51,1897,102],{"class":91},[51,1899,1669],{"class":476},[51,1901,1688],{"class":91},[37,1903,1905],{"id":1904},"faq","FAQ",[647,1907,1909],{"id":1908},"what-is-an-if-statement-in-python","What is an if statement in Python?",[14,1911,1912,1913,124],{},"It is a control statement that runs code only when a condition is ",[18,1914,27],{},[647,1916,1918],{"id":1917},"do-i-need-a-colon-after-if-in-python","Do I need a colon after if in Python?",[14,1920,1921,1922,1924],{},"Yes. The ",[18,1923,20],{}," line must end with a colon.",[647,1926,1928],{"id":1927},"what-is-the-difference-between-and-in-an-if-statement","What is the difference between = and == in an if statement?",[14,1930,1931,1933,1934,1936],{},[18,1932,62],{}," assigns a value. ",[18,1935,417],{}," compares two values.",[647,1938,1940],{"id":1939},"can-an-if-statement-run-more-than-one-line","Can an if statement run more than one line?",[14,1942,1943,1944,1946],{},"Yes. Any indented lines under the ",[18,1945,20],{}," statement are part of the block.",[14,1948,186],{},[42,1950,1952],{"className":44,"code":1951,"language":46,"meta":47,"style":47},"age = 18\n\nif age >= 18:\n    print(\"Adult\")\n    print(\"Access allowed\")\n",[18,1953,1954,1962,1966,1978,1992],{"__ignoreMap":47},[51,1955,1956,1958,1960],{"class":53,"line":54},[51,1957,58],{"class":57},[51,1959,62],{"class":61},[51,1961,66],{"class":65},[51,1963,1964],{"class":53,"line":69},[51,1965,73],{"emptyLinePlaceholder":72},[51,1967,1968,1970,1972,1974,1976],{"class":53,"line":76},[51,1969,20],{"class":79},[51,1971,82],{"class":57},[51,1973,85],{"class":61},[51,1975,88],{"class":65},[51,1977,92],{"class":91},[51,1979,1980,1982,1984,1986,1988,1990],{"class":53,"line":95},[51,1981,99],{"class":98},[51,1983,102],{"class":91},[51,1985,106],{"class":105},[51,1987,1088],{"class":109},[51,1989,106],{"class":105},[51,1991,115],{"class":91},[51,1993,1994,1996,1998,2000,2003,2005],{"class":53,"line":1095},[51,1995,99],{"class":98},[51,1997,102],{"class":91},[51,1999,106],{"class":105},[51,2001,2002],{"class":109},"Access allowed",[51,2004,106],{"class":105},[51,2006,115],{"class":91},[647,2008,2010],{"id":2009},"what-if-i-want-code-to-run-when-the-condition-is-false","What if I want code to run when the condition is False?",[14,2012,118,2013,2016,2017,2019],{},[18,2014,2015],{},"if-else",". That is covered on the ",[386,2018,1144],{"href":1143}," page.",[37,2021,2023],{"id":2022},"see-also","See also",[129,2025,2026,2030,2034,2038,2042],{},[132,2027,2028],{},[386,2029,638],{"href":637},[132,2031,2032],{},[386,2033,1144],{"href":1143},[132,2035,2036],{},[386,2037,1278],{"href":1277},[132,2039,2040],{},[386,2041,389],{"href":388},[132,2043,2044],{},[386,2045,1909],{"href":2046},"\u002Fglossary\u002Fwhat-is-an-if-statement-in-python\u002F",[14,2048,2049,2050,2052,2053,2055,2056,2055,2059,2062],{},"Once you understand a single ",[18,2051,20],{}," statement, the next step is to learn how to build full decision logic with ",[18,2054,20],{},", ",[18,2057,2058],{},"else",[18,2060,2061],{},"elif",", and boolean expressions.",[2064,2065,2066],"style",{},"html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--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 .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}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 .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 .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":47,"searchDepth":69,"depth":69,"links":2068},[2069,2070,2071,2072,2073,2079,2080,2081,2082,2090,2097],{"id":39,"depth":69,"text":40},{"id":149,"depth":69,"text":150},{"id":268,"depth":69,"text":269},{"id":392,"depth":69,"text":393},{"id":641,"depth":69,"text":642,"children":2074},[2075,2076,2077,2078],{"id":649,"depth":76,"text":650},{"id":701,"depth":76,"text":702},{"id":760,"depth":76,"text":761},{"id":812,"depth":76,"text":813},{"id":875,"depth":69,"text":876},{"id":1036,"depth":69,"text":1037},{"id":1147,"depth":69,"text":1148},{"id":1281,"depth":69,"text":1282,"children":2083},[2084,2085,2087,2088,2089],{"id":1290,"depth":76,"text":1291},{"id":1395,"depth":76,"text":2086},"Using = instead of ==",{"id":1530,"depth":76,"text":1531},{"id":1629,"depth":76,"text":1630},{"id":1806,"depth":76,"text":1807},{"id":1904,"depth":69,"text":1905,"children":2091},[2092,2093,2094,2095,2096],{"id":1908,"depth":76,"text":1909},{"id":1917,"depth":76,"text":1918},{"id":1927,"depth":76,"text":1928},{"id":1939,"depth":76,"text":1940},{"id":2009,"depth":76,"text":2010},{"id":2022,"depth":69,"text":2023},"Master python if statements explained in our comprehensive Python beginner guide.","md",{},"\u002Flearn\u002Fpython-if-statements-explained",{"title":5,"description":2098},"learn\u002Fpython-if-statements-explained","EcoJ8FYLrdjhgFEFzZIYBrEb8aJtnNzGZx3r8iF3j7c",1777585498419]