215 lines
2.9 KiB
Python
215 lines
2.9 KiB
Python
|
|
||
|
# coding: utf-8
|
||
|
|
||
|
# # Python: podstaw ciąg dalszy
|
||
|
#
|
||
|
# ## Tomasz Dwojak
|
||
|
#
|
||
|
# ### 13 czerwca 2018
|
||
|
|
||
|
# ## Co już znamy?
|
||
|
# * podstawowe typy danych i operacje na nich
|
||
|
# * Instrukcje sterujące: ``if``, ``for``
|
||
|
# * pisanie funkcji
|
||
|
#
|
||
|
#
|
||
|
|
||
|
# ## Co na dziś?
|
||
|
#
|
||
|
# * ``tuple`` i ``set``,
|
||
|
# * operacje na plikach,
|
||
|
# * coś więcej o funkcjach
|
||
|
# * korzystanie z bibliotek
|
||
|
# * przegląd najważniejszych bibliotek
|
||
|
|
||
|
# In[9]:
|
||
|
|
||
|
|
||
|
def dwojak(x):
|
||
|
x *= 2
|
||
|
return x
|
||
|
|
||
|
l = [1, 2, 3]
|
||
|
s = "123"
|
||
|
|
||
|
dwojak(l)
|
||
|
dwojak(s)
|
||
|
print(dwojak(1))
|
||
|
print(l)
|
||
|
print(s)
|
||
|
|
||
|
|
||
|
# ## Mutable i Immutable
|
||
|
#
|
||
|
# ### Mutable
|
||
|
# * listy,
|
||
|
# * słowniki,
|
||
|
# * sety,
|
||
|
# * własnoręcznie zdefiniowane klasy.
|
||
|
#
|
||
|
# ### Immutable
|
||
|
# * liczby: inty i floaty,
|
||
|
# * napisy,
|
||
|
# * tuple.
|
||
|
|
||
|
# ## Nieoczywistości
|
||
|
|
||
|
# In[11]:
|
||
|
|
||
|
|
||
|
def dwojak1(x): x *= 2
|
||
|
def dwojak2(x):
|
||
|
x = x * 2
|
||
|
print("F:", x)
|
||
|
|
||
|
l = [1,2, 3]
|
||
|
dwojak1(l)
|
||
|
print(l)
|
||
|
|
||
|
l = [1,2, 3]
|
||
|
dwojak2(l)
|
||
|
print(l)
|
||
|
|
||
|
|
||
|
# In[17]:
|
||
|
|
||
|
|
||
|
l = [1, 2, 3]
|
||
|
e = l[:]
|
||
|
e.append(4)
|
||
|
print(l)
|
||
|
print(e)
|
||
|
|
||
|
|
||
|
# In[19]:
|
||
|
|
||
|
|
||
|
e = []
|
||
|
f = [e for i in range(3)]
|
||
|
f[0].append(1)
|
||
|
print(f)
|
||
|
|
||
|
|
||
|
# ## To może ``tuple``?
|
||
|
# * stały rozmiar,
|
||
|
# * immutable,
|
||
|
# * mogą być kluczami w słownikach
|
||
|
|
||
|
# In[25]:
|
||
|
|
||
|
|
||
|
t = (1, "napis", [])
|
||
|
t[-1].append(0)
|
||
|
print(t)
|
||
|
print(len(t))
|
||
|
print({t: None})
|
||
|
|
||
|
|
||
|
# ## Funkcje cz. 2
|
||
|
|
||
|
# In[36]:
|
||
|
|
||
|
|
||
|
def suma(*args):
|
||
|
return sum(args)
|
||
|
print(suma(1,2,3,4,5))
|
||
|
|
||
|
def greet_me(z=None,**kwargs):
|
||
|
if kwargs is not None:
|
||
|
for key, value in kwargs.items():
|
||
|
print("%s == %s" %(key,value))
|
||
|
greet_me(a=1, b=(3,4))
|
||
|
|
||
|
|
||
|
# ## Generatory
|
||
|
|
||
|
# In[38]:
|
||
|
|
||
|
|
||
|
def alfaRange(x, y):
|
||
|
for i in range(ord(x), ord(y)):
|
||
|
print(i)
|
||
|
yield chr(i)
|
||
|
|
||
|
for c in alfaRange('a', 'e'):
|
||
|
print(c)
|
||
|
|
||
|
|
||
|
# ## Operacje na plikach
|
||
|
|
||
|
# In[45]:
|
||
|
|
||
|
|
||
|
plik = open("haslo.txt", 'r')
|
||
|
for linia in plik.readlines():
|
||
|
print(linia.strip())
|
||
|
print(plik.read())
|
||
|
print(">>")
|
||
|
plik.close()
|
||
|
|
||
|
|
||
|
# In[47]:
|
||
|
|
||
|
|
||
|
with open("haslo.txt", 'r') as plik:
|
||
|
for linia in plik.readlines():
|
||
|
print(linia)
|
||
|
print(plik.read())
|
||
|
|
||
|
|
||
|
# In[48]:
|
||
|
|
||
|
|
||
|
with open("haslo2.txt", 'w') as plik:
|
||
|
for word in ('corect', 'horse', 'battery', 'staple'):
|
||
|
plik.write(word)
|
||
|
plik.write('\n')
|
||
|
with open("haslo2.txt", 'w+') as plik:
|
||
|
plik.writelines([' '.join(('corect', 'horse', 'battery', 'staple'))])
|
||
|
|
||
|
|
||
|
# # Korzystanie z modułów
|
||
|
|
||
|
# ## Importowanie
|
||
|
|
||
|
# In[49]:
|
||
|
|
||
|
|
||
|
import os
|
||
|
print(os.name)
|
||
|
|
||
|
from os import getenv
|
||
|
print('Nazwa uzytkownika: {}'.format(getenv("USER")))
|
||
|
|
||
|
|
||
|
# In[50]:
|
||
|
|
||
|
|
||
|
from collections import *
|
||
|
print(Counter("konstantynopolitańczykowianeczka"))
|
||
|
|
||
|
import numpy as np
|
||
|
np.array([[1, 3, 4, 5]], dtype='float32')
|
||
|
|
||
|
|
||
|
# ## Instalacja
|
||
|
#
|
||
|
# * lokalnie (per użytkownik) lub globalnie
|
||
|
# * pyCharm lub linia komend, np. ``pip install --user flask`` lub ``python -m pip install --user flask``
|
||
|
|
||
|
# ## Wczytywanie z klawiatury
|
||
|
|
||
|
# In[51]:
|
||
|
|
||
|
|
||
|
name = input("What's your name?\n")
|
||
|
print("Welcome home, {}.".format(name))
|
||
|
|
||
|
|
||
|
# In[ ]:
|
||
|
|
||
|
|
||
|
liczby = eval(input("Podaj liczby"))
|
||
|
print(sum(liczby))
|
||
|
|