From ea4466637afc466f055e325faa168db04e7815a7 Mon Sep 17 00:00:00 2001 From: Tomasz Dwojak Date: Sat, 12 May 2018 10:05:27 +0200 Subject: [PATCH] Add labs 01 --- labs01/podstawy.ipynb | 801 ++++++++++++++++++++++++++++++++++++++ labs01/wprowadzenie.ipynb | 146 +++++++ 2 files changed, 947 insertions(+) create mode 100644 labs01/podstawy.ipynb create mode 100644 labs01/wprowadzenie.ipynb diff --git a/labs01/podstawy.ipynb b/labs01/podstawy.ipynb new file mode 100644 index 0000000..175783e --- /dev/null +++ b/labs01/podstawy.ipynb @@ -0,0 +1,801 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Ogólne wprowadzenie do Pythona\n", + "\n", + " \n", + "\n", + "## 19 Listopada 2017" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Hello Python!" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "adflakjfsldkfjasd\n", + "r\n", + "0.6666666666666666\n" + ] + } + ], + "source": [ + "r = \"adflakjfsldkfj\" + \"asd\"\n", + "print(r)\n", + "print('r')\n", + "print(2 / 3.0)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "print('Hello Python')\n", + "print(10) \n", + "print(3.14)\n", + "print('123456789 * 987654321')\n", + "print(10 ** 20)\n", + "print(12 ** (3 + 4 *(567 % 8) / 9))\n", + "print(\"PI\", \"jest równe\", 3.1415)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Komentarze\n", + " * Komentarze nie są interpretowane.\n", + " * Komentarze w Pythonie zaczynają się od znaku '#'\n", + " * Istnieją komentarze wielolinijkowe tagowane potrójnym \", czyli \"\"\" \"\"\"\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "print(\"Bardzo ważna wiadomość\") # A to jest komentarz\n", + "\"\"\"\n", + "Komentarz\n", + "wielo-\n", + "linijkowy\n", + "\"\"\"\n", + "# print(\"Nie chcę być wydrukowanym\")\n", + "print(\"A teraz chcę\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Zmienne\n", + " * case sensitive,\n", + " * brak deklaracji typu,\n", + " * do zmiennej można pezypisać wszystko." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "alaalaalaalaalaalaalaala\n" + ] + } + ], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\n", + "\n", + "\n" + ] + } + ], + "source": [ + "print(type(ocena_z_pythona))\n", + "print(type(pi))\n", + "print(type(imie_prowadzacego))\n", + "print(type(nazwisko_prowadzego))\n", + "print(type(None))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Listy\n", + " * W Pythonie nie ma tablic, są listy;\n", + " * Listy mogą przechowywać elementy różnych typów;\n", + " * Indeksowanie zaczyna się od 0." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "pusta_lista = [] # albo równoważnie pusta_lista = list()\n", + "oceny = [5, 4, 3, 5, 5]\n", + "roznosci = [3.14, \"pi\", [\"pi\"], 3]\n", + "#print(oceny[1])\n", + "ciag_10_elem = range(10)\n", + "\n", + "print(oceny[int(12/ 5)])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Dodawanie i usuwanie elementów z listy" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "11\n" + ] + } + ], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 3, 2, 3, 1, 2, 4]\n" + ] + } + ], + "source": [ + "#oceny.pop(0) # Domyślnie usuwa ostatni element z listy\n", + "oceny = [1,2,3,2,3,1,2, 4]\n", + "oceny.remove(2)\n", + "print(oceny)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Indeksowanie" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 3, 2, 3, 1, 2, 4]\n", + "pierwszy element: 1\n", + "ostatni element: 4\n", + "5 pierwszych: [1, 3, 2, 3, 1]\n", + "5 ostatnich [2, 3, 1, 2, 4]\n", + "od drugiego, do piątego [3, 2, 3, 1]\n", + "parzyste: [3, 3, 2]\n", + "od tyłu [4, 2, 1, 3, 2, 3, 1]\n" + ] + } + ], + "source": [ + "print(oceny)\n", + "print('pierwszy element:', oceny[0])\n", + "print('ostatni element:', oceny[-1])\n", + "print('5 pierwszych:', oceny[:5])\n", + "print('5 ostatnich', oceny[-5:])\n", + "print('od drugiego, do piątego', oceny[1:5])\n", + "print('parzyste:', oceny[1:6:2])\n", + "print('od tyłu', oceny[::-1])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Co możemy zrobić z listą?" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 2, 2, 3, 3, 4]\n", + "liczba piątek 0\n" + ] + } + ], + "source": [ + "oceny.sort()\n", + "print(oceny)\n", + "oceny.reverse()\n", + "print(\"liczba piątek\", oceny.count(5))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Pętla typu ```for```\n", + " * W Pythonie pętla *for* działa jak pętla *for each* w innych językach;\n", + " * W Pythonie nie ma klamr, ani *begin* i *end*; jako ciało pętli musi być wcięte (najcześciej 4 spacje)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "for i in range(10):# range[5] = [0,1,2,3,4]\n", + " pass\n", + " \n", + "print('element:', i) # Uwaga wcięcie!\n", + "print('sqr:', i **2) # Uwaga wcięcie!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + "Schemat:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "for zmienna in lista:\n", + " # jakiś kod" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "for ocena in oceny:\n", + " print(\"Ocena:\", ocena)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Lista kwadratów" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "kwadraty = []\n", + "for i in range(10):\n", + " kwadraty.append(i ** 2)\n", + "print(kwadraty)\n", + "print(kwadraty[5])" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Słowniki, czyli mapy" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "slownik = {}\n", + "s_oceny = {\"Justyna\" : [5,5,5], \"Bartek\" : [3,4,5], \"Ola\": [3,3,3]}\n", + "print(s_oceny)\n", + "s_oceny[1234567890] = [3,4,5]\n", + "#print(\"ADAM:\", s_oceny[\"Adam\"])\n", + "print(s_oceny)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "s_oceny['Ala'] = [2,2,2,4,4,4,3,3,3]\n", + "print(s_oceny)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Iterowanie po słowniku" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "for osoba in s_oceny:\n", + " print(osoba,':', s_oceny[osoba])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "for osoba, oceny in s_oceny.items():\n", + " print(osoba,':', oceny)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Instrukcja sterująca ```if ... else```" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + " * działa tak samo, jak w innych języka;\n", + " * jest dodatkowa instrukcja następnego warunku *elif...*;" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "zmienna = 7\n", + "if zmienna < 0:\n", + " print(\"Ujemna!\")\n", + "elif zmienna == 0:\n", + " print(\"Zero!\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "if \"Ala\" in s_oceny:\n", + " print(\"Ala jest w grupie!\")\n", + "else:\n", + " print(\"Niestety Ali nie ma grupie!\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Funkcje" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "def is_greater_than_5(x):\n", + " if x > 5:\n", + " return True\n", + " else:\n", + " return False" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "print(is_greater_than_5(5))\n", + "print(is_greater_than_5(-100))\n", + "print(is_greater_than_5(500))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Łańcuchy znakowe" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "napis = \"Wiadomość\"\n", + "print(napis)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [], + "source": [ + "for znak in napis:\n", + " print(znak, ord(znak))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [], + "source": [ + "if 'dom' in napis:\n", + " print(True)\n", + "else:\n", + " print(False)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "slide" + } + }, + "outputs": [], + "source": [ + "slowa = ['Bardzo' , 'ważna', 'wiadomość']\n", + "print(' '.join(slowa))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "text = \"Bardzo ważna wiadomość\"\n", + "print(text.split(' '))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true, + "slideshow": { + "slide_type": "fragment" + } + }, + "outputs": [], + "source": [ + "text = \"Nie wszyscy lubią spacje na końcu linii. \"\n", + "print(text)\n", + "print(text.strip(' '))" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Pytania?" + ] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "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.3" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/labs01/wprowadzenie.ipynb b/labs01/wprowadzenie.ipynb new file mode 100644 index 0000000..0371781 --- /dev/null +++ b/labs01/wprowadzenie.ipynb @@ -0,0 +1,146 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Wprowadzenie do programowania w Pythonie\n", + " \n", + "## 19 listopada 2017\n", + " \n", + "### Tomasz Dwojak" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + " * Tomasz Dwojak\n", + " * Doktorant UAM\n", + " * Zainteresowania naukowe: Machine Translation\n", + " * \n", + " * Pokój: B2-36\n", + " * Prawie 10-letnia przygoda z Pythonem" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Warunki zaliczenia\n", + " * Od tej edycji zajęcia z Pythona są osobnym przedmiotem.\n", + " * Ocena na podstawie zadań domowych.\n", + " * Termin na zadania domowe to prawie 2 tygodnie.\n", + " * Więcej na drugiej godzinie zajęć." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Ramowy plan zajęć\n", + " * Dwie części: podstawy Pythona i analiza danych w Pythonie" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Przydatne zasoby\n", + " * [Zanurkuj w Pythonie](https://upload.wikimedia.org/wikibooks/pl/5/5e/Zanurkuj_w_Pythonie.pdf),\n", + " * [Kurs Pythona na DataCamp](https://campus.datacamp.com/courses/intro-to-python-for-data-science),\n", + " * [Kursy na Courserze](https://www.coursera.org/courses?_facet_changed_=true&languages=en&primaryLanguages=en&query=python)\n", + " * [Kurs na edX: Introduction to Python for Data Science (start 7-06-16]](https://www.edx.org/course/introduction-python-data-science-microsoft-dat208x-1)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Środowisko pracy\n", + " * Python 3,\n", + " * narzędzia: PyCharm + Git\n", + " * Wszystko na wiki: https://git.wmi.amu.edu.pl/tdwojak/Python2017/wiki" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Dlaczego Python?" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "fragment" + } + }, + "source": [ + " * język programowania dla wszystkich.\n", + " * posiada ogromną liczbę [modułów](https://pypi.python.org/pypi) (80.000), które ułatwiają pracę;\n", + " * Aktualnie zajmuje 4 miejsce pod względem popularności [(TIOBE Index)](http://www.tiobe.com/tiobe_index);\n", + " \n", + " Motywacje zaczerpnięte m.in. z [5 Reasons why Python is Powerful Enough for Google](https://www.codefellows.org/blog/5-reasons-why-python-is-powerful-enough-for-google)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "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.3" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +}