{ "nbformat_minor": 2, "nbformat": 4, "cells": [ { "source": [ "$$\n", "\\def\\CC{\\bf C}\n", "\\def\\QQ{\\bf Q}\n", "\\def\\RR{\\bf R}\n", "\\def\\ZZ{\\bf Z}\n", "\\def\\NN{\\bf N}\n", "$$\n", "# Loop `for`\n", "\n", "## Exercise 1\n", "\n", "In an american novel, eigth small ducks are respectively called: Jack, Kack, Lack, Mack, Nack, Oack, Pack et Qack. Write a small script that generates all these names from the two following strings:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "prefixes = 'JKLMNOP'\n", "suffix = 'ack'" ], "outputs": [], "metadata": {} }, { "source": [ "If you use instruction `for ... in ...`, the code should contain only two lines:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "## Exercise 2\n", "\n", "Write a for loop that shows this:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# something" ], "outputs": [ { "execution_count": 1, "output_type": "execute_result", "data": { "text/plain": [ "+++++\n", "++++++++\n", "+++++++++++\n", "++++++++++++++\n", "+++++++++++++++++\n", "++++++++++++++++++++\n", "+++++++++++++++++++++++" ] }, "metadata": {} } ], "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "## Exercise 3\n", "\n", "Let the following lists:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "t1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n", "t2 = ['January', 'February', 'March', 'April', 'May', 'June',\n", "'July', 'August', 'September', 'October', 'November', 'December']" ], "outputs": [], "metadata": {} }, { "source": [ "Using `t1` and `t2` create a new list `t3` containing all elements of the two lists alternating them in such a way that each Month is followed by the corresponding number of days, that is, `['Janvier',31,'F\u00e9vrier',28,'Mars',31, etc...]` :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "## Exercise 4\n", "\n", "An integral can be computed as a limit of a Riemann sum:\n", "\n", "$$\\int_0^1 x^2\\,dx =\n", "\\lim_{n\\to\\infty}\\frac{1}{n}\\sum_{i=0}^n{\\left(\\frac{i}{n}\\right)^2}$$\n", "\n", "Write a for loop which computes the above Riemann sum for values of $n=10, 100,\n", "1000, 10000$ :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "How many digits after the decimal are correct?:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "How is this number of digits evolves when $n$ is multiplied by 10?:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "## Exercise 5\n", "\n", "Write a for loop which prints the factorization for each integer from 1 to 100:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "## Exercise 6\n", "\n", "Write a for loop which prints the derivates of the functions $f_n(x)=x^n$ for all values of $n$ from 1 to 20:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "## Exercise 7\n", "\n", "Write a for loop which prints the primitive of the functions $\\sin(x)$, $\\cos(x)$, $\\tan(x)$, $\\log(x)$, $\\exp(x)$, $\\sinh(x)$ and $\\cosh(x)$ :" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "## Exercise 8\n", "\n", "Define a square matrix $A$ of your choice. Write a for loop which prints the first 10 powers of the matrix $A$. How are the coefficients behaving?:" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# edit here" ], "outputs": [], "metadata": {} }, { "source": [ "## Reference\n", "\n", "Some of the exercises here are from the book of (G\u00e9rard Swinnen, [Apprendre \u00e0 programmer avec Python 3](http://inforef.be/swi/download/apprendre_python3_5.pdf), 2012) that you are invited to consult to learn more." ], "cell_type": "markdown", "metadata": {} } ], "metadata": { "kernelspec": { "display_name": "sagemath", "name": "sagemath" } } }