{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Markdown\n", "\n", "What Wikipedia says:\n", "> Markdown is a lightweight markup language with plain-text-formatting syntax. Its design allows it to be converted to many output formats, but the original tool by the same name only supports HTML. Markdown is often used to format readme files, for writing messages in online discussion forums, and to create rich text using a plain text editor.\n", "\n", "> Since the initial description of Markdown contained ambiguities and unanswered questions, the implementations that appeared over the years have subtle differences and many come with syntax extensions\n", "\n", "Please use a markdown cheats sheet to see how easy it is how to combine Maths and code. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 0: become familiar with markdown\n", "\n", "Write the markdown code for the mathematical expressions:\n", " - integral from a to b of function f(x) \n", " - the sum for k from 1 to infinity of 1/k^2\n", " - a short mathematical theorem of your choice, for example ***Fermat's little theorem*** or whatever you like" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**The integral** \n", "\n", " ... todo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**The sum** \n", "\n", "... todo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**The theorem** \n", "\n", "... todo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Variables " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Write a Python program to get the Python version you are using" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python version\n", "3.7.6 (default, Jan 8 2020, 19:59:22) \n", "[GCC 7.3.0]\n", "Version info.\n", "sys.version_info(major=3, minor=7, micro=6, releaselevel='final', serial=0)\n" ] } ], "source": [ "import sys\n", "print(\"Python version\")\n", "print (sys.version)\n", "print(\"Version info.\")\n", "print (sys.version_info)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As a first example see a Python program which accepts the radius of a circle from the user and compute the area." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import math\n", "def area_circle(r):\n", " print(\"The area of a circle with radius\"+str(r)+\"is \"+str(math.pi*r**2))\n", " return math.pi*r**2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The area of a circle with radius1is 3.141592653589793\n" ] } ], "source": [ "area=area_circle(1)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.141592653589793\n" ] } ], "source": [ "print(area)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 1: a simple function \n", "Now write a similar function which computes and prints for a cylinder, defined by radius r and height h \n", "the following values:\n", " * volume V\n", " * surface area A" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "# TODO : define the cylinder function\n", "def cylcomp(r,h):\n", " # go on" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# TODO : use the cylinder function\n", "cylcomp(3,4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 2: importing, understanding and using a module\n", "Write a Python program to print the calendar of a given month and year. \n", "Note : Use `calendar` module. \n", "To find out, how the calendar module can be used, use the build-in `help()` method:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "import calendar\n", "help(calendar)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# TODO : print the calendar for April 2020\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Task 3: work with inputs \n", "To get user inputs you can use the `ìnput()` function, for example:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "gaga\n", "You typed: gaga\n" ] } ], "source": [ "x=input()\n", "print(\"You typed: %s\" % (x))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now write a function, which does: \n", " - gets an input \n", " - count and print the number of characters of the input line\n", " - print the reverse input i.e. for input **Hello World** print **dlroW olleH**" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def char_cnt():\n", " # TODO\n", " " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "char_cnt()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 4: return values of functions\n", "\n", "Up to now we have not used any return values of functions. In this example we consider a simple function which computes and return the sum of two numbers:" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "def mysum(a,b):\n", " c=a+b\n", " return c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can use this function in own computations, for example" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 3 + 4 = 7.000000 \n" ] } ], "source": [ "s1=mysum(3,4)\n", "print(\" 3 + 4 = %f \" % (s1) )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now write a function which does the following\n", " - for given string argument **s** and integer **n** return a string, consisting of every n-th. character of s \n", " \n", "So for input **n=2** and **s=The quick brown fox jumps over the lazy dog** returns **Teqikbonfxjmsoe h aydg**" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "None\n" ] } ], "source": [ "def nstring(s,n):\n", " # TODO\n", " \n", "sx=nstring(\"The quick brown fox jumps over the lazy dog\",2)\n", "print(sx)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Think about the problems: \n", " - What will happen in your implementation, if **n** is larger then the total length of the string ?\n", " - What can you do to ensure, that your code returns what you want ? " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 5: your own example\n", "\n", "In the last task for today write a small program for a problem of your choice. Describe your problem in a short markdown cell, then implement it and make a test run. \n", "\n", "To get some more than only `print(\"Hello World\")` ;-) the conditions are:\n", " - at least 4 input values (can be numbers, strings or whatever)\n", " - at least 3 self defined funtions \n", " - one of this functions calls one other function\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem description ###\n", "I want to compute ..." ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "# Implementation part" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "# Test part" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.9" } }, "nbformat": 4, "nbformat_minor": 2 }