[`s^]as 1tnal/2018/prog1/2018prog1-w5.pdf · [`s^]as1 ()4 )8 i &2
TRANSCRIPT
[`S^]aS1()4�)8�I&2<_c[�%dwhile�e
1. Chapter 4.1.1I/321. 8�F`cQ_��
2. Chapter 3.1I/31. Iteration, looping (���%)2. _c[�%I�<�.�3. 3(:I�%#N�
3. $-4. �9
2018��f[`S^]aS1 1
2,\cU: http://ie.u-ryukyu.ac.jp/~tnal/2017/prog1/
8��I��F<8��I��JVTc[?'GM@FH"�=
+L5ACD>�%OZ`XRE��B<+L5B��O0�=
�%I#NJ6 b����b���%I;WP[IK=
��7�b dint,float,str,booleb$*(��b���b!4b1%ebY`c�dif, while, foreb8��,ddefe
�-
CL9HFM91(�5)JOC��(for�)'range()$�.I?@/46=O:M?
%��1. KEO@0�*�2. Chapter 3.2 For Loops
1. 3)�,0JOC��2. =O:M?%.<OA�
3. Chapter 3.4 A Few Words About Using Floats1. ������0�5�(
4. If�'JOC�0!#1. elif, continue, break
5. ��1. ��1�4: �2-0D8NCL9HFM92. ��5:��-;OG1 (��BM@�*) 7� +-14)
6. &
2018��PCL9HFM91 2
"�DO>: http://ie.u-ryukyu.ac.jp/~tnal/2018/prog1/
Chapter 3.2, 3.4���
3.2 For Loops3.4 A Few Words About Using Floats
2018��������1 3
3.2 For Loops (�)W23�9���
x = 4for i in range(0, x):
print(i)
2018��XNRDQOSD1 4
for�)in �*5� .>2FTESH&�U$�/2KTIV8�/4'(1)1303 �?=�/'(2)19 �?�"6/4MRJC?��U���V((3)� �8�/4(2)?��/�-2<for�?��(
FTESH&�9�: str, range, list
range ()%�range(stop): 0�stop-1:59�int�AMG@CL?��range(start, stop): start�stop-1
)range�AMG@CL*9�#?�!/2+7<'list�8BPHL/;,(
# �!�data = range(1, 4)print(list(data))
'2%1)����13str�",(!#*4
#&2+1# ��� 1�������for c in 'abc':
print(c)
#� 1abc
# srt�",(!#*
>>> len('abc')3>>> 'abc'[0]'a'>>> 'abc'[1]'b'>>> 'abc'[2]'c'>>> enemy = 'naltoma'>>> enemy[0]'n'
20185-0$/.1$1 5
'2%1)���[index]��� ����������
2.3 StringsY���:#&Z
2018��\RVHUTWH1 6
len(): ����)KXIWL:'2Y$ �Z
'���'[x:y] : slicing (LUFL��)���:x��0?y��=6D�@�4
�9��2B5A�>3��1!
'���'[0] : ���:0��[��4A(�D Index (FWONGL) 7�<*[Index;0��0?�/A*[���9;EGML��8-Yindex out of
rangeZ*[Index1SFQL��2BA7)�C0?�/A*
Tips: JSWP�"�:��D%>.+IndexError: string index out of range,
.�-"����2/list��'!��%0
# �.&2scores = [80, 100, 0]sum = Index = 0for score in scores:
sum += scoreprint('sum = {0}'.format(sum))index += 1
average = sum / len(scores)print('average = ' + str(average))
# �2sum = 80sum = 180sum = 180average = 60.0
2018�1(,�*)-�1 7
list (+"%)��'!��%-��������'!��%���- �+"%��2���- �+"%�[�-$#�"]�2�������'!��%�
- e.g., scores[0] = 80
MVCRPWC1(!5�)TYM� (for�),range())�6SIJ8=?GYDWI
*�$�1. UOYJ9�0�2. Chapter 3.2 For Loops
1. </�49TYM� 2. GYDWI*�6FYK�
3. Chapter 3.4 A Few Words About Using Floats1. ������9>�.
4. If�,TYM�9&(1. elif, continue, break
5. �#1. �#1�4: �;59NBXMVCRPWC2. �#5:��5EYQ1 ( �LWJ�0) A�%25:=/
6. �+
2018��\MVCRPWC1 8
'"NYH: http://ie.u-ryukyu.ac.jp/~tnal/2018/prog1/
GYDWI*�ZSIJ,���[8�3?�� A�1?=/87@/-
3.4 A Few Words About Using Floats(�� ���F�3+?%()
# HSK�3 (����F�:!,)x = 0.0for i in range(10):
x = x + 0.1
if x == 1.0:print('{0} = 1.0'.format(x))
else:print('{0} != 1.0'.format(x))
# �# �30.9999999999999999 != 1.0
2018��TMQGPNRG1 9
HRLOSJ>59E�T2)�
�B'�(rounded error):��@*�>$�<8E7- ��?1E�@�;:C$�<8=2.
�":/5�<&4EIT�?�0IQ6D�A2)� �4: http://goo.gl/xvrN6n����� ���: https://ja.wikipedia.org/wiki/����� ���
��% 3 �(�#% 3<002�#�= 5��*��+,�$�
# 1000�+� *0.15�$&.3>>> bignumber = 10**1000>>> bignumber += 0.1Traceback (most recent call last):File "<stdin>", line 1, in <module>
OverflowError: int too large to convert to float
# 100�+� *0.15�$&.3>>> bignumber = 10**100>>> bignumber += 0.1>>> >>> print(bignumber)1e+100>>> print('{0:f}'.format(bignumber))10000000000000000159028911097599180468360808563945281389781327557747838772170381060813469985856815104.000000
2018>7:698;61 10
- int)11000�'/��*��'�3�- float��$0�(%3(Overflow(≒��-4)�
� �� (exponential notation)1e+100 = 1 * 10**1000.0001 = 1 * 10**(-4) = 1e-04 "4,��'�
�!143�?
PYFUSZF1("5�)W\P�!(for�)/range()+�8VLM9@BJ\GZL
-�% 1. XR\M:�4�2. Chapter 3.2 For Loops
1. ?3�6:W\P�!2. J\GZL-�8I\N�
3. Chapter 3.4 A Few Words About Using Floats1. �����:A�2
4. If�/W\P�:'*1. elif, continue, break
5. �$1. �$1�4: �>7:QE[PYFUSZF2. �$5:��7H\T1 ( �OZM�4) D�&57=@3
6. �.
2018��]PYFUSZF1 11
)#Q\K: http://ie.u-ryukyu.ac.jp/~tnal/2018/prog1/
��D�3,9;�>(�8�1<C9��0
if��������
elif, continue, break
2018��� ����1 12
if���6elif7# ���'�#&A�F'�*� +.5/6��7score = 80if score >= 90:
eval = 'A'elif score >=80:
eval = 'B'else:
eval = 'others'
print(eval) # -> B
2018803-214-1 13
Else�'��)&����!$%���(�else if ,��"%elif�,���
Loop��1JcontinueK# FBI?�$(�J0��K/���4�$( �scores = [80, 100, 0, 60]sum = count = 0 # count0FBI?4�$(��for score in scores:
if score == 0:continue
sum += scorecount += 1
average = sum/countprint(sum, count, average) # -> 240 3 80.0
2018 �LAG9DCH91 14
>@7E?+0�=;54sum.��$���86H?412%�()$�score==0/��.0���� ,$*�����186H?��1$(!- �"/��0��'&��/<I:H=.�3��/��.�$*��4��J=continueK#'( �
Loop��2BbreakC# >:A5�$'�B0�� C+��0�$' B72�.Cscores = [80, 100, 0, 60]sum = count = 0 # count,>:A50�$'��for score in scores:
if score == 0:break
sum += scorecount += 1
average = sum/countprint(sum, count, average) # -> 180 2 90.0
2018��D9?2<;@21 15
��)�+3A6(�continue0break*���#+���=A98?41��!-�"�$BbreakC�&+�+=A9��0��%/�
JS@OMT@1(�5�)QVJ��(for�),range())�4PFG6;=DVATF
*#�1. RLVG7�0�2. Chapter 3.2 For Loops
1. :.�27QVJ��2. DVATF*4CVH�
3. Chapter 3.4 A Few Words About Using Floats1. ������7<�-
4. If�,QVJ�7%(1. elif, continue, break
5. �"1. �"1�4: �937K?UJS@OMT@2. �"5:��3BVN1 (��ITG�0) > $138;.
6. �+
2018��WJS@OMT@1 16
'!KVE: http://ie.u-ryukyu.ac.jp/~tnal/2018/prog1/
/5����>�&1;.
_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 18
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=
��
��1�4: ������#�!� �"�
��5:��$�1 (���"���) �� �����
2018��%�!� �"�1 19
��• �: ��(�������)
• ��: �����– 4�
• 4 Functions, scoping, and abstraction (��1#*�)• 4.1 Functions and Scoping
– (4.1.1 Function Definitions) #�� �– 4.1.2 Keyword Arguments and Default Values– (4.1.3 Scoping) #�� �
• 4.2 Specifications• (4.3 Recursion) �!"• (4.4 Global Variables) �!"• 4.5 Modules
• �)��(� %): paiza, progate
2018�+"'�&$(�1 20
�� �
• ���: 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
• str.format.)*+, http://docs.python.jp/3/tutorial/inputoutput.html
• �5�!��%IT��"���(0�$�#2�� �4�2��!��&� ��, http://itpro.nikkeibp.co.jp/members/ITPro/ITBASIC/20020624/1/?rt=nocnt
• ��������, https://ja.wikipedia.org/wiki/��������
20182,0'/-1'1 21