python, základní kameny až skály i...lecture03-python-stones-animace created date: 10/8/2019...
TRANSCRIPT
![Page 1: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/1.jpg)
Python, základní kameny až skály ITomáš Svoboda
B4B33RPH, 2019-10-08
![Page 2: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/2.jpg)
![Page 3: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/3.jpg)
![Page 4: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/4.jpg)
https://gitlab.fel.cvut.cz/RPH-student-materials http://cw.fel.cvut.cz/wiki/courses/b4b33rph/prednasky/start
https://www.root.cz/knihy/pro-git/
![Page 5: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/5.jpg)
platnost proměnných
1 W = 'to co je zde, je viditelne vsude' 2 a = 'definovano v hlavnim programu' 3 4 def my_function(x): 5 a = 'definovano uvnitr funkce' 6 print('W je znamo: ',W) 7 return x + ' ' + a 8 9 # toto se vykona vzdy (import nebo beho programu) 10 print(a) 11 12 if __name__ == "__main__": 13 # toto se pri importu nevykona 14 a = 'neco jineho' 15 b = my_function(a)
visualizacevariables-scope.py
![Page 6: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/6.jpg)
platnost proměnných
1 W = 'to co je zde, je viditelne vsude' 2 a = 'definovano v hlavnim programu' 3 4 def my_function(x): 5 a = 'definovano uvnitr funkce' 6 print('W je znamo: ',W) 7 return x + ' ' + a 8 9 # toto se vykona vzdy (import nebo beho programu) 10 print(a) 11 12 if __name__ == "__main__": 13 # toto se pri importu nevykona 14 a = 'neco jineho' 15 b = my_function(a)
visualizacevariables-scope.py
![Page 7: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/7.jpg)
platnost proměnných
1 W = 'to co je zde, je viditelne vsude' 2 a = 'definovano v hlavnim programu' 3 4 def my_function(x): 5 a = 'definovano uvnitr funkce' 6 print('W je znamo: ',W) 7 return x + ' ' + a 8 9 # toto se vykona vzdy (import nebo beho programu) 10 print(a) 11 12 if __name__ == "__main__": 13 # toto se pri importu nevykona 14 a = 'neco jineho' 15 b = my_function(a)
visualizacevariables-scope.py
![Page 8: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/8.jpg)
platnost proměnných
1 W = 'to co je zde, je viditelne vsude' 2 a = 'definovano v hlavnim programu' 3 4 def my_function(x): 5 a = 'definovano uvnitr funkce' 6 print('W je znamo: ',W) 7 return x + ' ' + a 8 9 # toto se vykona vzdy (import nebo beho programu) 10 print(a) 11 12 if __name__ == "__main__": 13 # toto se pri importu nevykona 14 a = 'neco jineho' 15 b = my_function(a)
visualizacevariables-scope.py
![Page 9: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/9.jpg)
platnost proměnných
1 W = 'to co je zde, je viditelne vsude' 2 a = 'definovano v hlavnim programu' 3 4 def my_function(x): 5 a = 'definovano uvnitr funkce' 6 print('W je znamo: ',W) 7 return x + ' ' + a 8 9 # toto se vykona vzdy (import nebo beho programu) 10 print(a) 11 12 if __name__ == "__main__": 13 # toto se pri importu nevykona 14 a = 'neco jineho' 15 b = my_function(a)
visualizacevariables-scope.py
![Page 10: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/10.jpg)
program structure - basic blocks 1 import math 2 3 class MyClass: 4 '''class for ''' 5 def __init__(self): 6 '''MyClass constructor''' 7 pass # nothing at the moment 8 9 def my_function(a,b): 10 '''compute sum a+b''' 11 pass # nothing at the moment 12 13 if __name__ == "__main__": 14 # actual program starts here 15 c = MyClass() # don't forget the parantheses! I will show! 16
function-vs-method.py
![Page 11: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/11.jpg)
program structure - basic blocks 1 import math 2 3 class MyClass: 4 '''class for ''' 5 def __init__(self): 6 '''MyClass constructor''' 7 pass # nothing at the moment 8 9 def my_function(a,b): 10 '''compute sum a+b''' 11 pass # nothing at the moment 12 13 if __name__ == "__main__": 14 # actual program starts here 15 c = MyClass() # don't forget the parantheses! I will show! 16
imports
function-vs-method.py
![Page 12: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/12.jpg)
program structure - basic blocks 1 import math 2 3 class MyClass: 4 '''class for ''' 5 def __init__(self): 6 '''MyClass constructor''' 7 pass # nothing at the moment 8 9 def my_function(a,b): 10 '''compute sum a+b''' 11 pass # nothing at the moment 12 13 if __name__ == "__main__": 14 # actual program starts here 15 c = MyClass() # don't forget the parantheses! I will show! 16
imports
Definitions:classesfunctions
function-vs-method.py
![Page 13: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/13.jpg)
program structure - basic blocks 1 import math 2 3 class MyClass: 4 '''class for ''' 5 def __init__(self): 6 '''MyClass constructor''' 7 pass # nothing at the moment 8 9 def my_function(a,b): 10 '''compute sum a+b''' 11 pass # nothing at the moment 12 13 if __name__ == "__main__": 14 # actual program starts here 15 c = MyClass() # don't forget the parantheses! I will show! 16
imports
Definitions:classesfunctions
main program
function-vs-method.py
![Page 14: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/14.jpg)
program structure - basic blocks 1 import math 2 3 class MyClass: 4 '''class for ''' 5 def __init__(self): 6 '''MyClass constructor''' 7 pass # nothing at the moment 8 9 def my_function(a,b): 10 '''compute sum a+b''' 11 pass # nothing at the moment 12 13 if __name__ == "__main__": 14 # actual program starts here 15 c = MyClass() # don't forget the parantheses! I will show! 16
V krátkých ukázkách budeme někdy ukazovat jen kód bez hlaviček a spol.
imports
Definitions:classesfunctions
main program
function-vs-method.py
![Page 15: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/15.jpg)
funkce vs. metoda 1 import math 2 3 class MyClass: 4 '''class for ''' 5 def __init__(self): 6 '''MyClass constructor''' 7 pass # nothing at the moment 8 def my_class_method(self): 9 print('nothing to report') 10 11 12 def my_function(a,b): 13 '''compute sum a+b''' 14 pass # nothing at the moment 15 16 if __name__ == "__main__": 17 # actual program starts here 18 c = MyClass() # don't forget the parantheses! I will show! 19 c.my_class_method() 20
![Page 16: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/16.jpg)
funkce vs. metoda 1 import math 2 3 class MyClass: 4 '''class for ''' 5 def __init__(self): 6 '''MyClass constructor''' 7 pass # nothing at the moment 8 def my_class_method(self): 9 print('nothing to report') 10 11 12 def my_function(a,b): 13 '''compute sum a+b''' 14 pass # nothing at the moment 15 16 if __name__ == "__main__": 17 # actual program starts here 18 c = MyClass() # don't forget the parantheses! I will show! 19 c.my_class_method() 20
![Page 17: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/17.jpg)
funkce vs. metoda 1 import math 2 3 class MyClass: 4 '''class for ''' 5 def __init__(self): 6 '''MyClass constructor''' 7 pass # nothing at the moment 8 def my_class_method(self): 9 print('nothing to report') 10 11 12 def my_function(a,b): 13 '''compute sum a+b''' 14 pass # nothing at the moment 15 16 if __name__ == "__main__": 17 # actual program starts here 18 c = MyClass() # don't forget the parantheses! I will show! 19 c.my_class_method() 20
![Page 18: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/18.jpg)
není číslo jako číslo 1 a = 0.1 2 b = 0.3 3 c = 3*a 4 if (b==c): 5 print(b,'and',c,'are equal') 6 else: 7 print(b,'and',c,'are NOT equal')
floating-point-surprise.py
![Page 19: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/19.jpg)
není číslo jako číslo 1 a = 0.1 2 b = 0.3 3 c = 3*a 4 if (b==c): 5 print(b,'and',c,'are equal') 6 else: 7 print(b,'and',c,'are NOT equal')
• vizualizace • https://docs.python.org/3/tutorial/floatingpoint.html • http://floating-point-gui.de/formats/binary/ • opatrnost při testování rovnosti (float) čísel • pokud opravdu potřeba: abs(a-b) < threshold
floating-point-surprise.py
![Page 20: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/20.jpg)
decimal vs binary
decimal-vs-binary-integers.png
![Page 21: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/21.jpg)
decimal vs binary
decimal-vs-binary-integers.png
![Page 22: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/22.jpg)
řetězce neboli stringy 1 a = 'ahoj' 2 b = 'svete' 3 c = a+b 4 for i,item in enumerate(c): 5 print(i,'-',item) 6 banner = ['ahoj','svete'] 7 for i,item in enumerate(banner): 8 print(i,'-',item) 9 for i,item in enumerate(banner): 10 for j,elem in enumerate(item): 11 print(i,'-',item,'***',j,':',elem)
visualizace
strings.py
![Page 23: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/23.jpg)
python indexing, slicing, …
![Page 24: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/24.jpg)
dokončeme teď R-P-S lepší hráč, s pamětí
![Page 25: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/25.jpg)
• player_skewed některé tahy preferuje na úkor jiných
• nevíme které to jsou
• nejprve hrajme náhodně a uchovávejme soupeřovy tahy
• analyzujme historii tahů
• hrajme optimálně (využijme nedokonalost protihráče)
![Page 26: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/26.jpg)
pointers
visualisation
is vs ==
is-vs-equal.py
![Page 27: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/27.jpg)
making copy, a[:], rychle, ale …
![Page 28: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/28.jpg)
making copy, a[:], rychle, ale …
![Page 29: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/29.jpg)
import copy and go deephttp://docs.python.org/3.4/library/copy.html
![Page 30: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/30.jpg)
pozor na mělkost kopií
![Page 33: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/33.jpg)
ale pozor …
![Page 34: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/34.jpg)
a ještě větší pozor na implicitní parametry
![Page 35: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/35.jpg)
implicitní parametry detailněji
![Page 36: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM](https://reader033.vdocuments.pub/reader033/viewer/2022042417/5f329c88b4e42254661434e1/html5/thumbnails/36.jpg)
běžte a programujte!
• http://pythontutor.com/visualize.html#mode=edit
• http://openbookproject.net/thinkcs/python/english3e/index.html