Kurzy programovacího jazyka Python
# Třídní atributy jsou de facto uloženy ve slovníku
# Ve skutečnosti B a C nemají vlastní hodnoty atributů
# pouze reference do A
class A(object):
attribute = "foo"
class B(A):
pass
class C(A):
pass
print(A.attribute, B.attribute, C.attribute)
A.attribute = "baz"
print(A.attribute, B.attribute, C.attribute)
# výchozí hodnota funkce je vyhodnocena pouze jedenkrát
# a to ve chvíli definici funkce
def foo(bar=[]):
bar.append("baz")
return bar
print(foo())
print(foo())
print(foo())
# výchozí hodnota funkce je vyhodnocena pouze jedenkrát
# a to ve chvíli definici funkce
def foo(bar=None):
if bar is None:
bar = []
bar.append("baz")
return bar
print(foo())
print(foo())
print(foo())
x = 1
def foo():
x += 1
print(x)
foo()
x = 1
def foo():
global x
x += 1
print(x)
foo()
seznam = []
def foo(x):
seznam.append(x)
print(seznam)
foo(1)
print(seznam)
foo(2)
print(seznam)
def bar(x):
seznam += [x]
print(seznam)
bar(3)
print(seznam)
bar(4)
print(seznam)
seznam = []
def foo(x):
seznam.append(x)
print(seznam)
foo(1)
print(seznam)
foo(2)
print(seznam)
def bar(x):
global seznam
seznam += [x]
print(seznam)
bar(3)
print(seznam)
bar(4)
print(seznam)
y = 10
def adder(x):
return y + x
print(adder(1))
y = 20
print(adder(1))
# Python 2 vs Python 3
x = 0.2
x += 0.1
print(x)
print(repr(x))
print(str(x))
Bližší informace o formátu float/double: https://www.h-schmidt.net/FloatConverter/IEEE754.html
numbers = [n for n in range(10)]
print(numbers)
for i in range(len(numbers)):
print(i)
if numbers[i] % 2 == 0:
del numbers[i]
print(numbers)