1
0
forked from tdwojak/Python2017
Python2017/labs01/podstawy.ipynb

770 lines
14 KiB
Plaintext
Raw Normal View History

2017-11-19 09:19:52 +01:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Ogólne wprowadzenie do Pythona\n",
"\n",
" \n",
"\n",
2017-11-19 10:40:34 +01:00
"## 19 Listopada 2017"
2017-11-19 09:19:52 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Hello Python!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 10:40:34 +01:00
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"print('Hello Python')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"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",
2017-11-19 10:40:34 +01:00
"print(\"PI\", \"jest równe \", 3.1415)"
2017-11-19 09:19:52 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Komentarze\n",
2017-11-19 10:40:34 +01:00
" * Komentarze nie są interpretowane.\n",
" * Komentarze w Pythonie zaczynają się od znaku '#'\n",
" * Istnieją komentarze wielolinijkowe tagowane potrójnym \", czyli \"\"\" \"\"\"\n"
2017-11-19 09:19:52 +01:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
2017-11-19 10:40:34 +01:00
"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ę\")"
2017-11-19 09:19:52 +01:00
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Zmienne\n",
" * case sensitive,\n",
" * brak deklaracji typu,\n",
2017-11-19 10:40:34 +01:00
" * do zmiennej można pezypisać wszystko."
2017-11-19 09:19:52 +01:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"zmienna = \"Zmienna\"\n",
"zmienna = 10 ** 44\n",
"ocena_z_pythona = 5\n",
"pi = 3.1415\n",
"imie_prowadzacego = 'Tomek'\n",
"nazwisko_prowadzego = \"Dwojak\"\n",
2017-11-19 10:40:34 +01:00
"czy_lubie_pythona = True # False \n",
"nic = None"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 10:40:34 +01:00
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"lucky_number = 7\n",
"lucky_number +=8\n",
"lucky_number = lucky_number / 3\n",
"print(lucky_number)"
2017-11-19 09:19:52 +01:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"print(type(ocena_z_pythona))\n",
"print(type(pi))\n",
"print(type(imie_prowadzacego))\n",
"print(type(nazwisko_prowadzego))\n",
"print(type(czy_lubie_pythona))"
]
},
{
"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": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"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",
"ciag_10_elem = range(10)\n",
"\n",
"print(\"ciąg:\", ciag_10_elem)\n",
"print(\"rożności:\", roznosci)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Dodawanie i usuwanie elementów z listy"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"oceny.append(4) # Dodanie elementu na koniec\n",
"print(oceny)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"oceny.extend([4,4,4]) # Rozszerzenie o inną listę\n",
"print(oceny)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"oceny.pop(4) # Domyślnie usuwa ostatni element z listy\n",
"print(oceny)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Indeksowanie"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"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[::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": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"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": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"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": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"for zmienna in lista:\n",
" # jakiś kod"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"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": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"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": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"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": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"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": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"for osoba in s_oceny:\n",
" print(osoba,':', s_oceny[osoba])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"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": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"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": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"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": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"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": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 09:19:52 +01:00
"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))"
]
},
2017-11-19 10:40:34 +01:00
{
"cell_type": "markdown",
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Łańcuchy znakowe"
]
},
{
"cell_type": "code",
2017-11-27 00:29:45 +01:00
"execution_count": 2,
2017-11-19 10:40:34 +01:00
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
2017-11-27 00:29:45 +01:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Wiadomość\n"
]
}
],
2017-11-19 10:40:34 +01:00
"source": [
"napis = \"Wiadomość\"\n",
"print(napis)"
]
},
{
"cell_type": "code",
2017-11-27 00:29:45 +01:00
"execution_count": 3,
2017-11-19 10:40:34 +01:00
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
2017-11-27 00:29:45 +01:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"W 87\n",
"i 105\n",
"a 97\n",
"d 100\n",
"o 111\n",
"m 109\n",
"o 111\n",
"ś 347\n",
"ć 263\n"
]
}
],
2017-11-19 10:40:34 +01:00
"source": [
"for znak in napis:\n",
" print(znak, ord(znak))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 10:40:34 +01:00
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"if 'dom' in napis:\n",
" print(True)\n",
"else:\n",
" print(False)"
]
},
2017-11-19 09:19:52 +01:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 10:40:34 +01:00
"slideshow": {
"slide_type": "slide"
}
2017-11-19 09:19:52 +01:00
},
"outputs": [],
2017-11-19 10:40:34 +01:00
"source": [
"slowa = ['Bardzo' , 'ważna', 'wiadomość']\n",
"print(' '.join(slowa))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 10:40:34 +01:00
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"text = \"Bardzo ważna wiadomość\"\n",
"print(text.split(' '))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
2017-11-27 00:29:45 +01:00
"collapsed": true,
2017-11-19 10:40:34 +01:00
"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?"
]
2017-11-19 09:19:52 +01:00
}
],
"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",
2017-11-19 10:40:34 +01:00
"version": "3.6.3"
2017-11-19 09:19:52 +01:00
}
},
"nbformat": 4,
2017-11-19 10:40:34 +01:00
"nbformat_minor": 1
2017-11-19 09:19:52 +01:00
}