{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Praktikum 1, lahendused\n", "\n", "Allolevad ülesanded on kusrsuse \"Sissejuhatus programmeerimisse Pythoni baasil\" YFX0500 **praktikumis** koos kursusega ja **kodus** iseseisvalt lahendamiseks. Kuidas lahendada allolevaid ülesandeid? Uuri ja kasuta loengus kommenteeritud süntaksi näiteid, abiinfot (Pythoni sisseehitatud funktsioon `help`, jne.), internetti ja enda aju. Küsimuste korral, mis tekivad praktikumi ajal, pöördu õppejõu või õppejõudude poole. Iganädalaselt peale praktikumi toimumist avaldatakse ülesannete lahendused kursuse kodulehel.\n", "\n", "
Viimati uuendatud 26.08.2024.
\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ülesanne 1:**\n", "\n", "Tutvu Jupyteri töölehe kasutajaliidesega (IDE Jupyter Notebook või JupyterLab):\n", "\n", "1. Loo või lisa paar/mitu uut koodirakku.\n", "2. Ühenda loodud rakud üheks koodirakuks.\n", "3. Lahuta loodud rakk kaheks rakkuks.\n", "4. Kustuta loodud rakud." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ülesanne 2:**\n", "\n", "Kirjuta enda esimene programm, nn. \"Hello, World!\" programm." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World!\n" ] } ], "source": [ "print('Hello, World!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ülesanne 3:**\n", "\n", "Kirjuta uus \"Hello, World!\" programm. Kommenteeri enda kirjutatud koodi kommentaariga \"See on minu programm!\"." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World!\n" ] } ], "source": [ "print('Hello, World!') # See on minu programm!" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World!\n" ] } ], "source": [ "# See on minu programm!\n", "print('Hello, World!')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, World!\n" ] } ], "source": [ "print('Hello, World!')\n", "# See on minu programm!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ülesanne 4:**\n", "\n", "Uuri mis abiinfo on funktsiooniga `print` kaasa pandud." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mSignature:\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msep\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m' '\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'\\n'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfile\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mflush\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n", "Prints the values to a stream, or to sys.stdout by default.\n", "\n", "sep\n", " string inserted between values, default a space.\n", "end\n", " string appended after the last value, default a newline.\n", "file\n", " a file-like object (stream); defaults to the current sys.stdout.\n", "flush\n", " whether to forcibly flush the stream.\n", "\u001b[0;31mType:\u001b[0m builtin_function_or_method" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "?print" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mSignature:\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msep\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m' '\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mend\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'\\n'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfile\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mflush\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n", "Prints the values to a stream, or to sys.stdout by default.\n", "\n", "sep\n", " string inserted between values, default a space.\n", "end\n", " string appended after the last value, default a newline.\n", "file\n", " a file-like object (stream); defaults to the current sys.stdout.\n", "flush\n", " whether to forcibly flush the stream.\n", "\u001b[0;31mType:\u001b[0m builtin_function_or_method" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "print?" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function print in module builtins:\n", "\n", "print(*args, sep=' ', end='\\n', file=None, flush=False)\n", " Prints the values to a stream, or to sys.stdout by default.\n", " \n", " sep\n", " string inserted between values, default a space.\n", " end\n", " string appended after the last value, default a newline.\n", " file\n", " a file-like object (stream); defaults to the current sys.stdout.\n", " flush\n", " whether to forcibly flush the stream.\n", "\n" ] } ], "source": [ "help(print)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# CTRL + I ja kliki objektile" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ülesanne 5:**\n", "\n", "Väljasta konsooli (prindi) lause: `Juulius Tipika koera nimi on d'Alembert.`" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Juulius Tipika koera nimi on d'Alembert.\n" ] } ], "source": [ "print(\"Juulius Tipika koera nimi on d'Alembert.\")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Juulius Tipika koera nimi on d'Alembert.\n" ] } ], "source": [ "print('''Juulius Tipika koera nimi on d'Alembert.''')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Juulius Tipika koera nimi on d'Alembert.\n" ] } ], "source": [ "print(\"\"\"Juulius Tipika koera nimi on d'Alembert.\"\"\")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Juulius Tipika koera nimi on d'Alembert.\n" ] } ], "source": [ "print('Juulius Tipika koera nimi on d\\'Alembert.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ülesanne 6:**\n", "\n", "Leia:\n", "\n", "$$8 \\cdot 2 - 10 = ?$$\n", "\n", "$$8 \\cdot (2 - 10) = ?$$\n", "\n", "$$ \\sqrt{2} = ? $$\n", "\n", "$$ \\sqrt[3]{2^5} = ? $$\n", "\n", "$$ \\left( \\sqrt[3]{2} \\right)^5 = ? $$\n", "\n", "$$\\frac{2^2 + 3^2}{\\sqrt{55}} = ?$$\n", "\n", "$$2^{3}\\!\\!\\!\\! \\mod 3 = ?$$" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "8 * 2 - 10" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-64" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "8 * (2 - 10)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.4142135623730951" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ** 0.5 # Vähem arvutustehteid võrreldes 2**(1/2)." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.174802103936399" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 ** (5/3)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.174802103936399" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2 ** (1/3)) ** 5 # Sama vastus mis eelmine. NB! Astendamise tehete järjekord Pythonis: paremalt vasakule." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.7529196424044293" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(2**2 + 3**2) / 55**(1/2)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**3 % 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ülesanne 7:**\n", "\n", "Kontrolli kas operaatoreid `+`, `-`, `*`, `/` on võimalik rakendada sõnedele? Kontrolli kas operaatoreid `+`, `-`, `*`, `/` on võimalik rakendadaja sõnedele ja numbritele segamini (segatehted)?\n", "\n", "Näiteks: `'abc' + 2`, `'abc' + 'abc'`, `'abc' - 'b'`, `2*'a'`, `'abc'/3`, jne." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abcdef'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'abc' + 'def' # Sidurdus = konkatenatsioon, + operaator on kontekstitundlik, polümorfism." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Juulius Tipikas 2'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'Juulius' + ' ' + 'Tipikas' + ' 2'" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'JuuliusJuuliusJuuliusJuuliusJuulius'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5*'Juulius'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ülesanne 8:**\n", "\n", "Kolme sõnet `Python` saab genereerida kasutades koodi `print('Python', 'Python', 'Python')`. Genereeri 200 sõnet `Python`." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python Python \n" ] } ], "source": [ "print(200 * 'Python ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ülesanne 9:**\n", "\n", "Genereeri konsooli järgmine väljund:\n", "\n", "```\n", " /|\n", " / |\n", " / |\n", " / |\n", " /____|\n", "```" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " /|\n", " / |\n", " / |\n", " / |\n", " /____|\n" ] } ], "source": [ "print('''\n", " /|\n", " / |\n", " / |\n", " / |\n", " /____|''')" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " /|\n", " / |\n", " / |\n", " / |\n", "/____|\n" ] } ], "source": [ "print(' /|')\n", "print(' / |')\n", "print(' / |')\n", "print(' / |')\n", "print('/____|') # Töömahukam võrreldes eelmisega." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Ülesanne 10:**\n", "\n", "Salvesta enda tehtud töö kasutades Jupyter integreeritud arenduskeskkonna (Jupyter IDE) graafilist kasutajaliidest. Sinu töö salvestatakse interaktiivse pythoni töölehe faili mille faililaiend on .ipynb (Interactive Python NoteBook). Enamus operatsiooni süsteemides ei avane selline fail sellele lihtsalt kaks korda klikides, Sa pead avama Jupyter IDE ja kasutama selle faililehitsejat faili avamiseks. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.0" } }, "nbformat": 4, "nbformat_minor": 4 }