{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Expressions, Data Types, and Variables\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## Math Expressions for Numeric Types\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "We will start first by dealing with numbers and see how we can do various math and logical operations with Python. Here are a few operators that we will be using:\n", "\n", "* `+` addition, add two numbers\n", "* `-` subtraction and negation\n", "* `*` multiplication, multiply two numbers\n", "* `/` divide first with the outcome being decimal outcome\n", "* `//` division with the outcome being integer (return only the integer part of the outcome)\n", "* `%` modulo/division remainder, what is the remainder when the first number is divided by the second?\n", "* `**` power, raise the first number into a power given by the second number" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "print(\"addition:\")\n", "print(1 + 1)\n", "print(1.5 + 1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "print(\"subtraction:\")\n", "print(1 - 1)\n", "print(1 - 1.0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "print(\"negation:\")\n", "print(-5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "print(\"multiplication:\")\n", "print(3 * 3) # integer\n", "print(3 * 3.0) # floating point" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "print(\"division:\")\n", "print(4 / 2)\n", "print(5 / 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "print(\"integer division:\")\n", "print(4 // 2)\n", "print(5 // 2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "print(\"modulo:\") # modulo is a fancy term for remainder of a division\n", "print(4 % 2)\n", "print(5 % 2)\n", "print(5.5 % 2)\n", "print(75 % 4)\n", "print(10 % 3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "print(\"power:\")\n", "print(10 ** 3)\n", "print(2 ** 5)" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## Exercise 1" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "Assume that you go to a restaurant, and you order $50 worth of food. Then you need to add the NY Sales Tax (8.875%) and add a tip (say, 20%). Write down the calculation that will print the total cost of the food." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# tip calculated on the pre-tax amount" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# tip calculated on the after-tax amount" ] }, { "cell_type": "markdown", "metadata": { "hidden": true, "solution2": "hidden", "solution2_first": true }, "source": [ "### Solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "print(\"The total cost of the meal, with tip on the pre-tax amount is:\")\n", "print(50 + 50 * 8.875 / 100 + 50 * 20 / 100)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "print(\"The total cost of the meal, with tip on the post-tax amount is:\")\n", "print(50 + 50 * 8.875 / 100 + (50 + 50 * 8.875 / 100) * 20 / 100)" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## Exercise 2" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "\n", "You have a stock that closed at `$550` on Monday, and then closed at `$560` on Tuesday. Calculate its daily return: the daily return is defined as the difference in the closing prices, divided by the closing price the day before." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# your code here" ] }, { "cell_type": "markdown", "metadata": { "hidden": true, "solution2": "hidden", "solution2_first": true }, "source": [ "### Solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "print(\"The daily return is:\")\n", "print((560 - 550) / 550)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "# If we want to show percentages,\n", "# We can modify our code and\n", "# add a multiplication with 100 in front:\n", "print(\"The daily return is:\")\n", "print(100 * (560 - 550) / 550)" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## Exercise 3\n" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "\n", "Assume that someone's height is 5 ft and 9 inches. Conver that to centimeters. Remember that one foot is 30.48 centimeters, and one inch is 2.54 centimeters." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# your code here" ] }, { "cell_type": "markdown", "metadata": { "hidden": true, "solution2": "hidden", "solution2_first": true }, "source": [ "### Solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "print(\"The height in centimeters is:\")\n", "print(5 * 30.48 + 9 * 2.54)" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## Exercise 4\n" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "\n", "Write a Python program to compute the future value of a deposit with a a principal amount of \\$10,000, rate of interest 3%, and after 5 years. Remember that the value is: $ Principal * (1+interest)^{years}$ and you will need to use the power operator (**) for this calculation." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# your code here" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true, "hidden": true, "solution2": "hidden", "solution2_first": true }, "source": [ "### Solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "print(\"The value of the deposit will be:\")\n", "print(10000 * (1 + 3 / 100) ** 5)" ] }, { "cell_type": "markdown", "metadata": { "heading_collapsed": true }, "source": [ "## Exercise 5" ] }, { "cell_type": "markdown", "metadata": { "hidden": true }, "source": [ "Assume that someone's height is 180 centimeters. Convert that height into feet and inches. Remember that one foot is 30.48 centimeters, and one inch is 2.54 centimeters. You will need to use the modulo operator (%) for this conversion. Also use the `int(...)` function to get the integer part of a division. Optionally, you can also use the `round(...)` function to get the rounded integer number from a decimal." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true }, "outputs": [], "source": [ "# your code here" ] }, { "cell_type": "markdown", "metadata": { "hidden": true, "solution2": "hidden", "solution2_first": true }, "source": [ "### Solution" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "print(\"180 cm is\")\n", "print(180 // 30.48)\n", "print(\"feet and \")\n", "print(180 % 30.48 / 2.54)\n", "print(\"inches\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "# let’s see how we can print the message in one line\n", "print(\"180 cm is\", 180 // 30.48, \"feet and \", 180 % 30.48 / 2.54, \"inches\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "# Let's use int() function to make the output look a bit nicer.\n", "print(\"180 cm is\", int(180 / 30.48), \"feet and\", int(180 % 30.48 / 2.54), \"inches\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "hidden": true, "solution2": "hidden" }, "outputs": [], "source": [ "# Let's use the functions int() and round()\n", "print(\"180 cm is\", int(180 / 30.48), \"feet and\", round(180 % 30.48 / 2.54, 1), \"inches\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }