[{"data":1,"prerenderedAt":959},["ShallowReactive",2],{"doc-\u002Fexamples\u002Fpython-hello-world-example-explained":3},{"id":4,"title":5,"body":6,"description":951,"extension":952,"meta":953,"navigation":954,"path":955,"seo":956,"stem":957,"__hash__":958},"content\u002Fexamples\u002Fpython-hello-world-example-explained.md","Python Hello World Example Explained",{"type":7,"value":8,"toc":918},"minimark",[9,13,17,53,60,65,68,84,87,91,94,112,115,135,147,151,154,162,165,203,206,217,221,224,229,232,244,247,256,259,277,280,285,289,296,314,317,329,332,344,348,351,359,362,369,373,376,394,397,420,423,440,448,452,455,459,479,483,503,510,545,548,554,557,561,596,599,603,606,610,613,635,641,644,662,669,675,678,698,701,704,722,726,729,750,753,771,775,778,781,814,817,836,843,847,854,860,864,867,871,874,878,881,885,914],[10,11,5],"h1",{"id":12},"python-hello-world-example-explained",[14,15,16],"p",{},"This beginner-friendly example shows the simplest useful Python program. You will learn what it does, how to run it, and how to fix a few common mistakes.",[18,19,24],"pre",{"className":20,"code":21,"language":22,"meta":23,"style":23},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","print(\"Hello, world!\")\n","python","",[25,26,27],"code",{"__ignoreMap":23},[28,29,32,36,40,44,48,50],"span",{"class":30,"line":31},"line",1,[28,33,35],{"class":34},"sptTA","print",[28,37,39],{"class":38},"sP7_E","(",[28,41,43],{"class":42},"sjJ54","\"",[28,45,47],{"class":46},"s_sjI","Hello, world!",[28,49,43],{"class":42},[28,51,52],{"class":38},")\n",[14,54,55,56,59],{},"Save this in a ",[25,57,58],{},".py"," file or run it in the Python interpreter to see your first working Python output.",[61,62,64],"h2",{"id":63},"what-this-example-shows","What this example shows",[14,66,67],{},"This example teaches a few important basics:",[69,70,71,75,78,81],"ul",{},[72,73,74],"li",{},"The smallest useful Python program",[72,76,77],{},"How to display text on the screen",[72,79,80],{},"How Python runs code from top to bottom",[72,82,83],{},"Why this is often the first program beginners write",[14,85,86],{},"Even though it is only one line long, it introduces an important idea: Python can execute your code and produce visible output right away.",[61,88,90],{"id":89},"the-hello-world-code","The Hello World code",[14,92,93],{},"Here is the full program:",[18,95,96],{"className":20,"code":21,"language":22,"meta":23,"style":23},[25,97,98],{"__ignoreMap":23},[28,99,100,102,104,106,108,110],{"class":30,"line":31},[28,101,35],{"class":34},[28,103,39],{"class":38},[28,105,43],{"class":42},[28,107,47],{"class":46},[28,109,43],{"class":42},[28,111,52],{"class":38},[14,113,114],{},"What this line means:",[69,116,117,123,129,132],{},[72,118,119,122],{},[25,120,121],{},"print()"," sends output to the screen",[72,124,125,128],{},[25,126,127],{},"\"Hello, world!\""," is the text to display",[72,130,131],{},"The text is inside quotes, so Python treats it as a string",[72,133,134],{},"In Python 3, the parentheses are required",[14,136,137,138,146],{},"If you want a deeper explanation of this function, see ",[139,140,142,143,145],"a",{"href":141},"\u002Freference\u002Fpython-print-function-explained\u002F","Python ",[25,144,121],{}," function explained",".",[61,148,150],{"id":149},"expected-output","Expected output",[14,152,153],{},"When you run the code, the output should be:",[18,155,160],{"className":156,"code":158,"language":159,"meta":23},[157],"language-text","Hello, world!\n","text",[25,161,158],{"__ignoreMap":23},[14,163,164],{},"A beginner mistake is to confuse code with output.",[69,166,167,192],{},[72,168,169,173,174],{},[170,171,172],"strong",{},"Code"," is what you write:\n",[18,175,176],{"className":20,"code":21,"language":22,"meta":23,"style":23},[25,177,178],{"__ignoreMap":23},[28,179,180,182,184,186,188,190],{"class":30,"line":31},[28,181,35],{"class":34},[28,183,39],{"class":38},[28,185,43],{"class":42},[28,187,47],{"class":46},[28,189,43],{"class":42},[28,191,52],{"class":38},[72,193,194,197,198],{},[170,195,196],{},"Output"," is what Python shows after running the code:\n",[18,199,201],{"className":200,"code":158,"language":159,"meta":23},[157],[25,202,158],{"__ignoreMap":23},[14,204,205],{},"You will usually see the output in one of these places:",[69,207,208,211,214],{},[72,209,210],{},"The terminal",[72,212,213],{},"The Python shell",[72,215,216],{},"The output panel in an IDE",[61,218,220],{"id":219},"how-to-run-the-example","How to run the example",[14,222,223],{},"Before running the program, make sure Python is installed on your computer.",[225,226,228],"h3",{"id":227},"run-it-in-the-python-interpreter","Run it in the Python interpreter",[14,230,231],{},"Open a terminal or command prompt and start Python:",[18,233,237],{"className":234,"code":235,"language":236,"meta":23,"style":23},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","python\n","bash",[25,238,239],{"__ignoreMap":23},[28,240,241],{"class":30,"line":31},[28,242,235],{"class":243},"sbgvK",[14,245,246],{},"On some systems, you may need:",[18,248,250],{"className":234,"code":249,"language":236,"meta":23,"style":23},"python3\n",[25,251,252],{"__ignoreMap":23},[28,253,254],{"class":30,"line":31},[28,255,249],{"class":243},[14,257,258],{},"Then type:",[18,260,261],{"className":20,"code":21,"language":22,"meta":23,"style":23},[25,262,263],{"__ignoreMap":23},[28,264,265,267,269,271,273,275],{"class":30,"line":31},[28,266,35],{"class":34},[28,268,39],{"class":38},[28,270,43],{"class":42},[28,272,47],{"class":46},[28,274,43],{"class":42},[28,276,52],{"class":38},[14,278,279],{},"Press Enter, and Python should display:",[18,281,283],{"className":282,"code":158,"language":159,"meta":23},[157],[25,284,158],{"__ignoreMap":23},[225,286,288],{"id":287},"run-it-from-a-file","Run it from a file",[14,290,291,292,295],{},"Create a file named ",[25,293,294],{},"hello.py"," and put this code inside it:",[18,297,298],{"className":20,"code":21,"language":22,"meta":23,"style":23},[25,299,300],{"__ignoreMap":23},[28,301,302,304,306,308,310,312],{"class":30,"line":31},[28,303,35],{"class":34},[28,305,39],{"class":38},[28,307,43],{"class":42},[28,309,47],{"class":46},[28,311,43],{"class":42},[28,313,52],{"class":38},[14,315,316],{},"Then run the file from your terminal:",[18,318,320],{"className":234,"code":319,"language":236,"meta":23,"style":23},"python hello.py\n",[25,321,322],{"__ignoreMap":23},[28,323,324,326],{"class":30,"line":31},[28,325,22],{"class":243},[28,327,328],{"class":46}," hello.py\n",[14,330,331],{},"Or, on some systems:",[18,333,335],{"className":234,"code":334,"language":236,"meta":23,"style":23},"python3 hello.py\n",[25,336,337],{"__ignoreMap":23},[28,338,339,342],{"class":30,"line":31},[28,340,341],{"class":243},"python3",[28,343,328],{"class":46},[225,345,347],{"id":346},"run-it-in-an-ide","Run it in an IDE",[14,349,350],{},"You can also run the program in tools like:",[69,352,353,356],{},[72,354,355],{},"VS Code",[72,357,358],{},"IDLE",[14,360,361],{},"Create a new Python file, paste in the code, and use the IDE's run button or menu option.",[14,363,364,365,146],{},"If you need more help with this step, see ",[139,366,368],{"href":367},"\u002Flearn\u002Fhow-to-run-python-code-command-line-and-ides","how to run Python code from the command line and IDEs",[61,370,372],{"id":371},"line-by-line-explanation","Line-by-line explanation",[14,374,375],{},"This program has only one line:",[18,377,378],{"className":20,"code":21,"language":22,"meta":23,"style":23},[25,379,380],{"__ignoreMap":23},[28,381,382,384,386,388,390,392],{"class":30,"line":31},[28,383,35],{"class":34},[28,385,39],{"class":38},[28,387,43],{"class":42},[28,389,47],{"class":46},[28,391,43],{"class":42},[28,393,52],{"class":38},[14,395,396],{},"Here is what each part does:",[69,398,399,404,411,417],{},[72,400,401,403],{},[25,402,35],{}," is a built-in Python function",[72,405,406,407,410],{},"The parentheses ",[25,408,409],{},"()"," hold the value passed to the function",[72,412,413,414,416],{},"The quotes tell Python that ",[25,415,47],{}," is text, not code",[72,418,419],{},"After printing the message, the program ends",[14,421,422],{},"You can think of it like this:",[69,424,425,433],{},[72,426,427,430,431],{},[170,428,429],{},"Function name:"," ",[25,432,35],{},[72,434,435,430,438],{},[170,436,437],{},"Value given to the function:",[25,439,127],{},[14,441,442,443,447],{},"If you are new to Python syntax, ",[139,444,446],{"href":445},"\u002Flearn\u002Fpython-syntax-basics-explained\u002F","Python syntax basics explained"," is a good next step.",[61,449,451],{"id":450},"simple-variations-to-try","Simple variations to try",[14,453,454],{},"Once the first version works, try changing it.",[225,456,458],{"id":457},"change-the-message-text","Change the message text",[18,460,462],{"className":20,"code":461,"language":22,"meta":23,"style":23},"print(\"Welcome to Python!\")\n",[25,463,464],{"__ignoreMap":23},[28,465,466,468,470,472,475,477],{"class":30,"line":31},[28,467,35],{"class":34},[28,469,39],{"class":38},[28,471,43],{"class":42},[28,473,474],{"class":46},"Welcome to Python!",[28,476,43],{"class":42},[28,478,52],{"class":38},[225,480,482],{"id":481},"print-your-name","Print your name",[18,484,486],{"className":20,"code":485,"language":22,"meta":23,"style":23},"print(\"My name is Sam\")\n",[25,487,488],{"__ignoreMap":23},[28,489,490,492,494,496,499,501],{"class":30,"line":31},[28,491,35],{"class":34},[28,493,39],{"class":38},[28,495,43],{"class":42},[28,497,498],{"class":46},"My name is Sam",[28,500,43],{"class":42},[28,502,52],{"class":38},[225,504,506,507,509],{"id":505},"use-two-print-lines","Use two ",[25,508,121],{}," lines",[18,511,513],{"className":20,"code":512,"language":22,"meta":23,"style":23},"print(\"Hello, world!\")\nprint(\"I am learning Python.\")\n",[25,514,515,529],{"__ignoreMap":23},[28,516,517,519,521,523,525,527],{"class":30,"line":31},[28,518,35],{"class":34},[28,520,39],{"class":38},[28,522,43],{"class":42},[28,524,47],{"class":46},[28,526,43],{"class":42},[28,528,52],{"class":38},[28,530,532,534,536,538,541,543],{"class":30,"line":531},2,[28,533,35],{"class":34},[28,535,39],{"class":38},[28,537,43],{"class":42},[28,539,540],{"class":46},"I am learning Python.",[28,542,43],{"class":42},[28,544,52],{"class":38},[14,546,547],{},"Expected output:",[18,549,552],{"className":550,"code":551,"language":159,"meta":23},[157],"Hello, world!\nI am learning Python.\n",[25,553,551],{"__ignoreMap":23},[14,555,556],{},"Python runs code from top to bottom, so the first line prints before the second line.",[225,558,560],{"id":559},"store-text-in-a-variable-and-print-the-variable","Store text in a variable and print the variable",[18,562,564],{"className":20,"code":563,"language":22,"meta":23,"style":23},"message = \"Hello, world!\"\nprint(message)\n",[25,565,566,584],{"__ignoreMap":23},[28,567,568,572,576,579,581],{"class":30,"line":31},[28,569,571],{"class":570},"su5hD","message ",[28,573,575],{"class":574},"smGrS","=",[28,577,578],{"class":42}," \"",[28,580,47],{"class":46},[28,582,583],{"class":42},"\"\n",[28,585,586,588,590,594],{"class":30,"line":531},[28,587,35],{"class":34},[28,589,39],{"class":38},[28,591,593],{"class":592},"slqww","message",[28,595,52],{"class":38},[14,597,598],{},"This introduces a variable, which is a name that stores a value.",[61,600,602],{"id":601},"common-beginner-mistakes","Common beginner mistakes",[14,604,605],{},"Here are some problems beginners often run into.",[225,607,609],{"id":608},"forgetting-quotation-marks","Forgetting quotation marks",[14,611,612],{},"This code is wrong:",[18,614,616],{"className":20,"code":615,"language":22,"meta":23,"style":23},"print(Hello, world!)\n",[25,617,618],{"__ignoreMap":23},[28,619,620,622,624,627,630,633],{"class":30,"line":31},[28,621,35],{"class":34},[28,623,39],{"class":38},[28,625,626],{"class":592},"Hello",[28,628,629],{"class":38},",",[28,631,632],{"class":592}," world!",[28,634,52],{"class":38},[14,636,637,638,640],{},"Python will not understand ",[25,639,47],{}," as text because it is not inside quotes.",[14,642,643],{},"Use this instead:",[18,645,646],{"className":20,"code":21,"language":22,"meta":23,"style":23},[25,647,648],{"__ignoreMap":23},[28,649,650,652,654,656,658,660],{"class":30,"line":31},[28,651,35],{"class":34},[28,653,39],{"class":38},[28,655,43],{"class":42},[28,657,47],{"class":46},[28,659,43],{"class":42},[28,661,52],{"class":38},[14,663,664,665,146],{},"If you forget quotes, you may get an error such as ",[139,666,668],{"href":667},"\u002Ferrors\u002Fnameerror-name-is-not-defined-fix\u002F","NameError: name is not defined",[225,670,672,673],{"id":671},"misspelling-print","Misspelling ",[25,674,35],{},[14,676,677],{},"This is wrong:",[18,679,681],{"className":20,"code":680,"language":22,"meta":23,"style":23},"pritn(\"Hello, world!\")\n",[25,682,683],{"__ignoreMap":23},[28,684,685,688,690,692,694,696],{"class":30,"line":31},[28,686,687],{"class":592},"pritn",[28,689,39],{"class":38},[28,691,43],{"class":42},[28,693,47],{"class":46},[28,695,43],{"class":42},[28,697,52],{"class":38},[14,699,700],{},"Function names must be spelled exactly right.",[14,702,703],{},"Correct version:",[18,705,706],{"className":20,"code":21,"language":22,"meta":23,"style":23},[25,707,708],{"__ignoreMap":23},[28,709,710,712,714,716,718,720],{"class":30,"line":31},[28,711,35],{"class":34},[28,713,39],{"class":38},[28,715,43],{"class":42},[28,717,47],{"class":46},[28,719,43],{"class":42},[28,721,52],{"class":38},[225,723,725],{"id":724},"using-smart-quotes","Using smart quotes",[14,727,728],{},"Sometimes code copied from a document or messaging app uses curly quotes like these:",[18,730,732],{"className":20,"code":731,"language":22,"meta":23,"style":23},"print(“Hello, world!”)\n",[25,733,734],{"__ignoreMap":23},[28,735,736,738,740,743,745,748],{"class":30,"line":31},[28,737,35],{"class":34},[28,739,39],{"class":38},[28,741,742],{"class":592},"“Hello",[28,744,629],{"class":38},[28,746,747],{"class":592}," world!”",[28,749,52],{"class":38},[14,751,752],{},"Those are not normal Python string quotes. Use straight quotes instead:",[18,754,755],{"className":20,"code":21,"language":22,"meta":23,"style":23},[25,756,757],{"__ignoreMap":23},[28,758,759,761,763,765,767,769],{"class":30,"line":31},[28,760,35],{"class":34},[28,762,39],{"class":38},[28,764,43],{"class":42},[28,766,47],{"class":46},[28,768,43],{"class":42},[28,770,52],{"class":38},[225,772,774],{"id":773},"running-python-code-in-the-wrong-tool","Running Python code in the wrong tool",[14,776,777],{},"Make sure you are actually running Python.",[14,779,780],{},"Useful commands to check:",[18,782,784],{"className":234,"code":783,"language":236,"meta":23,"style":23},"python --version\npython hello.py\npython3 --version\npython3 hello.py\n",[25,785,786,794,800,807],{"__ignoreMap":23},[28,787,788,790],{"class":30,"line":31},[28,789,22],{"class":243},[28,791,793],{"class":792},"stzsN"," --version\n",[28,795,796,798],{"class":30,"line":531},[28,797,22],{"class":243},[28,799,328],{"class":46},[28,801,803,805],{"class":30,"line":802},3,[28,804,341],{"class":243},[28,806,793],{"class":792},[28,808,810,812],{"class":30,"line":809},4,[28,811,341],{"class":243},[28,813,328],{"class":46},[14,815,816],{},"Common causes of problems include:",[69,818,819,822,825,830,833],{},[72,820,821],{},"Python is not installed or not added to PATH",[72,823,824],{},"The file is saved with the wrong extension",[72,826,827,828],{},"Quotes are missing around ",[25,829,47],{},[72,831,832],{},"The code uses curly quotes instead of normal quotes",[72,834,835],{},"The program is run with the wrong command",[14,837,838,839,146],{},"If Python reports a syntax problem, see ",[139,840,842],{"href":841},"\u002Ferrors\u002Fsyntaxerror-invalid-syntax-fix\u002F","SyntaxError: invalid syntax",[61,844,846],{"id":845},"faq","FAQ",[225,848,850,851,853],{"id":849},"why-does-hello-world-use-print","Why does Hello World use ",[25,852,121],{},"?",[14,855,856,857,859],{},"Because ",[25,858,121],{}," is the simplest way to show output on the screen in Python.",[225,861,863],{"id":862},"do-i-need-to-save-the-code-in-a-file-first","Do I need to save the code in a file first?",[14,865,866],{},"No. You can run it directly in the Python interpreter or save it in a file and run that file.",[225,868,870],{"id":869},"why-am-i-getting-a-syntax-error","Why am I getting a syntax error?",[14,872,873],{},"This usually happens if the quotes, parentheses, or spelling are incorrect.",[225,875,877],{"id":876},"what-should-i-learn-after-hello-world","What should I learn after Hello World?",[14,879,880],{},"A good next step is Python syntax, variables, strings, and how to run Python code.",[61,882,884],{"id":883},"see-also","See also",[69,886,887,893,898,904,908],{},[72,888,889],{},[139,890,892],{"href":891},"\u002Flearn\u002Fyour-first-python-program-hello-world-explained\u002F","Your first Python program: Hello World explained",[72,894,895],{},[139,896,897],{"href":367},"How to run Python code from the command line and IDEs",[72,899,900],{},[139,901,142,902,145],{"href":141},[25,903,121],{},[72,905,906],{},[139,907,446],{"href":445},[72,909,910],{},[139,911,913],{"href":912},"\u002Flearn\u002Fpython-strings-explained-basics-and-examples\u002F","Python strings explained: basics and examples",[915,916,917],"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 .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 .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}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 .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":23,"searchDepth":531,"depth":531,"links":919},[920,921,922,923,928,929,936,943,950],{"id":63,"depth":531,"text":64},{"id":89,"depth":531,"text":90},{"id":149,"depth":531,"text":150},{"id":219,"depth":531,"text":220,"children":924},[925,926,927],{"id":227,"depth":802,"text":228},{"id":287,"depth":802,"text":288},{"id":346,"depth":802,"text":347},{"id":371,"depth":531,"text":372},{"id":450,"depth":531,"text":451,"children":930},[931,932,933,935],{"id":457,"depth":802,"text":458},{"id":481,"depth":802,"text":482},{"id":505,"depth":802,"text":934},"Use two print() lines",{"id":559,"depth":802,"text":560},{"id":601,"depth":531,"text":602,"children":937},[938,939,941,942],{"id":608,"depth":802,"text":609},{"id":671,"depth":802,"text":940},"Misspelling print",{"id":724,"depth":802,"text":725},{"id":773,"depth":802,"text":774},{"id":845,"depth":531,"text":846,"children":944},[945,947,948,949],{"id":849,"depth":802,"text":946},"Why does Hello World use print()?",{"id":862,"depth":802,"text":863},{"id":869,"depth":802,"text":870},{"id":876,"depth":802,"text":877},{"id":883,"depth":531,"text":884},"Master python hello world example explained in our comprehensive Python beginner guide.","md",{},true,"\u002Fexamples\u002Fpython-hello-world-example-explained",{"title":5,"description":951},"examples\u002Fpython-hello-world-example-explained","JlHu0l2oJg-6wu7EYwTMINheQzF8c63goXlFrcLXfqI",1777585507268]