In [1]:
help([1, 2, 3])
In [2]:
dir([1, 2, 3])
Out[2]:
In [3]:
sum??
In [3]:
all([1==1, True, 10, -1, False, 3*5==1]), all([1==5, True, 10, -1])
Out[3]:
In [5]:
any([False, True]), any([False, False])
Out[5]:
In [4]:
bin(12), oct(12), hex(12), int('12'), float(12.)
Out[4]:
In [5]:
ord('A'), chr(65)
Out[5]:
In [8]:
raw_input(u"Podaj liczbę: ")
Out[8]:
In [7]:
zip([1,2,3, 3], [2, 3, 4, 10])
Out[7]:
In [10]:
sorted([8, 3, 12, 9, 3]), reversed(range(10)), list(reversed(range(10)))
Out[10]:
In [11]:
len([3, 2, 1]), len([[1, 2], [3, 4, 5]])
Out[11]:
In [12]:
list(), dict(), set(), tuple()
Out[12]:
In [13]:
A = (1, 2, 3)
B = [1, 2, 3]
A == B
Out[13]:
Czym się różni krotka od listy?
In [14]:
A = set()
A.add(2)
A.add(3)
A.add(4)
A
Out[14]:
In [15]:
A.add(3)
A
Out[15]:
In [16]:
B = set((4, 5, 6))
A.difference(B)
Out[16]:
In [17]:
A.symmetric_difference(B)
Out[17]:
In [18]:
A.intersection(B)
Out[18]:
In [19]:
A.union(B)
Out[19]:
In [20]:
pow(2, 10), divmod(10, 3), sum([1, 2, 3])
Out[20]:
In [21]:
round(0.5), round(0.2), round(0.9)
Out[21]:
In [22]:
min([1, 2, 3]), max([1, 2, 3])
Out[22]:
In [23]:
abs(10), abs(-10)
Out[23]:
In [24]:
24 % 5, 24 % 2
Out[24]:
In [25]:
f = lambda x: x+1
f(3)
Out[25]:
In [26]:
f = lambda a, b: a+b**3
In [27]:
f(2, 3)
Out[27]:
In [28]:
map(lambda x: x+10, [0, 2, 5, 234])
Out[28]:
In [29]:
[x+10 for x in [0, 2]]
Out[29]:
In [30]:
map(chr, [80, 121, 67, 105, 114, 99, 108, 101])
Out[30]:
In [31]:
[chr(x) for x in [80, 121, 67, 105, 114, 99, 108, 101]]
Out[31]:
In [32]:
filter(lambda x: x > 0, [-1, 0, 4, -3, 2])
Out[32]:
In [33]:
[x for x in [-1, 0, 4, -3, 2] if x > 0]
Out[33]:
In [34]:
reduce(lambda a, b: a - b, [2, 3, 4])
Out[34]:
In [35]:
2 - 3 - 4
Out[35]:
Więcej informacji temat funkcji wbudowanych na https://docs.python.org/2/library/functions.html
1 . Napisz kod tworzący listę z przedziału $[0, 100]$ liczb podzielnych przez 3 ale nie podzielnych przez 9
In [ ]:
2 . Napisz kod który zwraca unikalne elementy z podanej listy
In [ ]:
3 . Napisz kod który znajdzie maksimum wartości słownika
In [ ]:
In [39]:
%ls -l
In [40]:
fp = open("pycircle.txt", "w")
In [41]:
%ls -l
In [42]:
fp.write("Hello world\n")
fp.close()
In [43]:
%cat pycircle.txt
In [44]:
with open("pycircle.txt") as fp:
print fp.read(),
In [45]:
def fun1(a):
a.append(9)
return a
def fun2(a=[]):
a.append(9)
return a
In [46]:
lista1 = [1, 2, 3]
lista2 = [3, 4, 5]
fun1(lista1), fun2(lista2)
Out[46]:
In [47]:
def fun2(a=[]):
a.append(9)
return a
fun2()
Out[47]:
In [48]:
fun2()
Out[48]:
In [49]:
fun2()
Out[49]:
In [50]:
def show_local():
x = 23
print("Local: %s" % x)
show_local()
In [51]:
def show_enclosing(a):
def enclosing():
print("Enclosing: %s" % a)
enclosing()
show_enclosing(5)
In [52]:
x = 43
def show_global():
print("Global %s" % x)
show_global()
In [53]:
def show_built():
print("Built-in: %s" % abs)
show_built()
In [54]:
x = 43
def what_x():
print(x)
x = 4
In [55]:
what_x()
In [56]:
x = 43
def encl_x():
x = 23
def enclosing():
print("Enclosing: %s" % x)
enclosing()
In [57]:
encl_x()
In [59]:
x = 43
def what_about_globals():
global x
x = 37
print("In function %s" % x)
In [60]:
what_about_globals()
print("After function %s" % x)
In [61]:
def f(x):
f.l += x
print "x: ", x
print "f.l: ", f.l
f.l = 10
In [62]:
f(2)
f(14)
In [63]:
def powerer(power):
def nested(number):
return number ** power
return nested
In [64]:
f = powerer(3)
f(2), f(10)
Out[64]:
In [65]:
def licznik(start):
def nested(label):
print(label, nested.state)
nested.state += 1
nested.state = start
return nested
In [66]:
f = licznik(0)
f('a')
f('b')
f('c')
1 . Napisz funkcję która stworzy plik z pierwiastkami liczb z zakresu $[0, 100]$ (całkowite), każdy w osobnej linii
In [ ]:
2 . Napisz funkcję wczytująca pierwiastki z pliku z poprzedniego zadania, oblicz ich sumę i dopisz do pliku
In [ ]:
3 . Napisz funkcję która będzie działała jak ''.join()
za pomocą reduce
In [1]:
' '.join(['a', 'b', 'c'])
Out[1]:
In [6]:
def my_join(joining_str, list_of_str):
return reduce
In [7]:
my_join(" ", ['a', 'b', 'c'])
Out[7]:
In [8]:
' '.join(['a', 'b', 'c'])
Out[8]:
In [ ]: