+ All Categories
Home > Documents > Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019...

Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019...

Date post: 09-Jul-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
36
Python, základní kameny až skály I Tomáš Svoboda B4B33RPH, 2019-10-08
Transcript
Page 1: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM ...

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 ...
Page 3: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM ...
Page 4: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

decimal vs binary

decimal-vs-binary-integers.png

Page 23: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM ...

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 ...

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 ...

• 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 27: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM ...

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 ...

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 ...

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 ...

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 ...

ale pozor …

Page 35: Python, základní kameny až skály I...lecture03-python-stones-animace Created Date: 10/8/2019 7:00:17 AM ...

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 ...

běžte a programujte!

• http://pythontutor.com/visualize.html#mode=edit

• http://openbookproject.net/thinkcs/python/english3e/index.html


Recommended