{"cells":[{"cell_type":"markdown","id":"d185c582-e662-4362-8b9d-aec2d9c45668","metadata":{},"outputs":[],"source":["<p style=\"text-align:center\">\n","    <a href=\"https://skills.network\" target=\"_blank\">\n","    <img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/assets/logos/SN_web_lightmode.png\" width=\"300\" alt=\"Skills Network Logo\">\n","    </a>\n","</p>\n","\n","\n","# Writing Your First Python Code\n","\n","\n","Estimated time needed: **10** minutes\n","    \n","\n","## Objectives\n","\n","After completing this lab you will be able to:\n","* Write your basic python code\n","\n"]},{"cell_type":"markdown","id":"742f23b0-6ced-4f75-a0d7-580e23561492","metadata":{},"outputs":[],"source":["<h2>Table of Contents</h2>\n","<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n","    <ul>\n","        <li>\n","            <a href=\"#Say-'Hello'-to-the-world-in-Python\">Say 'Hello' to the world in Python</a>\n","            <ul>\n","                <li><a href=\"#What-version-of-Python-are-we-using?\">What version of Python are we using?</a></li>\n","                <li><a href=\"#Writing-comments-in-Python\">Writing comments in Python</a></li>\n","                <li><a href=\"#Errors-in-Python\">Errors in Python</a></li>\n","                <li><a href=\"#Does-Python-know-about-your-error-before-it-runs-your-code?\">Does Python know about your error before it runs your code?</a></li>\n","                <li><a href=\"#Exercise:-Your-First-Program\">Exercise: Your First Program</a></li>\n","            </ul>\n","        </li>\n","       \n","   \n"]},{"cell_type":"markdown","id":"6b37482f-6569-4dd2-9233-be0a13265d1c","metadata":{},"outputs":[],"source":["## Say 'Hello' to the world in Python\n"]},{"cell_type":"markdown","id":"0b328da0-9f69-4c28-bf02-4ae1a70abc91","metadata":{},"outputs":[],"source":["When learning a new programming language, it is customary to start with an \"hello world\" example. As simple as it is, this one line of code will ensure that we know how to print a string in output and how to execute code within cells in a notebook.\n"]},{"cell_type":"markdown","id":"f33c4435-2756-43d1-a013-75e753007fff","metadata":{},"outputs":[],"source":["<hr/>\n","<div class=\"alert alert-success alertsuccess\" style=\"margin-top: 20px\">\n","[Tip]: To execute the Python code in the code cell below, click on the cell to select it and press <kbd>Shift</kbd> + <kbd>Enter</kbd>.\n","</div>\n","<hr/>\n"]},{"cell_type":"code","id":"4419c5dc-7fa5-4fd0-b725-b6b31d58f7b7","metadata":{},"outputs":[],"source":["# Try your first Python output\n\nprint('Hello, Python!')"]},{"cell_type":"markdown","id":"08d5c683-d0c2-4d74-8a43-c0427ef7f50c","metadata":{},"outputs":[],"source":["After executing the cell above, you should see that Python prints <code>Hello, Python!</code>. Congratulations on running your first Python code!\n"]},{"cell_type":"markdown","id":"75c039bc-31b6-46ff-9f4f-59d40b41b9ec","metadata":{},"outputs":[],"source":["<hr/>\n","<div class=\"alert alert-success alertsuccess\" style=\"margin-top: 20px\">\n","    [Tip:] <code>print()</code> is a function. You passed the string <code>'Hello, Python!'</code> as an argument to instruct Python on what to print.\n","</div>\n","<hr/>\n"]},{"cell_type":"markdown","id":"9a4a0406-46c9-461d-9a82-9499352f1386","metadata":{},"outputs":[],"source":["### What version of Python are we using?\n"]},{"cell_type":"markdown","id":"bbb86c2d-1832-405b-ab50-b210d8c06430","metadata":{},"outputs":[],"source":["<p>\n","    There are two popular versions of the Python programming language in use today: Python 2 and Python 3. The Python community has decided to move on from Python 2 to Python 3, and many popular libraries have announced that they will no longer support Python 2.\n","</p>\n","<p>\n","    Since Python 3 is the future, in this course we will be using it exclusively. How do we know that our notebook is executed by a Python 3 runtime? We can look in the top-right hand corner of this notebook and see \"Python 3\".\n","</p>\n","<p>\n","    We can also ask Python directly and obtain a detailed answer. Try executing the following code:\n","</p>\n"]},{"cell_type":"code","id":"4b8d484a-699d-40c1-84a6-ce27185fc915","metadata":{},"outputs":[],"source":["# Check the Python Version\n\nimport sys\nprint(sys.version)"]},{"cell_type":"markdown","id":"20eb81c2-9127-47ad-ac87-5d5cf8a9c0e4","metadata":{},"outputs":[],"source":["<hr/>\n","<div class=\"alert alert-success alertsuccess\" style=\"margin-top: 20px\">\n","    [Tip:] <code>sys</code> is a built-in module that contains many system-specific parameters and functions, including the Python version in use. Before using it, we must explictly <code>import</code> it.\n","</div>\n","<hr/>\n"]},{"cell_type":"markdown","id":"0e4b1d4e-93c4-49c9-a642-e8114b5e7677","metadata":{},"outputs":[],"source":["### Writing comments in Python\n"]},{"cell_type":"markdown","id":"b9126598-7ef0-48b5-98cf-7744cea79006","metadata":{},"outputs":[],"source":["<p>\n","    In addition to writing code, note that it's always a good idea to add comments to your code. It will help others understand what you were trying to accomplish (the reason why you wrote a given snippet of code). Not only does this help <strong>other people</strong> understand your code, it can also serve as a reminder <strong>to you</strong> when you come back to it weeks or months later.</p>\n","\n","<p>\n","    To write comments in Python, use the number symbol <code>#</code> before writing your comment. When you run your code, Python will ignore everything past the <code>#</code> on a given line.\n","</p>\n"]},{"cell_type":"code","id":"5e05f9a4-1464-4e89-8f6f-dc037e8cf62e","metadata":{},"outputs":[],"source":["# Practice on writing comments\n\nprint('Hello, Python!') # This line prints a string\n"]},{"cell_type":"markdown","id":"0ec95448-8868-488c-ac7e-8414f2b7aa65","metadata":{},"outputs":[],"source":["<p>\n","    After executing the cell above, you should notice that <code>This line prints a string</code> did not appear in the output, because it was a comment (and thus ignored by Python).\n","</p>\n","<p>\n","    The second line was also not executed because <code>print('Hi')</code> was preceded by the number sign (<code>#</code>) as well! Since this isn't an explanatory comment from the programmer, but an actual line of code, we might say that the programmer <em>commented out</em> that second line of code.\n","</p>\n"]},{"cell_type":"markdown","id":"486d456e-a0b8-4087-9df4-d2301b1c559c","metadata":{},"outputs":[],"source":["<h3 id=\"errors\">Errors in Python</h3>\n"]},{"cell_type":"markdown","id":"5aca727a-3f7c-47af-b423-ef166f235558","metadata":{},"outputs":[],"source":["<p>Everyone makes mistakes. For many types of mistakes, Python will tell you that you have made a mistake by giving you an error message. It is important to read error messages carefully to really understand where you made a mistake and how you may go about correcting it.</p>\n","<p>For example, if you spell <code>print</code> as <code>frint</code>, Python will display an error message. Give it a try:</p>\n"]},{"cell_type":"code","id":"0024f837-2b8a-4817-8299-511af4828cc1","metadata":{},"outputs":[],"source":["# Print string as error message\n\nfrint(\"Hello, Python!\")"]},{"cell_type":"markdown","id":"55f435b2-3cd6-48ea-bb6e-1b06c0f1964a","metadata":{},"outputs":[],"source":["<p>The error message tells you: \n","<ol>\n","    <li>where the error occurred (more useful in large notebook cells or scripts), and</li> \n","    <li>what kind of error it was (NameError)</li> \n","</ol>\n","<p>Here, Python attempted to run the function <code>frint</code>, but could not determine what <code>frint</code> is since it's not a built-in function and it has not been previously defined by us either.</p>\n"]},{"cell_type":"markdown","id":"0fbb0c67-1752-4a0b-8459-f78fb390dece","metadata":{},"outputs":[],"source":["<p>\n","    You'll notice that if we make a different type of mistake, by forgetting to close the string, we'll obtain a different error (i.e., a <code>SyntaxError</code>). Try it below:\n","</p>\n"]},{"cell_type":"code","id":"fd79a176-f3e3-4584-bd91-fc99ceba96c6","metadata":{},"outputs":[],"source":["# Try to see built-in error message\n\nprint(\"Hello, Python!)"]},{"cell_type":"markdown","id":"aa840668-5e61-4880-a962-0cc6bc65e5c7","metadata":{},"outputs":[],"source":["### Does Python know about your error before it runs your code?\n"]},{"cell_type":"markdown","id":"4d4e8c04-8e51-4ba1-bcd1-d37de3f1fbb3","metadata":{},"outputs":[],"source":["Python is what is called an <em>interpreted language</em>. Compiled languages examine your entire program at compile time, and are able to warn you about a whole class of errors prior to execution. In contrast, Python interprets your script line by line as it executes it. Python will stop executing the entire program when it encounters an error (unless the error is expected and handled by the programmer, a more advanced subject that we'll cover later on in this course).\n"]},{"cell_type":"markdown","id":"e47a9f67-97ef-448c-86d7-dc9fc55aabfb","metadata":{},"outputs":[],"source":["Try to run the code in the cell below and see what happens:\n"]},{"cell_type":"code","id":"af0f03fa-8301-4d8f-b3ee-575a9f900f45","metadata":{},"outputs":[],"source":["# Print string and error to see the running order\n\nprint(\"This will be printed\")\nfrint(\"This will cause an error\")\nprint(\"This will NOT be printed\")"]},{"cell_type":"markdown","id":"c1f41ea4-e8aa-432d-973b-6f22ea429943","metadata":{},"outputs":[],"source":["### Exercise: Your First Program\n"]},{"cell_type":"markdown","id":"cb8e7b0e-4d38-497b-a2c0-a463eacd9523","metadata":{},"outputs":[],"source":["<p>Generations of programmers have started their coding careers by simply printing \"Hello, world!\". You will be following in their footsteps.</p>\n","<p>In the code cell below, use the <code>print()</code> function to print out the phrase: <code>Hello, world!</code></p>\n"]},{"cell_type":"code","id":"69f24712-46c9-49fa-8080-f19c959fca48","metadata":{},"outputs":[],"source":["# Write your code below. Don't forget to press Shift+Enter to execute the cell\n"]},{"cell_type":"markdown","id":"f18ab862-3991-41b6-8fd4-53c28ceb8591","metadata":{},"outputs":[],"source":["<details><summary>Click here for the solution</summary>\n","\n","```python\n","print(\"Hello, world!\")\n","\n","```\n","\n","</details>\n"]},{"cell_type":"markdown","id":"b8022e1f-937c-4230-973a-0e290caefbea","metadata":{},"outputs":[],"source":["<p>Now, let's enhance your code with a comment. In the code cell below, print out the phrase: <code>Hello, world!</code> and comment it with the phrase <code>Print the traditional hello world</code> all in one line of code.</p>\n"]},{"cell_type":"code","id":"6912919e-ea26-4388-a851-86473336cb85","metadata":{},"outputs":[],"source":["# Write your code below. Don't forget to press Shift+Enter to execute the cell\n"]},{"cell_type":"markdown","id":"ff4eb662-7d7d-4d28-9686-22efc40c8cc5","metadata":{},"outputs":[],"source":["<details><summary>Click here for the solution</summary>\n","\n","```python\n","print(\"Hello, world!\") # Print the traditional hello world\n","\n","```\n","\n","</details>\n"]},{"cell_type":"markdown","id":"3d645591-6f9c-4ab3-9293-1c3b2b6932c0","metadata":{},"outputs":[],"source":["<p>What is the value of <code>z</code> where <code>z = x + y</code>?</p>\n"]},{"cell_type":"code","id":"968904a8-de3f-4e48-b84b-7827ad4b05f2","metadata":{},"outputs":[],"source":["# Write your code below. Don't forget to press Shift+Enter to execute the cell\n"]},{"cell_type":"markdown","id":"9649df29-f0c7-4ee4-9072-c339223995a1","metadata":{},"outputs":[],"source":["<details><summary>Click here for the solution</summary>\n","\n","```python\n","17\n","\n","```\n","\n","</details>\n"]},{"cell_type":"markdown","id":"c79fcbb3-ce52-4de7-ac19-e50c379e64ac","metadata":{},"outputs":[],"source":["<hr>\n","\n","<p>Congratulations, you have completed your first lesson and hands-on lab in Python.\n","<hr>\n"]},{"cell_type":"markdown","id":"5ab4987b-eedb-4be6-905c-3a085467e12a","metadata":{},"outputs":[],"source":["## Author\n","\n","<a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a>\n","\n","\n","## Other contributors\n","\n","<a href=\"www.linkedin.com/in/jiahui-mavis-zhou-a4537814a\">Mavis Zhou</a>\n","\n","\n","\n","## <h3 align=\"center\"> © IBM Corporation 2023. All rights reserved. <h3/>\n"]},{"cell_type":"markdown","id":"d5c69c56-82ef-455a-9b6a-843c445595f5","metadata":{},"outputs":[],"source":["```toggle## Change Log\n","```\n","\n","\n","```toggle|  Date (YYYY-MM-DD) |  Version | Changed By  |  Change Description |\n","```\n","```toggle|---|---|---|---|\n","```\n","```toggle| 2023-10-30 | 2.2 | Abhishek Gagneja| Updated instructions |\n","```\n","```toggle| 2022-01-10  | 2.1  | Malika  | Removed the readme for GitShare|\n","```\n","```toggle| 2020-08-26  | 2.0  | Lavanya | Moved lab to course repo in GitLab |\n","```\n"]}],"metadata":{"kernelspec":{"display_name":"Python 3 (ipykernel)","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.11.7"},"prev_pub_hash":"205af83a85a70edf4e318f775fabe17843ea41df74f979adab2ff8cf7104f876"},"nbformat":4,"nbformat_minor":4}