{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Variables and basic calculations - Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1.1.\n", "\n", "Write code that can print the sentence “It works!” or any other sentence that you can think of." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"It works!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1.2.\n", "\n", "\n", "Write a program which produces the following output: \n", "\n", "```\n", "This is the first line.\n", "This is the second line.\n", "This line contains a tab. \n", "```\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"This is the first line.\\nThis is the second line.\")\n", "print(\"This line contains a \\ttab\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1.3.\n", "\n", "\n", "Declare three numerical variables with arbitrary values. What is the average of these three values? " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = 7\n", "b = 6\n", "c = 9\n", "\n", "average = ( a + b + c ) / 3\n", "print(average)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1.4.\n", "\n", "\n", "Given an exchange rate of 1.2409 (euros to American dollars), how many dollars can you buy for 150 euros? Use the in-built round() function to ensure that the floating point number that results from the calculation is shown as an actual amount, indicating the number of cents. The first parameter of 'round()' is the number to be rounded, and the second number is the number of digits beyond the decimal point. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "exchangeRate = 1.2409\n", "\n", "euros = 150\n", "dollars = exchangeRate * euros\n", "\n", "print( round( dollars, 2 ) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercise 1.5.\n", "\n", "\n", "Write code which can calculate the number of seconds in seven days.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "secondsInADay = 60 * 60 * 24\n", "\n", "print( 'Seven days last ' + str( 7 * secondsInADay ) + ' seconds ' ) " ] } ], "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.7.2" } }, "nbformat": 4, "nbformat_minor": 2 }