1
0
forked from tdwojak/Python2019
Python2019/labs02/Podstawy cz. 2.ipynb
2019-01-27 08:55:03 +01:00

406 lines
8.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Podstawy Pythona: cz. 3\n",
"\n",
" \n",
"\n",
"## 27 stycznia 2019"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Co już było?\n",
" * podstawowe typy danych\n",
" * operacje arytmetyczne\n",
" * wyświtlanie na ekran\n",
" * pętla `for`, instrukcja `if`\n",
" * listy i słowniki"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Funkcje\n",
"\n",
" * znamy kilka funkcji, np. print, len\n",
" * funckje w pythonie nie odbiegają od funkcji w innych językach\n",
" * może istnieć tylko 1 funkcja o danej nazwie"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"def nazwa_funkcji(argument1, argument2, argument3):\n",
" # instrukcje do wykoniania\n",
" return wartosc"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"def is_greater_than_5(x):\n",
" if x > 5:\n",
" return True\n",
" else:\n",
" return False\n",
" \n",
"print(is_greater_than_5(10))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8.0\n"
]
}
],
"source": [
"def srednia(lista):\n",
" s = 0\n",
" for item in lista:\n",
" s += item\n",
" return s / len(lista) \n",
"\n",
"print(srednia([7,8,9]))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"def count(lista, item):\n",
" l = 0\n",
" for i in lista:\n",
" if i == item:\n",
" l += 1\n",
" return l"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"count([3,3,3,4], 3)\n",
"count(lista=[3,3,3,4], item=3)\n",
"count(item=3, lista=[3,3,3,4])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"def is_grater_than(x, y=0):\n",
" if x > y:\n",
" return True\n",
" else:\n",
" return False"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"is_grater_than(5)\n",
"is_grater_than(x=5)\n",
"is_grater_than(5, 0)\n",
"is_grater_than(5, 6)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 'b', 'c', 'd']\n"
]
}
],
"source": [
"def alfaRange(x, y):\n",
" for i in range(ord(x), ord(y)):\n",
" yield chr(i)\n",
"print(list(alfaRange('a', 'e')))"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Operacje na plikach"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"plik = open(\"haslo.txt\", 'r')\n",
"for linia in plik.readlines():\n",
" print(linia.strip())\n",
"print(\">>\")\n",
"plik.close()"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: 'haslo.txt'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-18-c98a6e23ed86>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"haslo.txt\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'r'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mplik\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mlinia\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mplik\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreadlines\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlinia\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'haslo.txt'"
]
}
],
"source": [
"with open(\"haslo.txt\", 'r') as plik:\n",
" for linia in plik.readlines():\n",
" print(linia)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"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 bibliotek"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"posix\n",
"Nazwa uzytkownika: tomasz\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": 21,
"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": 21,
"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": {},
"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.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}