{"cells":[{"cell_type":"markdown","id":"b00d9937-2637-49c4-a8a1-4a57839cdc29","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","# Working with Variables and Expressions in Python\n","\n","\n","Estimated time needed: **10** minutes\n","    \n","\n","## Objectives\n","\n","After completing this lab you will be able to:\n","\n","* Use expressions and variables to perform operations\n"]},{"cell_type":"markdown","id":"569f9453-2fc4-46ac-85d0-5400e663ad89","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=\"#Expressions-and-Variables\">Expressions and Variables</a>\n","            <ul>\n","                <li><a href=\"#Expressions\">Expressions</a></li>\n","                <li><a href=\"#Exercise:-Expressions\">Exercise: Expressions</a></li>\n","                <li><a href=\"#Variables\">Variables</a></li>\n","                <li><a href=\"#Exercise:-Expression-and-Variables-in-Python\">Exercise: Expression and Variables in Python</a></li>\n","            </ul>\n","        </li>\n","    </ul>\n","</div>\n","\n","<hr>\n"]},{"cell_type":"markdown","id":"dbff522c-7814-49c0-8361-cc2d74c4f889","metadata":{},"outputs":[],"source":["## Expressions and Variables\n"]},{"cell_type":"markdown","id":"4544e323-7b0b-454c-9073-545ff35119cd","metadata":{},"outputs":[],"source":["### Expressions\n"]},{"cell_type":"markdown","id":"124df50c-5cba-48a9-b8e0-24dce4bcfce8","metadata":{},"outputs":[],"source":["<p>Expressions in Python can include operations among compatible types (e.g., integers and floats). For example, basic arithmetic operations like adding multiple numbers:</p>\n"]},{"cell_type":"code","id":"03502095-0b15-449b-8271-fbd170050ab4","metadata":{},"outputs":[],"source":["# Addition operation expression\n\n43 + 60 + 16 + 41"]},{"cell_type":"markdown","id":"df1741d8-2b88-4740-8dd7-e1587185af1a","metadata":{},"outputs":[],"source":["<p>We can perform subtraction operations using the minus operator. In this case the result is a negative number:</p>\n"]},{"cell_type":"code","id":"b9922df5-1f1a-42f3-84f3-097bf217a1c3","metadata":{},"outputs":[],"source":["# Subtraction operation expression\n\n50 - 60"]},{"cell_type":"markdown","id":"c3e0ddfd-3a57-4ba1-8438-fd26d0dc79b8","metadata":{},"outputs":[],"source":["<p>We can do multiplication using an asterisk:</p>\n"]},{"cell_type":"code","id":"ffc31140-d992-4478-b4eb-32b162c5a0ae","metadata":{},"outputs":[],"source":["# Multiplication operation expression\n\n5 * 5"]},{"cell_type":"markdown","id":"83d870a6-0bcd-456d-9f6d-139669446034","metadata":{},"outputs":[],"source":["<p>We can also perform division with the forward slash:\n"]},{"cell_type":"code","id":"846a7a04-ee33-49a1-aafe-d986f91c06eb","metadata":{},"outputs":[],"source":["# Division operation expression\n\n25 / 5"]},{"cell_type":"code","id":"3b5a60c2-62d4-4a9e-8a55-dbdc65794bc7","metadata":{},"outputs":[],"source":["# Division operation expression\n\n25 / 6"]},{"cell_type":"markdown","id":"c5d0c2b6-1a91-4c94-8a59-c4c1a82a8d61","metadata":{},"outputs":[],"source":["<p>As seen in the quiz above, we can use the double slash for integer division, where the result is rounded down to the nearest integer:\n"]},{"cell_type":"code","id":"e1d1cd89-70da-493d-a3e4-630145a44765","metadata":{},"outputs":[],"source":["# Integer division operation expression\n\n25 // 5"]},{"cell_type":"code","id":"21a46cea-2cd9-473d-8684-bddc22ad43df","metadata":{},"outputs":[],"source":["# Integer division operation expression\n\n25 // 6"]},{"cell_type":"markdown","id":"0936344b-0367-4cb9-806e-77f32175ce4a","metadata":{},"outputs":[],"source":["<p>Let's write an expression that calculates how many hours there are in 160 minutes:\n"]},{"cell_type":"code","id":"4e06df49-d18b-4b8d-8173-3982847c9965","metadata":{},"outputs":[],"source":["# Write your code below. Don't forget to press Shift+Enter to execute the cell\n160//60"]},{"cell_type":"markdown","id":"abd3424a-ef03-4c17-b9a9-df2645e4c22d","metadata":{},"outputs":[],"source":["<p>Python follows well accepted mathematical conventions when evaluating mathematical expressions. In the following example, Python adds 30 to the result of the multiplication (i.e., 120).\n"]},{"cell_type":"code","id":"a9cc2eeb-0d1c-4b65-90e0-6c4cdc8424f3","metadata":{},"outputs":[],"source":["# Mathematical expression\n\n30 + 2 * 60"]},{"cell_type":"markdown","id":"4120ef1c-5038-40ec-9ee3-1c70fbd34383","metadata":{},"outputs":[],"source":["<p>And just like mathematics, expressions enclosed in parentheses have priority. So the following multiplies 32 by 60.\n"]},{"cell_type":"code","id":"7b587d17-0b46-466f-b55a-35691db3ce14","metadata":{},"outputs":[],"source":["# Mathematical expression\n\n(30 + 2) * 60"]},{"cell_type":"markdown","id":"7a11f503-d799-4a15-9d85-11207aaf7ea2","metadata":{},"outputs":[],"source":["### Variables\n"]},{"cell_type":"markdown","id":"a8a8b8d8-39c0-49d7-9a70-55be1b41c2be","metadata":{},"outputs":[],"source":["<p>Just like with most programming languages, we can store values in <i>variables</i>, so we can use them later on. For example:</p>\n"]},{"cell_type":"code","id":"e7f1738e-424f-41ee-b19f-8781d2e99e1f","metadata":{},"outputs":[],"source":["# Store value into variable\n\nx = 43 + 60 + 16 + 41"]},{"cell_type":"markdown","id":"40613b4c-4800-4029-9d96-d415102935cf","metadata":{},"outputs":[],"source":["<p>To see the value of <code>x</code> in a Notebook, we can simply place it on the last line of a cell:</p>\n"]},{"cell_type":"code","id":"51f4c73e-4abc-43e6-9a60-14e7479f54f5","metadata":{},"outputs":[],"source":["# Print out the value in variable\n\nx"]},{"cell_type":"markdown","id":"9add8824-634c-4a15-9ed3-a8fab42cb7ae","metadata":{},"outputs":[],"source":["<p>We can also perform operations on <code>x</code> and save the result to a new variable:</p>\n"]},{"cell_type":"code","id":"2a3bd188-80b6-4299-9e8e-66094372f801","metadata":{},"outputs":[],"source":["# Use another variable to store the result of the operation between variable and value\n\ny = x / 60\ny"]},{"cell_type":"markdown","id":"be370900-9486-4e40-84f5-cda77b1881dd","metadata":{},"outputs":[],"source":["<p>If we save a value to an existing variable, the new value will overwrite the previous value:</p>\n"]},{"cell_type":"code","id":"ce77c933-0b2b-4e8a-bfac-319cefd798c8","metadata":{},"outputs":[],"source":["# Overwrite variable with new value\n\nx = x / 60\nx"]},{"cell_type":"markdown","id":"a46c0466-86ca-42d8-8cb2-40ccd3321927","metadata":{},"outputs":[],"source":["<p>It's a good practice to use meaningful variable names, so you and others can read the code and understand it more easily:</p>\n"]},{"cell_type":"code","id":"f078bb77-8a75-4af6-9325-0fc8f3844f46","metadata":{},"outputs":[],"source":["# Name the variables meaningfully\n\ntotal_min = 43 + 42 + 57 # Total length of albums in minutes\ntotal_min"]},{"cell_type":"code","id":"9e0e851a-4054-4d48-b3eb-4ac5b6c37d2a","metadata":{},"outputs":[],"source":["# Name the variables meaningfully\n\ntotal_hours = total_min / 60 # Total length of albums in hours \ntotal_hours"]},{"cell_type":"markdown","id":"ab4eac27-f787-4c7c-869d-7785fce9f38d","metadata":{},"outputs":[],"source":["<p>In the cells above we added the length of three albums in minutes and stored it in <code>total_min</code>. We then divided it by 60 to calculate total length <code>total_hours</code> in hours. You can also do it all at once in a single expression, as long as you use parenthesis to add the albums length before you divide, as shown below.</p>\n"]},{"cell_type":"code","id":"5960d098-02e7-4368-9f3c-3eb80dfcf8fc","metadata":{},"outputs":[],"source":["# Complicate expression\n\ntotal_hours = (43 + 42 + 57) / 60  # Total hours in a single expression\ntotal_hours"]},{"cell_type":"markdown","id":"22e70d15-112a-43db-a9ef-8efd755c5f9b","metadata":{},"outputs":[],"source":["<p>If you'd rather have total hours as an integer, you can of course replace the floating point division with integer division (i.e., <code>//</code>).</p>\n"]},{"cell_type":"markdown","id":"6f96c463-b783-45d6-9c73-570426adfaff","metadata":{},"outputs":[],"source":["### Exercise: Expressions in Python\n"]},{"cell_type":"markdown","id":"a778824d-5a52-47bb-986b-63fa96e3a2ed","metadata":{},"outputs":[],"source":["Write an expression to add 30 and 20 and subtract 40\n"]},{"cell_type":"code","id":"02df8ff3-2ed9-4399-a58c-878ba70a0473","metadata":{},"outputs":[],"source":["# Write your code below. Don't forget to press Shift+Enter to execute the cell\n"]},{"cell_type":"markdown","id":"e74007a8-c767-4884-a4a8-43bf9f06159e","metadata":{},"outputs":[],"source":["<details><summary>Click here for the solution</summary>\n","\n","```python\n","30+20-40\n","# This will print 10\n","\n","```\n","\n","</details>\n"]},{"cell_type":"markdown","id":"ba3dba6e-69ec-4ab5-b412-4e1164b9acaa","metadata":{},"outputs":[],"source":["Write an expression to subtract 5 from 55 and divide the result by 10\n"]},{"cell_type":"code","id":"02c01947-e3a8-4f90-9bb5-57562b3d8a42","metadata":{},"outputs":[],"source":["# Write your code below. Don't forget to press Shift+Enter to execute the cell\n"]},{"cell_type":"markdown","id":"9ae9afb8-d1f7-4784-ae6a-15b6a9e913ac","metadata":{},"outputs":[],"source":["<details><summary>Click here for the solution</summary>\n","\n","```python\n","(55-5)/10\n","# This will print 5.0\n","\n","```\n","\n","</details>\n"]},{"cell_type":"markdown","id":"d43eb47d-0744-4018-9c32-e3a3662d4d5f","metadata":{},"outputs":[],"source":["Write an expression to multiply 6 with 10 and divide the result by 12\n"]},{"cell_type":"code","id":"c182c909-13e5-42f1-a2de-0c1c0e0b8e60","metadata":{},"outputs":[],"source":["# Write your code below. Don't forget to press Shift+Enter to execute the cell\n"]},{"cell_type":"markdown","id":"952ffcca-6e78-4476-938a-d72eb4b2803c","metadata":{},"outputs":[],"source":["<details><summary>Click here for the solution</summary>\n","\n","```python\n","(6*10)/12\n","# This will print 5.0\n","\n","```\n","\n","</details>\n"]},{"cell_type":"markdown","id":"7aa8d9d8-6020-44a2-b410-2a65fd66a0c7","metadata":{},"outputs":[],"source":["### Exercise:Variables in Python\n"]},{"cell_type":"markdown","id":"e1e907e5-94c6-4261-bb06-375849676997","metadata":{},"outputs":[],"source":["<p>What is the value of <code>x</code> where <code>x = 3 + 2 * 2</code></p>\n"]},{"cell_type":"code","id":"4b11938f-32e2-433a-825e-881c317301a8","metadata":{},"outputs":[],"source":["# Write your code below. Don't forget to press Shift+Enter to execute the cell\n"]},{"cell_type":"markdown","id":"979b30d0-75e0-4dc3-b3c8-31d87d007d88","metadata":{},"outputs":[],"source":["<details><summary>Click here for the solution</summary>\n","\n","```python\n","\n","x = 3 + 2 * 2\n","x\n","# This will print 7\n","\n","```\n","\n","</details>\n"]},{"cell_type":"markdown","id":"0a02034b-b513-4e97-81b7-2de21cd4721c","metadata":{},"outputs":[],"source":["<p>What is the value of <code>y</code> where <code>y = (3 + 2) * 2</code>?</p>\n"]},{"cell_type":"code","id":"a4c3c3b9-e6f1-4984-9ede-9d8a6d07fa82","metadata":{},"outputs":[],"source":["# Write your code below. Don't forget to press Shift+Enter to execute the cell\n"]},{"cell_type":"markdown","id":"4bcf807c-b867-4a77-be73-66a3b81e06da","metadata":{},"outputs":[],"source":["<details><summary>Click here for the solution</summary>\n","\n","```python\n","\n","y = (3 + 2) * 2\n","y\n","# This will print 10\n","\n","```\n","\n","</details>\n"]},{"cell_type":"markdown","id":"36a4d4ca-8f12-4e86-91da-7234c2e9281e","metadata":{},"outputs":[],"source":["<p>What is the value of <code>z</code> where <code>z = x + y</code>?</p>\n"]},{"cell_type":"code","id":"5f963ce3-bd63-404b-8a0d-0c7216710819","metadata":{},"outputs":[],"source":["# Write your code below. Don't forget to press Shift+Enter to execute the cell\n"]},{"cell_type":"markdown","id":"f2dfcf5c-41ca-4640-80f9-4b722bc7078c","metadata":{},"outputs":[],"source":["<details><summary>Click here for the solution</summary>\n","\n","```python\n","z = x + y\n","z\n","# This will print 17\n","\n","```\n","\n","</details>\n"]},{"cell_type":"markdown","id":"48d47446-419b-4d3e-9358-7a041757cdbd","metadata":{},"outputs":[],"source":["<hr>\n","<p>Congratulations, you have completed your hands-on lab on Expressions and Variables in Python.\n","<hr>\n"]},{"cell_type":"markdown","id":"1698e347-5a72-411d-bfe9-d6fde1f6b046","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":"8d806b99-3f97-4ef2-9f50-d4c62fecee85","metadata":{},"outputs":[],"source":["\n","```{toggle} ## Change Log\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":"cb6de4a8ea0f2d49680e69257ccc5d4e7699b2001c8f948194af176e2c19d23b"},"nbformat":4,"nbformat_minor":4}