Python2019/labs01/podstawy.ipynb
2019-01-26 08:23:20 +01:00

1048 lines
19 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Podstawy Pythona: cz. 1\n",
"\n",
" \n",
"\n",
"## 26 stycznia 2019"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"sentence = \"I'd guess that it's Frank Sinatra.\"\n",
"sentence = sentence.replace(' \\'', '\\'') \n",
"sentence = sentence.replace(' n\\'t', 'n\\'t')\n",
"words = sentence.split()\n",
"for i in range(len(words)):\n",
" for contraction, expansion in contractions_expansions.items():\n",
" if contraction == words[i]: \n",
" words[i] = expansion \n",
"sentence = ' '.join(words) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"print('Hello Python')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello\n",
"Python\n",
"Hello Python !\n"
]
}
],
"source": [
"print('Hello') \n",
"print('Python')\n",
"print('Hello', 'Python', '!')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Typy proste:\n",
" * liczby całkowite: `int`, np. `3`, `1000`, `-100`, `0`\n",
" * liczby rzeczywiste: `float`: `3.14`, `5.0`, `-0.001`\n",
" * napisy: `str`: `\"Python\"`, `\"Rock'n'Roll\"`, `'zalicznie z przedmiotu'`\n",
" * logiczne `bool`, `True`, `False`\n",
" * None: `None`"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"1000.0\n",
"100 100.0 Sto\n",
"Zaokrąglenie PI: 3.141519\n"
]
}
],
"source": [
"print(10)\n",
"print(1000.0)\n",
"print (100, 100.0, \"Sto\")\n",
"print(\"Zaokrąglenie PI:\", 3.141519)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'int'>\n",
"<class 'float'>\n",
"<class 'str'>\n",
"<class 'NoneType'>\n"
]
}
],
"source": [
"print(type(100))\n",
"print(type(20.5))\n",
"print(type(\"okno\"))\n",
"print(type(None))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Operacje arytmetyczne na liczbach:\n",
" * dodawanie `+`, np. `2 + 3`\n",
" * odejmowanie `-`, np. `10-9`\n",
" * mnożenie `*`, np. `2.0 * 3.0`\n",
" * dzielenie `/`, np. `3 / 4` ( == 0.75)\n",
" * dzielenie całkowite `//`, `3 / 4` ( == 0)\n",
" * potęgowanie `**`, np. `10 ** 3` ( == 1000)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"2 ** 10 = 1024\n",
"0.5555555555555556\n"
]
}
],
"source": [
"print(1 + 2 - 3)\n",
"print(\"2 ** 10\", \"=\", 2 ** 10)\n",
"print(1 + 2 - 3 + (4 * 5) / 6 ** 2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Operacje na napisach:\n",
" * konkatenacja `+`, np. `\"Tarnowo \" + \"Podgórne\"`\n",
" * wielokrotność: `*`, np. `\"O\" * 6` (\"OOOOOO\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Noooooooooo!\n"
]
}
],
"source": [
"print(\"N\" + \"o\" * 10 + '!')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Konwersja typów:"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"200\n",
"100100\n",
"200.0\n",
"True\n"
]
}
],
"source": [
"print(int(\"100\") * 2)\n",
"print(str(100) * 2)\n",
"print(float(100) * 2)\n",
"print(bool(\"Fałsz\"))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Zmienne\n",
" * case sensitive,\n",
" * brak deklaracji typu,\n",
" * do zmiennej można przypisać wszystko."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"email: t.dwojak@amu.edu.pl\n"
]
}
],
"source": [
"user = \"t.dwojak\"\n",
"domain = \"amu.edu.pl\"\n",
"email = user + \"@\" + domain\n",
"print(\"email:\", email)"
]
},
{
"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": [
"# Podstawy cz. 2"
]
},
{
"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.\n",
" * Funkcja `len` zwraca liczbę elementów listy"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Liczba elementów 5\n"
]
}
],
"source": [
"x = [] # albo równoważnie pusta_lista = list()\n",
"oceny = [5, 4, 3, 5, 5]\n",
"roznosci = [3.14, \"pi\", [\"pi\"], 3]\n",
"list_0_9 = list(range(10))\n",
"print(\"Liczba elementów\", len(oceny))"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5, 4, 3, 5, 5]\n",
"4\n"
]
}
],
"source": [
"print(oceny)\n",
"print(oceny[1])"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Dodawanie i usuwanie elementów z listy\n",
"\n",
"Istnieją dwie metody:\n",
" * `append(x)`: dodaje x na koniec listy\n",
" * `extend(x)`: rozszerza listę o każdy element z x "
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['duck-duck-go', 'yahoo']\n"
]
}
],
"source": [
"engines = []\n",
"engines.append('duck-duck-go')\n",
"engines.append(\"yahoo\")\n",
"print(engines)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['google', 1, 'sto']\n"
]
}
],
"source": [
"m = [\"google\", 'bing']\n",
"m.extend(l)\n",
"print(m)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 3, 2, 3, 1, 2]\n"
]
}
],
"source": [
"liczby = [1,2,3,2,3,1,2,4]\n",
"liczby.pop() # Domyślnie usuwa ostatni element z listy\n",
"liczby.remove(2)\n",
"print(liczby)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Inne przydatne metody:\n",
" * `sort()`: sortuje listę rosnąco\n",
" * `count(x)`: zlicza wystąpienia x w liście\n",
" * `index(x)`: zwraca indeks pierwszego wystąpienia x"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"7\n",
"[1, 1, 2, 2, 2, 3, 3, 4]\n"
]
}
],
"source": [
"liczby = [1,2,3,2,3,1,2,4]\n",
"print(liczby.count(1))\n",
"print(liczby.index(4))\n",
"liczby.sort()\n",
"print(liczby)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Indeksowanie"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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": [
"oceny = [1, 3, 2, 3, 1, 2, 4]\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": [
"## 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": 22,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"element 0\n",
"element 1\n",
"element 2\n"
]
}
],
"source": [
"n = 3\n",
"\n",
"for i in range(n):\n",
" print('element', i)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"jeden\n",
"dwa\n",
"trzy\n"
]
}
],
"source": [
"l = [\"jeden\", \"dwa\", \"trzy\"]\n",
"for item in l:\n",
" print(item)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]\n"
]
}
],
"source": [
"kwadraty = []\n",
"\n",
"for i in range(10):\n",
" kwadraty.append(i ** 2)\n",
"\n",
"print(kwadraty)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Słowniki: `dict`"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Justyna': [5, 5, 5], 'Bartek': [3, 4, 5], 'Ola': [3, 3, 3]}\n",
"{'Justyna': [5, 5, 5], 'Bartek': [3, 4, 5], 'Ola': [3, 3, 3], 'Jan': [3, 4, 5]}\n"
]
}
],
"source": [
"slownik = {}\n",
"s_oceny = {\"Justyna\" : [5,5,5], \"Bartek\" : [3,4,5], \"Ola\": [3,3,3]}\n",
"print(s_oceny)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Justyna': [5, 5, 5], 'Bartek': [3, 4, 5], 'Ola': [3, 3, 3], 'Jan': [3, 4, 5]}\n"
]
}
],
"source": [
"s_oceny[\"Jan\"] = [3,4,5]\n",
"print(s_oceny)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Iterowanie po słowniku"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Justyna : [5, 5, 5]\n",
"Bartek : [3, 4, 5]\n",
"Ola : [3, 3, 3]\n",
"Jan : [3, 4, 5]\n"
]
}
],
"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": 30,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Podaj liczbe:\n",
"3\n"
]
}
],
"source": [
"zmienna = int(input(\"Podaj liczbe:\\n\"))\n",
"\n",
"if zmienna < 0:\n",
" print(\"Ujemna!\")\n",
"elif zmienna > 0:\n",
" print(\"Dodatnia\") \n",
"else:\n",
" print(\"Zero!\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Przydatne warunki\n",
"\n",
" * Porównanie za pomocą `==`\n",
" * znak różności: `!=`\n",
" * Sprawdzenie, czy coś jest w liście: `item in l`\n",
" * Do łączenia warunków służą słowa klucznowe `and` i `or`"
]
},
{
"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": {
"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": [
"slowa = ['Bardzo' , 'ważna', 'wiadomość']\n",
"print(' '.join(slowa))"
]
},
{
"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": "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?"
]
},
{
"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))"
]
}
],
"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.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}