Upload
khangminh22
View
1
Download
0
Embed Size (px)
Citation preview
��������������� ���� �
��������������� ���� �
��������������������������������������������������
��������
�����������������
��������������� ���� �
!"����#��
� $������������%���������������#����������������������������������������������&
� $��!��!'��������������%��������������������&
� $��������������������'���������������''����������������������������&
��������������� ���� (
!"����#���)����&*
� $��%������������������������������������������������'����������������������������'����&
��������������� ���� +
$������������������������
� ����� �%�’#������������������������������������������ �����������������������'������������''������)*&
� ,��'�-��������������������)!�*
� ��������������������������'�!������)���&�.��*
� ������������������������������'��)�&��/)**
��������������� ���� 0
$������������������������
� ��������������������� ����������������%�������!��������������%�������������'���&
� $���������!�������%����������'��!����������'���&
� $�����������!��������%��%����������'���&
��������������� ���� �
$������������������������
� 1#�������'������������'�����������������������'�������������%!�2�&� �����������%��������������������%�����������&
� �������%���$������������������!���������������%����������'���&
� �������������!�������������������������'�����������2�����������������'������������������������&
��������������� ���� �
��������������� ���� 3
��������� �������''�
� ��������������'�2�����!����� ����''�����������������������&
� $���!��������– %��%��������.����������������������������#��������.����������&�4�����������5�������������.����������������!���������������������&
��������������� ���� 6
��������� �������''�
� $���������������������������������������������''�����������������������&
� 4���������������������������������� �%������������������������''�������#�2��&
��������������� ���� 7
��������� �������''�
� 1����,�������'�����…def main():
print "Happy birthday to you!" print "Happy birthday to you!" print "Happy birthday, dear Fred..." print "Happy birthday to you!"
� 8�#����������9>>> main()Happy birthday to you!Happy birthday to you!Happy birthday, dear Fred...Happy birthday to you!
��������������� ���� �:
��������� �������''�
� $����’����������'�����������������������;�)print "Happy birthday to you!"*
� 4����������������������������������������'����def happy():
print "Happy birthday to you!“
� 4����������������� �%�������%��������������&
��������������� ���� ��
��������� �������''�
� $�����%�������<def singFred():
happy()happy()print "Happy birthday, dear Fred...“happy()
� 8�#������������������<>>> singFred()Happy birthday to you!Happy birthday to you!Happy birthday, dear Fred...Happy birthday to you!
��������������� ���� ��
��������� �������''�
� ����������������������#�������'�����������;
� 4��������’��=���’��!������>�4�����'��%��������%����=������������;def singLucy():
happy()happy()print "Happy birthday, dear Lucy...“happy()
��������������� ���� (
��������������� ���� �(
��������� �������''�
� 4�����'��%��������������������������!����=�����������def main():
singFred()printsingLucy()
� $�����#�������������%�������>>> main()Happy birthday to you!Happy birthday to you!Happy birthday, dear Fred..Happy birthday to you!
Happy birthday to you!Happy birthday to you!Happy birthday, dear Lucy...Happy birthday to you!
��������������� ���� �+
��������� �������''�
� $�������%��2������;�,��… �����’�����''��'��������������'������&
� $�����'�������������!��%����singFred���singLucy ������������������������print ��������&
� $������%��������������'��!����''�������������!���������������&
��������������� ���� �0
��������� �������''�
� $�������������������singdef sing(person):
happy()happy()print "Happy birthday, dear", person + ".“happy()
� $��������������������������������������&��������� ����#��!'��������������'�?���%���������������������''��&
��������������� ���� ��
��������� �������''�
� �����%��������–>>> sing("Fred")Happy birthday to you!Happy birthday to you!Happy birthday, dear Fred.Happy birthday to you!
� 4��������������������%����������;
��������������� ���� �3
��������� �������''�
� �����%�����������def main():
sing("Fred")printsing("Lucy")
� 8�#������������������>>> main()Happy birthday to you!Happy birthday to you!Happy birthday, dear Fred.Happy birthday to you!
Happy birthday to you!Happy birthday to you!Happy birthday, dear Lucy.Happy birthday to you!
��������������� ���� �6
�������@'���%�������������
� ��������������#'�������������� �%����������'��������%����# Draw bar for initial principalbar = Rectangle(Point(0, 0), Point(1, principal))bar.setFill("green")bar.setWidth(2)bar.draw(win)
bar = Rectangle(Point(year, 0), Point(year+1, principal))bar.setFill("green")bar.setWidth(2)bar.draw(win)
��������������� ���� +
��������������� ���� �7
�������@'���%�������������
� $��������'����%�����!�� �%���������������������������������&
� $�����������!��������
� 1�%��''�����!������'��!�
� $���%����%�����!��%�''�!����%����
� $�����������#'�������!�������'������������������������������&
��������������� ���� �:
�������@'���%�������������
� $�������'�������������'��2��'�2�������def drawBar(window, year, height):
# Draw a bar in window starting at year with given heightbar = Rectangle(Point(year, 0), Point(year+1, height))bar.setFill("green")bar.setWidth(2)bar.draw(window)
� $������������������� �%������'������������#'���&����%�������8���%�� �%�������%��!����������:�����������'����A�:::������������''�
drawBar(win, 0, 2000)
��������������� ���� ��
�����������������������$�������'�
� ����2���������������'��������������������������'����������%,���������� �!���%������������%����%�#��!'�>
� $�������� ����#��!'�����������������'����������������#���#��!'�����!������������&
��������������� ���� ��
�����������������������$�������'�
� B��������������������%��'���'����!�����&�$���#��!'����������������������������������'��'��������������� ��#���������������������#����������������#��!'�������������������������������������&
� $�����'��%������������������������#��!'����������������������������������#��!'�����!������������������&
��������������� ���� �(
�����������������������$�������'�
� ����������GraphWin �������#��!'��win ��������������������main �����������������'��������!'�����drawBar&
� $���window �����������drawBar���������������#'������win �����main %����drawBar ����''��&
��������������� ���� �+
�����������������������$�������'�
� ����������������������'��2��'�2�����������C���D)C����'-��������D*�C!���D
� $����������������������������!��������������
� ����'-������������������!'��������'�������#��!'������
��������������� ���� 0
��������������� ���� �0
�����������������������$�������'�
� ����'��������� �'�2��''�#��!'����������������������� ������'��������!'���������!�������������������&�@��!'���%�����������'�������'��%�������������������������������������������'�������������#��!'��������������������������!���&
��������������� ���� ��
�����������������������$�������'�
� ���������������''���!�����������������''�%���!���'����������'�������������������&C���D)C���'-��������D*
� 4�������������������������������'' ������������������-������������&
��������������� ���� �3
�����������������������$�������'�
� $����''�������������������5����������������������������''&
� $�������'����������������������������������������#'��������'����!���������'������������������''&
� $���!������������������������5������&
� ������'����������������������"���������%������������������%���''��&
��������������� ���� �6
�����������������������$�������'�
� =��’��������������������''�%��������sing(“Fred”)printsing(“Lucy”)
� 4������������������sing(“Fred”) ��5�����������main �����������'�����������&
� �������'��2�����������������������sing ��������������������������'�������� �person&�
��������������� ���� �7
�����������������������$�������'
� $�������'�����������������������#'�������������'��������&���E�������������''�%���������������!�����5�������person = “Fred”
��������������� ���� (:
�����������������������$�������'�
F������������#��!'��person ���"����
!���������'�?��&
��������������� ���� �
��������������� ���� (�
�����������������������$�������'�
� ������������� ��������!������5�����������!�������sing&
� $��������������������������������������'' ����happy&�4������������5�>
� ���������������������5�����������sing ������������������'����happy&
� happy ����������������'������� �%���������5����������������'������������%��������'��������������&
��������������� ���� (�
�����������������������$�������'�
� B5��������������������������%��%�����%����������������happy&
� 4�����������������������������sing �������'������������main ���������������������'����''�%�����������������''&
��������������� ���� ((
�����������������������$�������'�
� F��������������person #��!'�����sing ������������;
� $�������������������!��'��'����������#��!'���������'�����%������������������5���&
� =��'�#��!'��������� ���������#'�����������������������5�����������������5�&
��������������� ���� (+
�����������������������$�������'�
� $�����5������������������!���print �%���������������!'�2�'���&
� ��������������������������''����sing ����������'�����������������sing �������� �%������������'���������G=���H&
��������������� ���� (0
�����������������������$�������'�
� $���!�������sing ����5�����������=����%����������������������������happy ���������'������������main&
��������������� ���� (�
�����������������������$�������'�
��������������� ���� 3
��������������� ���� (3
����������������������$�������'�
� ������������������������������5��'��%����'���'����������&���������������������'�������'�����������������������!��������������� ��&&��������������'������������������������������������'�������� ���������������'�������������������������������������'�������� ����&
��������������� ���� (6
�����������������������$�������'�
� ������5��'� ���������������''����drawBar�drawBar(win, 0, principal)
� 4����������'�������������drawBar ����������������������������������������'��������������������������������def drawBar(window, year, height):
��������������� ���� (7
�����������������������$�������'�
� $�����������������������������������!�������!������������%�����������������������������
window = winyear = 0height = principal
��������������� ���� +:
8������I���'�����������������
� ������������������#������������������������'�?�������#��!'���������������&
� ���������������������������������&
� 4������''�����������������������������������������'���!��������������������&
��������������� ���� +�
����������$���I������@'���
� 4�’#��'��������������������5��'���������������������������#'������������''��&
� discRt = math.sqrt(b*b – 4*a*c)
� $���#'���b*b – 4*a*c ����������'������������math.sqrt&
� 4�����sqrt ������� �����.����������������������&
��������������� ���� +�
����������$���I������@'���
� $��������������������������.�����������!���
def square(x):return x*x
� 4����������������������return �����5����������������������������������'��������������%������������������%���''��&
� ���������� �����#'��)�*����#������������return �����������������!�2���������''��������5�������������'�&
��������������� ���� 6
��������������� ���� +(
����������$���I������@'���
� >>> square(3)9
� >>> print square(4)16
� >>> x = 5>>> y = square(x)>>> print y25
� >>> print square(x) + square(3)34
��������������� ���� ++
����������$���I������@'���
� 4��������������.����������������%������������������'��'���������������!��%����)5� ��*����)5� ��*&
� def distance(p1, p2):dist = math.sqrt(square(p2.getX() - p1.getX()) +
square(p2.getY() - p1.getY()))return dist
��������������� ���� +0
����������$���I������@'���
� �������������������������������������������������#'��&
� $��������� �����'��'������������������5����������������return ��������&
� ����������)5 ��*�����J�5�K�������J�5�< ����������� �����
��������������� ���� +�
����������$���I������@'���
� 4�����''���������������� ���������'���������������&
� num1, num2 = input("Please enter two numbers (num1, num2) ")s, d = sumDiff(num1, num2)print "The sum is", s, "and the difference is", d
� ���!����� �����#'��������������!��������������� ����� ��������������#'������������)�������* ����� ���������������)��������������*&
��������������� ���� +3
����������$���I������@'���
� ���“����” – ''��������������������������#'�� �%��������������������return ���������������&�����������%��������return ����!�2�������'��!"��� ���������None&
� ������������!'������%�������#'��-���������������������������������return;
��������������� ���� +6
����������$���I������@'���
� ��������#'��-��������������������������������������� �����2�����2����������������!�����������'��������return;
��������������� ���� 7
��������������� ���� +7
��������������L��������������
� I������#'���������������%������������������������������������!�2���������''��&
� ��������� �%����������������!�2���������''���!����2���������������������������������&
� M�����������%���������%��������������!'����.���������������������������!�'������'��!������%����������%��2�����������'���������!��%�������'��������'���������&
��������������� ���� 0:
��������������L��������������
� ���������������%����������������������!�2��������&� ������������%��%��'������������������������'�������������������������&�=��’��'��2���������-������������������&
� def addInterest(balance, rate):newBalance = balance * (1 + rate)balance = newBalance
��������������� ���� 0�
��������������L��������������
� $������������������������!'������������������������%�#'����������'�����������������������&
� =��’��%�����������������������������def test():
amount = 1000rate = 0.05addInterest(amount, rate)print amount
��������������� ���� 0�
��������������L��������������
� 4�������������������0N�%�''�!������������������� �����������:0:&
� >>> test()1000
� 4���%����%���>�F�����;
��������������� ���� 0(
��������������L��������������
� $����������%��'���������������������������������%��'��'�#��!'����''���amount ���rate%���������#������������'�#'�������1000 ���0.05 ���������#�'�&
def addInterest(balance, rate):
newBalance = balance * (1 + rate)
balance = newBalance
def test():
amount = 1000
rate = 0.05
addInterest(amount, rate)
print amount
��������������� ���� 0+
��������������L��������������
� ������'����������������������addInterest��������&
� $�������'����������balance ���rate ��������������#'��������������'����������amount ���rate.
� B#���������rate���������!��� ����������������#��!'���)!�����������������'��*&
def addInterest(balance, rate):
newBalance = balance * (1 + rate)
balance = newBalance
def test():
amount = 1000
rate = 0.05
addInterest(amount, rate)
print amount
��������������� ���� �:
��������������� ���� 00
��������������L��������������
� $��������������������������������������#��!'���balance ���rate���addInterest ����������������#'�������������'���������;
def addInterest(balance, rate):
newBalance = balance * (1 + rate)
balance = newBalance
def test():
amount = 1000
rate = 0.05
addInterest(amount, rate)
print amount
��������������� ���� 0�
��������������L��������������
��������������� ���� 03
��������������L��������������
� B5�����������������'�������addInterest����������%�#��!'� �newBalance&
� balance �������������������#'������newBalance&
def addInterest(balance, rate):
newBalance = balance * (1 + rate)
balance = newBalance
def test():
amount = 1000
rate = 0.05
addInterest(amount, rate)
print amount
��������������� ���� 06
��������������L��������������
� balance ��%�������������������#'�����newBalance �!������������������������amount �������test��������&
�������������)!'��� ����*�
��%,'��� J�!'����O�)��K����*
!'����J��%,'���
��������)*�
������J��:::
����J�:&:0
����������)����� ����*
�����������
��������������� ���� 07
��������������L��������������
��������������� ���� �:
��������������L��������������
� B5�����������addInterest �������'��������������'������������test&
� $���'��'�#��!'�� ����'����������������� ����addInterest ��%� �!���amount ���rate �������test������������''���������������������'�#'���;
def addInterest(balance, rate):
newBalance = balance * (1 + rate)
balance = newBalance
def test():
amount = 1000
rate = 0.05
addInterest(amount, rate)
print amount
��������������� ���� ��
��������������� ���� ��
��������������L��������������
� $��������?�����������'�������������������������'�������#������#'��� ����������'���������&�$����������������������#���������������#��!'��������'����������'��������&
� ���������������������''����������!��#'��&
��������������� ���� ��
��������������L��������������
� ��������������'�����)�KK ��� ������������*����''�%�#��!'���������'#������!������������������������������&�$������������������������������������!�����������&
� 4�������%�#'������������������������'�������� �����#'����������#��!'����������''������������''�������&
��������������� ���� �(
��������������L��������������
� ������������������’���#���������!�'��� �����'������#��%��'��!��������������addInterest �������������������������������newBalance&
��������������� ���� �+
��������������L��������������
def addInterest(balance, rate):newBalance = balance * (1 + rate)return newBalance
def test():
amount = 1000rate = 0.05amount = addInterest(amount, rate)print amount
test()
��������������� ���� �0
��������������L��������������
� ����������'��2���������'�������� ����%�����%������������������!�2�������'��%���������������&�4�����'�������������������!'���������'��� �������������������������������������������!'������������'���&
� 4�����'������������������!'�����������'����%���������'�2��balances[0] = balances[0] * (1 + rate)
��������������� ���� ��
��������������L��������������
� $������������ �“��'���'������#'����������:�� ����������������'����!��)��K����*������������������'��!�2����������:��
����������������'���&”� �����������'�%�������������%��'��!��%�����'�����������������������������: �� �… �'�����– �&
��������������� ���� ��
��������������� ���� �3
��������������L��������������# addinterest3.py
# Illustrates modification of a mutable parameter (a list).
def addInterest(balances, rate):
for i in range(len(balances)):
balances[i] = balances[i] * (1+rate)
def test():
amounts = [1000, 2200, 800, 360]
rate = 0.05
addInterest(amounts, 0.05)
print amounts
test()
��������������� ���� �6
��������������L��������������
� I����!�� ����������'���������������#'����[1000, 2200, 800, 360]
� $�����������������[1050.0, 2310.0, 840.0, 378.0]
� 4����������>�����������������������!��#'�� �!������'��2��'�2��amounts ���!���������;
��������������� ���� �7
��������������L��������������
� $����������%��'��������test ����������#��!'���amounts ���rate&
� $���#'����������#��!'��amounts ����'�����!"�������������������������#'���&
def addInterest(balances, rate):
for i in range(len(balances)):
balances[i] = balances[i] * (1+rate)
def test():
amounts = [1000, 2200, 800, 360]
rate = 0.05
addInterest(amounts, 0.05)
print amounts
��������������� ���� 3:
��������������L��������������
��������������� ���� 3�
��������������L��������������
� F�5� �addInterest�5������&�$���'������������������������5������������: �� �9 �'�����<����������������#'������balances&
def addInterest(balances, rate):
for i in range(len(balances)):
balances[i] = balances[i] * (1+rate)
def test():
amounts = [1000, 2200, 800, 360]
rate = 0.05
addInterest(amounts, 0.05)
print amounts
��������������� ���� 3�
��������������L��������������
��������������� ���� �(
��������������� ���� 3(
��������������L��������������
� �����������������'��#'�������'������������������������?�������������!�����������!�5����#����������� �!���������%�#'����%������������������������������'���&
� $����'��#'����%�''�!�������������������!����''������&
def addInterest(balances, rate):
for i in range(len(balances)):
balances[i] = balances[i] * (1+rate)
def test():
amounts = [1000, 2200, 800, 360]
rate = 0.05
addInterest(amounts, 0.05)
print amounts
��������������� ���� 3+
��������������L��������������
� 4����addInterest ��������� �����'��������������amounts ��%���������������%�#'���&
� $���#��!'��amounts %��E��������)��E�����''��'���* �!�������������������'������������ �����������������#���!'����������''��������&
��������������� ���� 30
��������������L��������������
� ������������'%���������!��#'��&�1�%�#�� ��������#'����������#��!'���������!'���!"����)'�2���'���������������!"���* ��������������������������������!"����%�''!��#���!'����������''��������&
� $������������������������5��'���������'�������������������������������0;
��������������� ���� 3�
����������������������������
� ����� ������������#��!�����������������������������������������'������&
� ��������������������������������������2�����������������������'�&
� �������'�����������������������������'������'�5 ����������������������������'������2�������������������������&
��������������� ���� 33
����������������������������
� ���%�������'�%�������������'�5����������!��2���'���������%���������''�����!������ ��������%������2�����������������%�&
� $����������%�''�!������������������������'�����������7&
��������������� ���� 36
����������������������������def main():
# Introduction
print "This program plots the growth of a 10 year investment."
# Get principal and interest rate
principal = input("Enter the initial principal: ")
apr = input("Enter the annualized interest rate: ")
# Create a graphics window with labels on left edge
win = GraphWin("Investment Growth Chart", 320, 240)
win.setBackground("white")
win.setCoords(-1.75,-200, 11.5, 10400)
Text(Point(-1, 0), ' 0.0K').draw(win)
Text(Point(-1, 2500), ' 2.5K').draw(win)
Text(Point(-1, 5000), ' 5.0K').draw(win)
Text(Point(-1, 7500), ' 7.5k').draw(win)
Text(Point(-1, 10000), '10.0K').draw(win)
# Draw bar for initial principal
drawBar(win, 0, principal)
# Draw a bar for each subsequent year
for year in range(1, 11):
principal = principal * (1 + apr)
drawBar(win, year, principal)
raw_input("Press <Enter> to quit.")
win.close()
��������������� ���� �+
��������������� ���� 37
����������������������������
� 4������2���������������������!'��!����#�����������'�������'�������������������%����%�%��������������%�''�!����%��������#'��������������������&
��������������� ���� 6:
����������������������������def createLabeledWindow():
window = GraphWin("Investment Growth Chart", 320, 240)
window.setBackground("white")
window.setCoords(-1.75,-200, 11.5, 10400)
Text(Point(-1, 0), ' 0.0K').draw(window)
Text(Point(-1, 2500), ' 2.5K').draw(window)
Text(Point(-1, 5000), ' 5.0K').draw(window)
Text(Point(-1, 7500), ' 7.5k').draw(window)
Text(Point(-1, 10000), '10.0K').draw(window)
return window
def main():
print "This program plots the growth of a 10 year investment."
principal = input("Enter the initial principal: ")
apr = input("Enter the annualized interest rate: ")
win = createLabeledWindow()
drawBar(win, 0, principal)
for year in range(1, 11):
principal = principal * (1 + apr)
drawBar(win, year, principal)
raw_input("Press <Enter> to quit.")
win.close()