1
0
forked from tdwojak/Python2017
Python2017/labs03/Podstawy 2.ipynb
2017-12-02 12:10:31 +01:00

523 lines
9.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Python: podstaw ciąg dalszy\n",
"\n",
"## Tomasz Dwojak\n",
"\n",
"### 2 grudnia 2017"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Co już znamy?\n",
" * podstawowe typy danych i operacje na nich\n",
" * Instrukcje sterujące: ``if``, ``for``\n",
" * pisanie funkcji\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"## Co na dziś?\n",
"\n",
" * ``tuple`` i ``set``,\n",
" * operacje na plikach,\n",
" * coś więcej o funkcjach\n",
" * korzystanie z bibliotek\n",
" * przegląd najważniejszych bibliotek"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"123\n",
"[1, 2, 3, 1, 2, 3]\n"
]
}
],
"source": [
"def dwojak(x): x *= 2\n",
" \n",
"l = [1, 2, 3]\n",
"s = \"123\"\n",
"\n",
"dwojak(l)\n",
"dwojak(s)\n",
"\n",
"print(l)\n",
"print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Mutable i Immutable\n",
"\n",
"### Mutable\n",
" * listy,\n",
" * słowniki,\n",
" * sety,\n",
" * własnoręcznie zdefiniowane klasy.\n",
" \n",
"### Immutable\n",
" * liczby: inty i floaty,\n",
" * napisy,\n",
" * tuple."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Nieoczywistości"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 1, 2, 3]\n",
"[1, 2, 3]\n"
]
}
],
"source": [
"def dwojak1(x): x *= 2\n",
"def dwojak2(x): x = x * 2\n",
"\n",
"l = [1,2, 3]\n",
"dwojak1(l)\n",
"print(l)\n",
"\n",
"l = [1,2, 3]\n",
"dwojak2(l)\n",
"print(l)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4]\n"
]
}
],
"source": [
"l = [1, 2, 3]\n",
"e = l\n",
"e.append(4)\n",
"print(l)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1], [1], [1]]\n"
]
}
],
"source": [
"e = []\n",
"f = [e for i in range(3)]\n",
"f[0].append(1)\n",
"print(f)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## To może ``tuple``?\n",
" * stały rozmiar,\n",
" * immutable,\n",
" * mogą być kluczami w słownikach"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 'napis', None)\n",
"3\n"
]
}
],
"source": [
"t = (1, \"napis\", None)\n",
"elem = t[0]\n",
"print(t)\n",
"print(len(t))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Funkcje cz. 2"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15\n",
"a == 1\n",
"b == (3, 4)\n"
]
},
{
"data": {
"text/plain": [
"'b'"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def suma(*args):\n",
" return sum(args)\n",
"print(suma(1,2,3,4,5))\n",
"\n",
"def greet_me(**kwargs):\n",
" if kwargs is not None:\n",
" for key, value in kwargs.items():\n",
" print(\"%s == %s\" %(key,value))\n",
"greet_me(a=1, b=(3,4))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Generatory"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a\n",
"b\n",
"c\n",
"d\n"
]
}
],
"source": [
"def alfaRange(x, y):\n",
" for i in range(ord(x), ord(y)):\n",
" yield chr(i)\n",
"\n",
"for c in alfaRange('a', 'e'):\n",
" print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Operacje na plikach"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"W Paryżu najlepsze kasztany są na placu Pigalle\n",
"Zuzanna lubi je tylko jesienią.\n",
"\n"
]
}
],
"source": [
"plik = open(\"haslo.txt\", 'r')\n",
"for linia in plik.readlines():\n",
" print(linia.strip())\n",
"print(plik.read())\n",
"plik.close()"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"W Paryżu najlepsze kasztany są na placu Pigalle\n",
"\n",
"Zuzanna lubi je tylko jesienią.\n",
"\n"
]
}
],
"source": [
"with open(\"haslo.txt\", 'r') as plik:\n",
" for linia in plik.readlines():\n",
" print(linia)\n",
"# print(plik.read())"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"with open(\"haslo2.txt\", 'w') as plik:\n",
" for word in ('corect', 'horse', 'battery', 'staple'):\n",
" plik.write(word)\n",
" plik.write('\\n')\n",
"with open(\"haslo2.txt\", 'w+') as plik:\n",
" plik.writelines([' '.join(('corect', 'horse', 'battery', 'staple'))])"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Korzystanie z modułów"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Importowanie"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"posix\n",
"Nazwa uzytkownika: tomaszd\n"
]
}
],
"source": [
"import os\n",
"print(os.name)\n",
"\n",
"from os import getenv\n",
"print('Nazwa uzytkownika: {}'.format(getenv(\"USER\")))"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Counter({'o': 4, 'n': 4, 'a': 4, 'k': 3, 't': 3, 'y': 2, 'i': 2, 'c': 2, 'z': 2, 's': 1, 'p': 1, 'l': 1, 'ń': 1, 'w': 1, 'e': 1})\n"
]
},
{
"data": {
"text/plain": [
"array([[ 1., 3., 4., 5.]], dtype=float32)"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from collections import *\n",
"print(Counter(\"konstantynopolitańczykowianeczka\"))\n",
"\n",
"import numpy as np\n",
"np.array([[1, 3, 4, 5]], dtype='float32')"
]
},
{
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}