Úvod do programování v Pythonu
Python - datové typy pokračování
Anastázie a Filip Sedlákovi
Funkce a metody
Seznam (list)
>>> my_list = ['banana', 42, 'Nasťa', ['a', 'b']]
>>> type(my_list)
<class 'list'>
>>> nucl = 'ACGT'
>>> list(nucl)
['A', 'C', 'G', 'T']
Seznam (list)
>>> my_list[3] = 'apple'
>>> my_list
['banana', 42, 'Nasťa', 'apple']
>>> name = 'Nasta'
>>> name[0] = 'C'
Traceback (most recent call last):
File "<ipython-input-9-69728e5ccb1f>", line 1, in <module>
name[0] = 'C'
TypeError: 'str' object does not support item assignment
Operace se seznamy
>>> numbers = ['one', 'two', 'three', 'four']
>>> len(numbers)
4
>>> numbers[0::2]
['one', 'three']
>>> numbers[-1]
'four'
Operace se seznamy
>>> my_list + numbers
['banana', 42, 'Nasťa', 'apple', 'one', 'two', 'three', 'four']
>>> ['GCC']*3
['GCC', 'GCC', 'GCC']
>>> numbers.append('five')
>>> numbers
['one', 'two', 'three', 'four', 'five']
Operace se seznamy
>>> del numbers[1]
>>> numbers
['one', 'three', 'four', 'five']
>>> fruits = ['banana','apple', 'mango', 'kiwi']
>>> fruits.sort()
>>> print(fruits)
['apple', 'banana', 'kiwi', 'mango']
>>> dir(list)
[..., 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
Cvičení
Cvičení
Množina (set)
>>> basket = {'apple','orange','apple','pear','orange','banana'}
>>> basket
{'apple', 'banana', 'orange', 'pear'}
>>> type(basket)
<class 'set'>
>>> set('Nasťa')
{'N', 'ť', 'a', 's'}
Množina (set)
>>> basket = {'apple','orange','apple','pear','orange','banana'}
>>> basket
{'apple', 'banana', 'orange', 'pear'}
>>> basket[0]
Traceback (most recent call last):
File "<ipython-input-7-0ed6bd42dd11>", line 1, in <module>
basket[0]
TypeError: 'set' object does not support indexing
Množina (set)
>>> my_friends = set(['Lenka', 'Hana', 'Veronika'])
>>> your_friends = set(['Jana', 'Olga', 'Hana'])
>>> my_friends.intersection(your_friends)
{'Hana'}
>>> my_friends.difference(your_friends)
{'Lenka', 'Veronika'}
>>> your_friends.difference(my_friends)
{'Jana', 'Olga'}
Slovník (dictionary)
>>> info = {'name':'Nasťa', 'dog': 'Miky'}
>>> type(info)
dict
>>> info = {['name', 'first']:'Nasta'}
Traceback (most recent call last):
File "<ipython-input-16-7ab66cffb746>", line 1, in <module>
info = {['name', 'first']:'Nasta'}
TypeError: unhashable type: 'list'
Přístup k hodnotám ve slovníku
>>> info['name']
'Nasta'
>>> info.values()
dict_values(['Miky', 'Nasta'])
>>> info.keys()
dict_keys(['dog', 'name'])
>>> info = {'name': 'Nasta', 'dog': 'Miky', 'name': 'Julie', 'dog': 'Rex'}
>>> info['name']
'Julie'
Operace se slovníky
>>> info['color'] = 'blue'
>>> info
{'color': 'blue', 'dog': 'Miky', 'name': 'Nasta'}
>>> info.items()
dict_items([('color', 'blue'), ('dog', 'Miky'), ('name', 'Nasta')])
Operace se slovníky
>>> info.keys()
dict_keys({'color', 'dog', 'name'})
>>> info.values()
dict_values([ 'blue', 'Miky', 'Nasta'])
Operace se slovníky
>>>del info['name']
>>>info
{'color': 'blue', 'dog': 'Miky'}
>>> dir(dict)
[..., 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
Jak vytvořit...
Cvičení
alphabet = 'abcdefghijklmnopqrstuvwxyz'
zen = """
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""
Cvičení
Cykly - for
for i in iterovany_objekt:� telo bloku� telo bloku
Cykly - for
>>> word = 'python'
>>> for letter in word:
... print(letter)
p
y
t
h
o
n
Cykly - for
>>> fruits = ['jablka', 'hrušky', 'meruňky', 'broskve', 'pomeranče']
>>> for fruit in fruits:
... print('Mám ráda ' + fruit)
Mám ráda jablka
Mám ráda hrušky
Mám ráda meruňky
Mám ráda broskve
Mám ráda pomeranče
Cykly - for
>>> fruits = ['jablka', 'hrušky', 'meruňky', 'broskve', 'pomeranče']
>>> for i in range(len(fruits)):
... print('Mám ráda ' + fruits[i])
Mám ráda jablka
Mám ráda hrušky
Mám ráda meruňky
Mám ráda broskve
Mám ráda pomeranče
Iterace slovníkem
>>> kids = {'Sedlák': 'David', 'Iohanescu': 'Julie'}
>>> for key, value in kids.items():
... print(value, key)
David Sedlák
Julie Iohanescu
Odsazení (indentation)
>>> for key, value in kids.item():
... print(value, key)
File "<ipython-input-37-38507da79ee2>", line 2
print(value, key)
^
IndentationError: expected an indented block
Domací úkol