{"id":60,"date":"2018-09-18T23:47:54","date_gmt":"2018-09-19T03:47:54","guid":{"rendered":"http:\/\/sites.nd.edu\/code-fair\/?p=60"},"modified":"2018-10-31T12:49:40","modified_gmt":"2018-10-31T16:49:40","slug":"how-to-use-python","status":"publish","type":"post","link":"https:\/\/sites.nd.edu\/code-fair\/2018\/09\/18\/how-to-use-python\/","title":{"rendered":"How to Use Python"},"content":{"rendered":"<p>Python is a popular programming language known for being simultaneously powerful and easy to learn and use. Several projects on this site use Python. This page will show how to set up and use a Python environment on your computer, not how to use the language itself. If you don&#8217;t know how to code in Python and want to learn, take a look at any of the many Python tutorials available for free online, like <a href=\"https:\/\/docs.python.org\/3\/tutorial\/\" rel=\"noopener\" target=\"_blank\">this one<\/a>. <\/p>\n<h3><b>Set Up Python<\/b><\/h3>\n<h4><b>Windows<\/b><\/h4>\n<ol>\n<li>Download the installer from <a href=\"https:\/\/www.python.org\/downloads\/\" rel=\"noopener\" target=\"_blank\">the official Python website<\/a>. The exact version number may vary from what is shown here, but it should look like <code>3.X.X<\/code><\/li>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/install_python_windows.png\" alt=\"\" width=\"591\" height=\"282\" class=\"alignnone size-full wp-image-110\" srcset=\"https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/install_python_windows.png 591w, https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/install_python_windows-300x143.png 300w\" sizes=\"auto, (max-width: 591px) 100vw, 591px\" \/><\/p>\n<li>Run the downloaded file, following the instructions onscreen. <b>Make sure that you check the box to add it to PATH.<\/b> This is essential to running Python easily from a command prompt.<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/install_python_windows_installer.png\" alt=\"\" width=\"663\" height=\"383\" class=\"alignnone size-full wp-image-111\" srcset=\"https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/install_python_windows_installer.png 663w, https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/install_python_windows_installer-300x173.png 300w\" sizes=\"auto, (max-width: 663px) 100vw, 663px\" \/><\/p>\n<li>Open a command prompt. This can be done by searching &#8220;cmd&#8221; in the Start menu, or pressing Windows+R and typing &#8220;cmd&#8221;.<\/li>\n<li>Type <code>python --version<\/code> at the command prompt to verify that Python was installed. You should get a version number <code>3.X.X<\/code>. If you instead get an error, either Python was not installed correctly or it was not added to PATH.<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/install_python_windows_cmd_verify.png\" alt=\"\" width=\"960\" height=\"418\" class=\"alignnone size-full wp-image-112\" srcset=\"https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/install_python_windows_cmd_verify.png 960w, https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/install_python_windows_cmd_verify-300x131.png 300w, https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/install_python_windows_cmd_verify-768x334.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/>\n<\/ol>\n<h3><b>Install Modules<\/b><\/h3>\n<p>Modules are sets of code pre-written for doing common tasks, like creating graphs, loading images, or doing certain kinds of math. Some modules come pre-installed with Python, but some Python scripts require you to install certain modules from the internet before they can run. Luckily, installing modules is very easy. <\/p>\n<ol>\n<li>Make sure your computer is connected to the internet.<\/li>\n<li>Open a command prompt and type <code>python -m pip install NameOfTheModule<\/code>. Wait until the installation process returns you to the command prompt.<\/li>\n<li>You&#8217;re done! You only have to do this once for any module required by a script.<\/li>\n<\/ol>\n<h3><b>Create, Edit, and Run Python Scripts<\/b><\/h3>\n<h4><b>IDLE<\/b><\/h4>\n<p>The Python installer, in addition to installing Python, installed a program called IDLE. IDLE is an Integrated Development Environment, meaning that it gives you a set of tools to create, edit and run Python scripts.<\/p>\n<ol>\n<li>Open IDLE.<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/python_shell.png\" alt=\"\" width=\"589\" height=\"641\" class=\"alignnone size-full wp-image-114\" srcset=\"https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/python_shell.png 589w, https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/python_shell-276x300.png 276w\" sizes=\"auto, (max-width: 589px) 100vw, 589px\" \/><\/p>\n<li>This window is called the &#8220;shell.&#8221; You can execute single lines of Python code at a time by typing them into the prompt and hitting Enter\/Return. Start out by trying simple commands like <code>print(\"hello\")<\/code> or <code>sum([1, 4, 6, 7])<\/code> to get the hang of it.<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/python_shell_simple_commands.png\" alt=\"\" width=\"589\" height=\"642\" class=\"alignnone size-full wp-image-123\" srcset=\"https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/python_shell_simple_commands.png 589w, https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/python_shell_simple_commands-275x300.png 275w\" sizes=\"auto, (max-width: 589px) 100vw, 589px\" \/><\/p>\n<li>While typing directly into the shell is good for doing relatively simple tasks, you will quickly find that doing anything very complicated or repeating previous commands takes a lot of retyping and\/or copy-pasting. Luckily for you, there is a way to save many lines of code at once and have them executed in order on command without having to retype them.<\/li>\n<li>In the shell menu, go to <code>File -&gt; New File<\/code> to create a new Python code (<code>.py<\/code>) file. A blank editor window should come up. Notice that your shell window is still open &#8211; don&#8217;t close it; you&#8217;ll need it!<\/li>\n<li>Now, type in some Python commands, one on each line. You can use as many lines as you want; the commands will be executed in order, starting with the first line and ending with the last.<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/python_edit_simple.png\" alt=\"\" width=\"566\" height=\"104\" class=\"alignnone size-full wp-image-121\" srcset=\"https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/python_edit_simple.png 566w, https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/python_edit_simple-300x55.png 300w\" sizes=\"auto, (max-width: 566px) 100vw, 566px\" \/><\/p>\n<li>To run your code, go the the menu option <code>Run -&gt; Run Module<\/code>. Name and save your file when it prompts you to do so. Notice that the file extension it is saved as is <code>.py<\/code>, signifying that it is a Python program. You can open, edit and run any <code>.py<\/code> file with IDLE<\/li>\n<li>When you run your file, your shell window will be brought to the front, a <code>== RESTART ==<\/code> line will appear, and the output of your code will be displayed in blue. When your program finishes, the prompt <code>&gt;&gt;&gt;<\/code> will appear again and you can type in the shell again like normal.<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/python_run_simple.png\" alt=\"\" width=\"566\" height=\"99\" class=\"alignnone size-full wp-image-120\" srcset=\"https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/python_run_simple.png 566w, https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/python_run_simple-300x52.png 300w\" sizes=\"auto, (max-width: 566px) 100vw, 566px\" \/><\/p>\n<li>Tip: if you ever want to stop your running program and get back to the shell prompt immediately, hit <code>Ctrl-C<\/code>, <code>Command-C<\/code>, or <code>Shift-F6<\/code>.<\/li>\n<\/ol>\n<h4><b>Command Prompt<\/b><\/h4>\n<p>While IDLE will run any Python file, sometimes you may want to run a <code>.py<\/code> file from the command line. Perhaps you are working on someone else&#8217;s computer and IDLE isn&#8217;t installed, or you already have a command prompt open, or you just like working from a command prompt better. In any case, it&#8217;s a good skill to know. Assuming that you have already installed Python on your computer: <\/p>\n<ol>\n<li>Open a command prompt.<\/li>\n<li>Find the file path to the <code>.py<\/code> file you want to run. It should look something like <code>C:\/This\/Is\/A\/Windows\/File\/Path\/<\/code> or <code>\/This\/Is\/An\/Apple\/File\/Path\/<\/code><\/li>\n<li>In the command prompt, type <code>cd \"\/Your\/File\/Path\/\"<\/code>, including the quotes. This <code>cd<\/code> command tells the prompt where to look for your Python file.<\/li>\n<li>Finally, type <code>python YourFileName.py<\/code>. The output of that file will show in the command prompt, like it did in the IDLE shell.<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/run_cmd_cd.png\" alt=\"\" width=\"501\" height=\"120\" class=\"alignnone size-full wp-image-125\" srcset=\"https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/run_cmd_cd.png 501w, https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/run_cmd_cd-300x72.png 300w\" sizes=\"auto, (max-width: 501px) 100vw, 501px\" \/><\/p>\n<li>Alternately, instead of using the <code>cd<\/code> command and then the <code>python -m<\/code> command in steps (3) and (4), you could type <code>python \"\/Your\/File\/Path\/YourFileName.py\"<\/code>. Either way works.<\/li>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/run_cmd_direct.png\" alt=\"\" width=\"615\" height=\"90\" class=\"alignnone size-full wp-image-118\" srcset=\"https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/run_cmd_direct.png 615w, https:\/\/sites.nd.edu\/code-fair\/files\/2018\/09\/run_cmd_direct-300x44.png 300w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Python is a popular programming language known for being simultaneously powerful and easy to learn and use. Several projects on this site use Python. This page will show how to set up and use a Python environment on your computer, not how to use the language itself. If you don&#8217;t know how to code in &hellip; <\/p>\n<p><a class=\"more-link block-button\" href=\"https:\/\/sites.nd.edu\/code-fair\/2018\/09\/18\/how-to-use-python\/\">Continue reading &raquo;<\/a><\/p>\n","protected":false},"author":3185,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10981],"tags":[],"class_list":["post-60","post","type-post","status-publish","format-standard","hentry","category-how-to","nodate"],"_links":{"self":[{"href":"https:\/\/sites.nd.edu\/code-fair\/wp-json\/wp\/v2\/posts\/60","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sites.nd.edu\/code-fair\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sites.nd.edu\/code-fair\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sites.nd.edu\/code-fair\/wp-json\/wp\/v2\/users\/3185"}],"replies":[{"embeddable":true,"href":"https:\/\/sites.nd.edu\/code-fair\/wp-json\/wp\/v2\/comments?post=60"}],"version-history":[{"count":10,"href":"https:\/\/sites.nd.edu\/code-fair\/wp-json\/wp\/v2\/posts\/60\/revisions"}],"predecessor-version":[{"id":126,"href":"https:\/\/sites.nd.edu\/code-fair\/wp-json\/wp\/v2\/posts\/60\/revisions\/126"}],"wp:attachment":[{"href":"https:\/\/sites.nd.edu\/code-fair\/wp-json\/wp\/v2\/media?parent=60"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sites.nd.edu\/code-fair\/wp-json\/wp\/v2\/categories?post=60"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sites.nd.edu\/code-fair\/wp-json\/wp\/v2\/tags?post=60"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}