{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Excercise I - Python Essentials\n", "\n", "The purpose of this exercise is to get familiar with pure Python, meaning that you don't need any third-party libraries such as NumPy for now." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Mathematical operations\n", "\n", "Calculate the following mathematical expressions for a predefined value of x:\n", "\n", "a) $x^2$\n", "\n", "b) $\\sqrt{(x^2 + y^2)}$\n", "\n", "c) $\\frac{1}{x^y}$\n", "\n", "d) $\\sum_{x=1}^5 x^3 - 2x$ (Hint: Use a [for loop](https://docs.python.org/3/tutorial/controlflow.html#for-statements) and sum to a variable)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "for x in 1, 2, 3, 4, 5:\n", " a = x**3 - 2*x" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Consider a list of numerical values called *numbers*:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "numbers = [1, 4., 2.5, 3, 10.]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "a) What is the difference between the first and the last entry?" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "b) Calculate the sum of numbers and call it `summe` (Hint: You can use a [for loop](https://docs.python.org/3/tutorial/controlflow.html#for-statements) again)." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "ename": "NameError", "evalue": "name 'summe' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-5-75e858c309b4>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Check if your answer is correct (*DO NOT CHANGE THIS CELL*)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0msumme\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m20.5\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Well done.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Try again.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNameError\u001b[0m: name 'summe' is not defined" ] } ], "source": [ "# Check if your answer is correct (*DO NOT CHANGE THIS CELL*)\n", "if summe == 20.5:\n", " print(\"Well done.\")\n", "else:\n", " print(\"Try again.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "c) Calculate the mean of numbers and call it `mean` (Hint: with [`len()`](https://docs.python.org/3/library/functions.html#len) you can get the number of items in a list)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Check if your answer is correct (*DO NOT CHANGE THIS CELL*)\n", "if mean == 4.1:\n", " print(\"Well done.\")\n", "else:\n", " print(\"Try again.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "d) Do all entries in `numbers` have the same type? You can check the type of a Python object with `type(object)` and convert integers to floats with `float(1)` for example." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "e) Create a new list `numbers2` with floating points only using a [list comprehension](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Dictionaries" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "a) Write a Python script to generate a dictionary where the keys are numbers between 1 and 15 (both included) and the values are square of keys. (Hint: [Dictionary documentation](https://docs.python.org/3/tutorial/datastructures.html#dictionaries))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "b) Iterate over the dictionary and print key, value pairs." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "a) Define a function called `std` which calculates and returns the standard deviation of a list of numbers. (Hint: Maybe you can reuse some of the code you wrote in exercise 2?)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Check if your answer is correct (*DO NOT CHANGE THIS CELL*)\n", "if std([1,4,1,0]) == 1.5:\n", " print(\"Well done.\")\n", "else:\n", " print(\"Try again.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "b) Define the piecewise function `N` expressed as:\n", "\n", "$$\n", "N(x) = \\left\\lbrace\\begin{array}{ll}\n", "0, & x < 0\\\\\n", "x, & 0\\leq x < 1\\\\\n", "2-x, & 1\\leq x < 2\\\\\n", "0, & x \\geq 2\n", "\\end{array}\\right.\n", "$$\n", "\n", "Hint: Use an [if statement](https://docs.python.org/3/tutorial/controlflow.html#if-statements)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "c) Write a Python function called `swap` to reverse a string. (Hint: `[::-1]`)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your answer here." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Check if your answer is correct (*DO NOT CHANGE THIS CELL*)\n", "if swap(\"1234abcd\") == \"dcba4321\":\n", " print(\"Well done.\")\n", "else:\n", " print(\"Try again.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "**BONUS questions**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1) Write a function to count the upper and lower case letters in a given string.\n", "\n", "```\n", "Sample String : 'The quick Brown Fox'\n", "Expected Output : \n", "No. of Upper case characters : 3\n", "No. of Lower case Characters : 12\n", "```\n", "\n", "Hint: Maybe some of these [string methods](https://docs.python.org/3/library/stdtypes.html#string-methods) are useful." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2) Write a function called `unique`, to find the unique values in a list." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your answer here" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Check if your answer is correct (*DO NOT CHANGE THIS CELL*)\n", "if unique([1,2,3,3,3,3,4,5]) == [1, 2, 3, 4, 5]:\n", " print(\"Well done.\")\n", "else:\n", " print(\"Try again.\")" ] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 4 }