{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "\n", "\n", "\n", "| - | - | - |\n", "|-------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|\n", "| [Exercise 1 (hello world)](<#Exercise-1-(hello-world)>) | [Exercise 2 (compliment)](<#Exercise-2-(compliment)>) | [Exercise 3 (multiplication)](<#Exercise-3-(multiplication)>) |\n", "| [Exercise 4 (multiplication table)](<#Exercise-4-(multiplication-table)>) | [Exercise 5 (two dice)](<#Exercise-5-(two-dice)>) | [Exercise 6 (triple square)](<#Exercise-6-(triple-square)>) |\n", "| [Exercise 7 (areas of shapes)](<#Exercise-7-(areas-of-shapes)>) | [Exercise 8 (solve quadratic)](<#Exercise-8-(solve-quadratic)>) | [Exercise 9 (merge)](<#Exercise-9-(merge)>) |\n", "| [Exercise 10 (detect ranges)](<#Exercise-10-(detect-ranges)>) | [Exercise 11 (interleave)](<#Exercise-11-(interleave)>) | [Exercise 12 (distinct characters)](<#Exercise-12-(distinct-characters)>) |\n", "| [Exercise 13 (reverse dictionary)](<#Exercise-13-(reverse-dictionary)>) | [Exercise 14 (find matching)](<#Exercise-14-(find-matching)>) | [Exercise 15 (two dice comprehension)](<#Exercise-15-(two-dice-comprehension)>) |\n", "| [Exercise 16 (transform)](<#Exercise-16-(transform)>) | [Exercise 17 (positive list)](<#Exercise-17-(positive-list)>) | [Exercise 18 (acronyms)](<#Exercise-18-(acronyms)>) |\n", "| [Exercise 19 (sum equation)](<#Exercise-19-(sum-equation)>) | [Exercise 20 (usemodule)](<#Exercise-20-(usemodule)>) | |\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic concepts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Basic input and output" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The traditional \"Hello, world\" program is very simple in Python. You can run the program by selecting the cell by mouse and pressing control-enter on keyboard. Try editing the string in the quotes and rerunning the program." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello world2!\n" ] } ], "source": [ "print(\"Hello world2!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Multiple strings can be printed. By default, they are concatenated with a space:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, John! How are you?\n" ] } ], "source": [ "print(\"Hello,\", \"John!\", \"How are you?\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the print function, numerical expression are first evaluated and then automatically converted to strings. Subsequently the strings are concatenated with spaces:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 plus 2 equals 3\n" ] } ], "source": [ "print(1, \"plus\", 2, \"equals\", 1+2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reading textual input from the user can be achieved with the input function. The input function is given a string parameter, which is printed and prompts the user to give input. In the example below, the string entered by the user is stored the variable `name`. Try executing the program in the interactive notebook by pressing control-enter!" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Give me your name: Jarkko\n", "Hello, Jarkko\n" ] } ], "source": [ "name=input(\"Give me your name: \")\n", "print(\"Hello,\", name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Indentation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Repetition is possible with the for loop. Note that the body of for loop is indented with a tabulator or four spaces.\n", "Unlike in some other languages, braces are not needed to denote the body of the loop. When the indentation stops, the body of the loop ends." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n", "Hello\n", "Hello\n", "Bye!\n" ] } ], "source": [ "for i in range(3):\n", " print(\"Hello\")\n", "print(\"Bye!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Indentation applies to other compound statements as well, such as bodies of functions, different branches of an if statement, and while loops. We shall see examples of these later." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `range(3)` expression above actually results with the sequence of integers 0, 1, and 2. So, the range is a half-open interval with the end point excluded from the range. In general, expression range(n) gives integers 0, 1, 2, ..., n-1. Modify the above program to make it also print the value of variable i at each iteration. Rerun the code with control-enter." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "####