hudbiu 1 - university of the ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6...

25
_hUdbiU 1 (*5) fk_)(for)9range()5Ee[\GNPYkVi[ 7.( 1. gak\H!? 2. Chapter 3.2 For Loops 1. M=CHfk_) 2. YkVi[7EXk] 3. Chapter 3.4 A Few Words About Using Floats 1. %'HO< 4. If9fk_H04 1. elif, continue, break 5. &- 1. &-14: LDH`Tj_hUdbiU 2. &-5: DWkc1 (^i\?) S/ ADKN= 6. 8 2018n_hUdbiU1 1 3,`kZ: http://ie.u-ryukyu.ac.jp/~tnal/2018/prog1/ YkVi[7le[\9mG BP)S!@PN=GFR=: S=6GIL 2E";JQG$: +>F#S )1AN= -

Upload: others

Post on 11-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

_hUdbiU1(*5�)fk_�)(for�)9range()5�Ee[\GNPYkVi[

7�.(1. gak\H!? 2. Chapter 3.2 For Loops

1. M=�CHfk_�)2. YkVi[7�EXk]�

3. Chapter 3.4 A Few Words About Using Floats1. %���'�H O�<

4. If�9fk_�H041. elif, continue, break

5. &-1. &-1�4: LDH`Tj_hUdbiU2. &-5:��DWkc1 (��^i\�?) S�/ADKN=

6. �8

2018��n_hUdbiU1 1

3,`kZ: http://ie.u-ryukyu.ac.jp/~tnal/2018/prog1/

YkVi[7�le[\9���mG�BP���)S!@PN=GFR=:

��S�=6GI�L2�E";JQG$�:

+>F� #S)1AN=

�-

Page 2: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

-5#3/6#1(�6�)),'#�����&$7-����2+'*(&*�0%174

1. Chapter 1 Turing Complete

2. Chapter 4.1.2 Keyword Arguments and Default Values

3. Chapter 4.1.3 Scoping

4. Chapter 4.2 Specifications

1. docstring + doctest (�����!��")

5. Chapter 4.5 Modules

6. �8� ����9

7. ��

2018��:-5#3/6#1 2

��.7%: http://ie.u-ryukyu.ac.jp/~tnal/2018/prog1/

Page 3: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

Turing complete (ENTPRA��)W���1�74��)

• Universal Turing Machine (�!ENTPRAICR)– 0/1@#%61>�(&9FTHULMPV0.=*FTH�9��7$:�13>��@�5+

– Church-Turing Thesis (ENTPRA9��)• ,;2-.>'�0�( 9��6"�!8<*ENTPRAICR6HQAOK!6.=*4?@"�61>+

2018��XHQAOJRA1 3

BRGNTD9��

:ENTPRASICR7BRGNTD��:http://www.slideshare.net/junpeitsuji/ss-57954980

Page 4: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

AK3HDL31(�6�)<?:3�����'74MA����G>:=;7=�E6FMJ

1. Chapter 1 Turing Complete

2. Chapter 4.1.2 Keyword Arguments and Default Values

3. Chapter 4.1.3 Scoping

4. Chapter 4.2 Specifications

1. docstring + doctest (���)�0,#2)

5. Chapter 4.5 Modules

6. ��N/! �("O

7. ��

2018�PAK3HDL31 4

��BM6: http://ie.u-ryukyu.ac.jp/~tnal/2018/prog1/

4L@FM8*��&�1��9FMIL3C5L)$�%�+%-. �

Page 5: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

Chapter 4, 4.1.2, 4.1.3, 4.2, 4.5���

4 FUNCTIONS, SCOPING, AND ABSTRACTION4.1.2 Keyword Arguments and Default Values4.1.3 Scoping4.2 Specifications4.5 Modules

2018��������1 5

Page 6: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

4.1.2 Keywords Arguments and Default Values

# ����1def myrange(start, stop, step=1):

result = []num = startwhile num<stop:

result.append(num)num += step

return result

class range(object)| range(stop) -> range object| range(start, stop[, step]) -> range object| | Return an object that produces a sequence of integers from start (inclusive)| to stop (exclusive) by step.

2018����������1 6

��������� �

# ���1>>> myrange(0, 5)[0, 1, 2, 3, 4]>>> myrange(start=0, stop=5)[0, 1, 2, 3, 4]>>> myrange(0, 5, step=2)[0, 2, 4]

Page 7: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

9A.?;B.1(�6�)472.�����"1/C9����>625315�<0=C@

1. Chapter 1 Turing Complete

2. Chapter 4.1.2 Keyword Arguments and Default Values

3. Chapter 4.1.3 Scoping

4. Chapter 4.2 Specifications

1. docstring + doctest (���$�)' ,)

5. Chapter 4.5 Modules

6. ��E(� �#�F

7. ��

2018��G9A.?;B.1 7

��:C0: http://ie.u-ryukyu.ac.jp/~tnal/2018/prog1/

��%�$&48-@5�+��!�*D

Page 8: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

Stack frame � Name Spacedef f(x):

y = 1x = x + yprint('f(x): x = {0}'.format(x))print('f(x): y = {0}'.format(y))return(x)

x = 3y = 2z = f(x)print('x = {0}'.format(x))print('y = {0}'.format(y))print('z = {0}'.format(z))

def g():print('z = {0}'.format(z))g()

2018��1*-',+.'1 8

(1) ��f()!�

0(/)�� ���"�� � �&%�����"���!$�

Stack frame: 1f()

(2) (/)!�Stack frame: 1

f(), x=3, y=2

(3) ���(���#��)Stack frame: 1

f(), x=3, y=2, z(3-1) ���#����

Stack frame: 2y=1, x=4(3-2) ���,return��

Stack frame: 1f(), x=3, y=2, z=4(3-3) �����!��

Page 9: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

$:;<8%� =Last-in, First-out (LIFO)

2018��D>A9@?B91 9

Stack frame: 1

f(), x=3, y=2, z

(3-1) !�2�+��

��

:;<83��(push)

Stack frame: 1

f(), x=3, y=2, z

��

:;<83��(push)

Stack frame: 1

f(), x=3, y=2, z=4

:;<8'4��(pop)

Stack frame: 2

y=1, x=4

(3-2) !� �,return�� (3-3) !� ���1��

Stack frame: 2

y=1, x=4

��

��: ��/=C;� ,

https://ja.wikipedia.org/wiki/=C;�

!�72�,.20�+&Stack frame(�46"-)0����(��*65#

Page 10: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

Stack FrameHaUk\• \[_i`hnUkZ_gZ,��3N?;IYVg_\]TUh�)�H�H^j[VoDI3�CH0��l���S\imY?3�HYZ[V]imcD$�<>RQ4

• 0�:�J�>RQE3�?8YZ[V]imc:�ORQ4

– ==DH�#I3YXm_�G7QYZ[V]imcGI�1S K>F84n��7Po

– 0�:&�@QE3YZ[V]imcI��(pop)>RQ4• ��?F8EdegG�P'QAM3 24

– "YZ[V]imcG+/>RC8F8�:�!>RQE35�J�?�HYZ[V]imc6S�%@Q4=H.P�%S\[_i`hLD(P-?CN*B9OF8��3NameErrorEFQ4

2018��p_jWfbkW1 10

Page 11: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

KOD��0� (2.4�0 ��)http://ie.u-ryukyu.ac.jp/~tnal/2017/prog1/loop.py

# スライムのHPが0より大きい間タコ殴りにするゲーム

import random

def encount_enemy():hitpoint = random.randint(3, 7)return hitpoint

enemy_hitpoint = encount_enemy()print('スライムに遭遇した。(敵HP=', enemy_hitpoint, ')')

while (enemy_hitpoint > 0):attack = random.randint(2,4)enemy_hitpoint = enemy_hitpoint – attackprint('あなたの攻撃!スライムに', attack, 'のダメージ!(敵

HP=', enemy_hitpoint, ')')

print('スライムを倒した!')

2018��PDM;JFN;1 11

4��0while�<OB�(1) ��encount_enemy()-��(,6!random"1�&0���-1�'7,#.#

(2) ����0>?@:CLOG/��(.#*3�A@DLEK0>?@:CLOG8��)6 (3) A@DLEK0>?@:CLOG/1�!import random"-�2�9+randomH=IOK%��'7,$5�&78��encount_enemy()-4��)6

Page 12: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

BL6IDM61(�6�)=@;6�#!�*97NB#��#H?;><9>#F8GNJ

1. Chapter 1 Turing Complete

2. Chapter 4.1.2 Keyword Arguments and Default Values

3. Chapter 4.1.3 Scoping

4. Chapter 4.2 Specifications

1. docstring + doctest (���,%2/)4)

5. Chapter 4.5 Modules

6. ��O 1'� +(P

7. "

2018��QBL6IDM61 12

��CN8: http://ie.u-ryukyu.ac.jp/~tnal/2018/prog1/

!�3�.�(*9:;5AKNE-��#��� -�2��3��(0&$

Page 13: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

4.2 Specifications (��, ;0B@G:)

• 3@G:�– �#��+�(�

• docstring�'*,3@G:– �"""�"""�$�"!����'�,3@G:�

–/G9G:$=F61- �,�%�–��;0B@G:% #(��

% curl https://ie.u-ryukyu.ac.jp/~tnal/2018/prog1/week6_doctest.py -O

% pydoc -w week6_doctest

% open week6_doctest.html

2018��M>F2D?G21 13

pydoc:;0B@G8I4CGH7IE

L�)A5BIE�J<./E�$)&�K

��: Example Google Style Python Docstrings

http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html

Page 14: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

doctest<ADUPLNMKN(���<+C?6G)

• UPLNMKN (Unit Test, �MKN)– &�/��$C<��5D.F�"(– print��48�!�#9>;1')��47��F�-7B'��47��/�BED2:Z��or�[*F�#(

• doctest<ADUPLNMKN– � 90DOHTSYNZdocstring[– �

• $ =KIWQN� % python week6_doctest.py

• UPLNMKN=� % python -m doctest week6_doctest.py -v

2018��\QXJVRYJ1 14

$ � 9>UPLNMKN>� 3E;,(Z%��<�#5D@=[

Page 15: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

doctest���oct:tnal% python -m doctest week6_doctest.py -vTrying:

add(1, 2)Expecting:

3okTrying:

add(-1, 3)Expecting:

2okTrying:

add(0, 0.5)Expecting:

0.5ok(���)

(���)2 items passed all tests:

1 tests in __main__2 tests in __main__.add

3 tests in 2 items.3 passed and 0 failed.Test passed.

2018��-$(�'%)�1 15

2����!&������! "�# ��+������,*1��! "�__main__���� *2��! "�__main__.add����

! "1

! "2

! "3

Page 16: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

doctest%���

• �+=27;2!%��"�����(#�"���)�"��� �*#��

– �1�"�1 �&��– [1, 2]"[1,2]&��• doctest&18>1%��'��$3,4.�• str�-60,.5"� %��

2018� ?7</:9=/1 16

Page 17: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

;E/C=F/1(�6�) 6:4/����$30G;�� �A947537�?2@GD

1. Chapter 1 Turing Complete

2. Chapter 4.1.2 Keyword Arguments and Default Values

3. Chapter 4.1.3 Scoping

4. Chapter 4.2 Specifications

1. docstring + doctest (��&�*'"-)

5. Chapter 4.5 Modules

6. ��H�) ��%!I

7. ��

2018��J;E/C=F/1 17

��<G2: http://ie.u-ryukyu.ac.jp/~tnal/2018/prog1/

8.@>F5G1BF$A947537,��#��&�+(��

Page 18: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

4.5 Modules (?8@EB, � )

• 7E:1��#%;34B = ?8@EB

• ���1% python>>> import week6_doctest>>> week6_doctest.add(1, 2)3

• ���2% python>>> import week6_doctest as wd>>> wd.add(1, 2)3

• ���3% python>>> from week6_doctest import *>>> add(1, 2)3

• �-�2&?8@EB,help()(:5@>D91�(!0�>>> import week6_doctest as wd>>> help(wd)

2018��F<C6A=D61 18

��?8@EB1import(�-�.

?8@EB�*��1�"0

?8@EB�+��1��#'�-�.��* ,�'+� �

pydoc(�/0:5@>D9)���,�$�

�?8@EB�.��� (?8@EB�+� 1��

Page 19: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

AI7GCJ71(�6�) =@;7��#�+:8KA#� #F?;><:>#D9EKH

1. Chapter 1 Turing Complete

2. Chapter 4.1.2 Keyword Arguments and Default Values

3. Chapter 4.1.3 Scoping

4. Chapter 4.2 Specifications1. docstring + doctest (��-%2.*6)

5. Chapter 4.5 Modules

6. ��L�1(��,)M

7. ��

2018��NAI7GCJ71 19

��BK9: http://ie.u-ryukyu.ac.jp/~tnal/2018/prog1/

�!������ �!"-03D9EKH�/�/5�'30&-,4&$

Page 20: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

Reserved words, ��https://goo.gl/rEzdAN

• ��������� ����

2018����������1 20

Page 21: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

E?G

• UVWP\gj_?URj]A <5���*B�– ��.=JURj]>�7KJUVWP\gj_C@K2m

– ��• +�L�D�9?�85UVWP\gj_3(�7KJ-• +�B��L 0J?,UVWP\gj_C��7KJ-• ���*C�UVWP\gj_2I);=��9J-

• from, import AHJaTbjf%F&F• docstringAHJZOb`iXjSdi– !�A#'>4J3,>4J:6NMZA�1/-

• doctestAHJc[WYXUY– ���$k�">4JZOb`iY-

2018��l]hQe^iQ1 21

Page 22: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

n|^xq}^1("6�)gke^�&<1�Jb_~n<��<vjehfbh<tau~z

1. Chapter 1 Turing Complete

2. Chapter 4.1.2 Keyword Arguments and Default Values

3. Chapter 4.1.3 Scoping

4. Chapter 4.2 Specifications

1. docstring + doctest (� �L>UQEZ)

5. Chapter 4.5 Modules

6. �$��TC�0KD�

7. �2

2018���n|^xq}^1 22

+#o~a: http://ie.u-ryukyu.ac.jp/~tnal/2018/prog1/

5:87436798:;LSVtau~z)R,RY�AVS@LKX@=

i\us}f~`w}JvjehfbhY�?F/�L�WS@=

1�Y�O�DJbce]m{~rM-�<�!0M.U�Y�'DS@=

1�M��LNgm[zh�Y(�IBV�

_}lu~cM� I>V�%du~y}^p`}LG?H*PHRS@=

Page 23: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

��

��1�4: �& "1+705,426,

��5:� -831 (�/6.��) *�� %'�

2018��;05,426,1 23

��5$!�)��1+#������ �(�1+"��*�& %'��9�2"observer�'�:

Page 24: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

��• �: ��(�%!� ��)

–72;/��2 �72;/ ����& �#��• ��: ����"– 4�• 4.3 Recursion• 4.4 Global Variables

• �:��((,,4): –08+539+,)6-'.* <716�� �$���

• https://paiza.jp/challenges/info

2018�=08+539+1 24

Page 25: hUdbiU 1 - University of the Ryukyustnal/2018/prog1/2018prog1-w6.pdf · 2018-05-24 · -5#3/6#1 ( 6 )),'# &$7- 2+'*(&* 0%174 1.Chapter 1 Turing Complete 2.Chapter 4.1.2 Keyword Arguments

���

• ��: Introduction to Computation and Programming Using Python: With Application to Understanding Data

• Python 3.5.1 documentation, https://docs.python.org/3.5/index.html

• ����� �������������, http://www.slideshare.net/junpeitsuji/ss-57954980

• doctest — Test interactive Python examples, https://docs.python.org/3/library/doctest.html

• Example Google Style Python Docstrings, http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html

2018����� ��� 1 25