PyCircle

Wprowadzenie

Hello world


In [2]:
print "Hello world"


Hello world

Boole


In [10]:
True


Out[10]:
True

In [11]:
False


Out[11]:
False

In [1]:
foo = True

In [2]:
foo


Out[2]:
True

Liczby


In [2]:
2+2


Out[2]:
4

In [3]:
2*2


Out[3]:
4

Stringi


In [4]:
"ala ma kota"


Out[4]:
'ala ma kota'

In [5]:
"ala ma kota"[4:6]


Out[5]:
'ma'

In [6]:
"ala ma kota"[4:]


Out[6]:
'ma kota'

In [7]:
"ala ma kota"[:6]


Out[7]:
'ala ma'

In [8]:
"ala ma kota"[::2]


Out[8]:
'aam oa'

In [9]:
"ala ma kota"[::-1]


Out[9]:
'atok am ala'

In [32]:
"foo bar".split()


Out[32]:
['foo', 'bar']

In [33]:
"foo, bar".split(", ")


Out[33]:
['foo', 'bar']

In [36]:
"foo".upper()


Out[36]:
'FOO'

In [37]:
"FOO".lower()


Out[37]:
'foo'

In [38]:
"foo".capitalize()


Out[38]:
'Foo'

Listy


In [12]:
a = [1, 5, 'foo', 7]

In [13]:
a


Out[13]:
[1, 5, 'foo', 7]

In [15]:
a.append(4)

In [16]:
a


Out[16]:
[1, 5, 'foo', 7, 4]

In [17]:
a.extend([51, 67])

In [18]:
a


Out[18]:
[1, 5, 'foo', 7, 4, 51, 67]

Slowniki


In [44]:
foo = {'foo': 'bar', 'kopytko': 'no elo'}

In [45]:
foo


Out[45]:
{'foo': 'bar', 'kopytko': 'no elo'}

In [46]:
foo['srogie'] = 'grzyby'

In [47]:
foo


Out[47]:
{'foo': 'bar', 'kopytko': 'no elo', 'srogie': 'grzyby'}

In [48]:
foo.update({'aaa': 'bbb', 'nieee': 'wspaniale'})

In [49]:
foo


Out[49]:
{'aaa': 'bbb',
 'foo': 'bar',
 'kopytko': 'no elo',
 'nieee': 'wspaniale',
 'srogie': 'grzyby'}

Ewaluacja do boola


In [14]:
bool(1)


Out[14]:
True

In [15]:
bool(0)


Out[15]:
False

In [10]:
bool('foo')


Out[10]:
True

In [11]:
bool('')


Out[11]:
False

In [25]:
bool([2])


Out[25]:
True

In [20]:
bool([])


Out[20]:
False

In [21]:
bool({'foo': 'bar'})


Out[21]:
True

In [22]:
bool({})


Out[22]:
False

Konwersja innych typow


In [54]:
str(4)


Out[54]:
'4'

In [55]:
int('5')


Out[55]:
5

In [56]:
int('no witam')


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-56-034fb3425232> in <module>()
----> 1 int('no witam')

ValueError: invalid literal for int() with base 10: 'no witam'

Instrukcje if


In [12]:
if True:
    print "To sie wyswietli"
else:
    print "A to nie"


To sie wyswietli

In [13]:
a = 5
if a == 2:
    print "dwojka"
elif a == 5:
    print "piatka"
else:
    print "jakas liczba"


piatka

Petla for


In [23]:
for i in xrange(10):
    print i


0
1
2
3
4
5
6
7
8
9

In [24]:
for element in [5, 2, 8]:
    print element


5
2
8

In [27]:
for key, value in {'foo': 'bar', 'kopytko': 'no elo'}.iteritems():
    print key
    print value
    print


kopytko
no elo

foo
bar

List comprehension


In [28]:
[i for i in xrange(10)]


Out[28]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [30]:
["%s -> %s" % (k, v) for k, v in {'foo': 'bar', 'kopytko': 'no elo'}.iteritems()]


Out[30]:
['kopytko -> no elo', 'foo -> bar']

Dict comprehension


In [31]:
{i: i**2 for i in xrange(10)}


Out[31]:
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

Funkcje


In [34]:
def foo():
    return 'bar'

In [35]:
print foo()


bar

In [41]:
def foo(label):
    return label.capitalize()

In [42]:
foo('foo')


Out[42]:
'Foo'

In [52]:
def foo(*args, **kwargs):
    result = {element: element.capitalize() for element in args}
    result.update(kwargs)
    return result

In [53]:
foo('tak', 'nie', 'nie wiem', przypadek='nie sadze')


Out[53]:
{'nie': 'Nie', 'nie wiem': 'Nie wiem', 'przypadek': 'nie sadze', 'tak': 'Tak'}

Generatory


In [57]:
def powers(limit):
    for i in xrange(limit):
        yield i**2

In [58]:
my_powers = powers(3)

In [59]:
next(my_powers)


Out[59]:
0

In [60]:
next(my_powers)


Out[60]:
1

In [61]:
next(my_powers)


Out[61]:
4

In [62]:
next(my_powers)


---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
<ipython-input-62-3e3c7e456c4e> in <module>()
----> 1 next(my_powers)

StopIteration: 

In [63]:
for i in powers(3):
    print i


0
1
4

In [64]:
list(powers(3))


Out[64]:
[0, 1, 4]

That's all, folks