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

602 lines
11 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Ogólne wprowadzenie do Pythona\n",
"\n",
" \n",
"\n",
"## 13 maja 2017"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Hello Python!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"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 prawie równe \", 3.1415)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Komentarze\n",
" * Komentarze w Pythonie zaczynają się od '#',\n",
" * Komentarze nie są interpretowane."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"print(\"Ala ma kota.\") # A to jest komentarz\n",
"# print(\"Nie chcę być wydrukowanym\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Zmienne\n",
" * case sensitive,\n",
" * brak deklaracji typu,\n",
" * do zmiennej można przypisać każdy obiekt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"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",
"czy_lubie_pythona = True # False "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"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": {
"collapsed": false,
"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": {
"collapsed": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"oceny.append(4) # Dodanie elementu na koniec\n",
"print(oceny)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"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": {
"collapsed": false,
"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": {
"collapsed": false,
"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": {
"collapsed": false,
"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": {
"collapsed": false,
"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": false,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"for zmienna in lista:\n",
" # jakiś kod"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"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": false,
"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": false,
"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": false,
"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": false,
"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": false,
"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": false,
"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": false,
"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": false,
"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": false,
"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": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}