版本1和2间的区别
于2006-09-01 12:09:11修订的的版本1
大小: 3820
编辑: czk
备注:
于2006-09-01 12:14:52修订的的版本2
大小: 4976
编辑: czk
备注:
删除的内容标记成这样。 加入的内容标记成这样。
行号 16: 行号 16:
 * Define the following six functions (either interactively, or in a module file that can be imported):
{{{
def f1(a, b): print a, b # Normal args
def f2(a, *b): print a, b # Positional varargs
def f3(a, **b): print a, b # Keyword varargs
def f4(a, *b, **c): print a, b, c # Mixed modes
def f5(a, b=2, c=3): print a, b, c # Defaults
def f6(a, b=2, *c): print a, b, c # Defaults and positional varargs
}}}
Now, test the following calls interactively and try to explain each result. Do you think mixing matching modes is a good idea in general? Can you think of cases where it would be useful?
{{{
>>> f1(1, 2)
>>> f1(b=2, a=1)
>>> f2(1, 2, 3)
>>> f3(1, x=2, y=3)
>>> f4(1, 2, 3, x=2, y=3)
>>> f5(1)
>>> f5(1, 4)
>>> f6(1)
>>> f6(1, 3, 4)
}}}
 * Write code to build a new list containing the square roots of all the numbers in this list: [2, 4, 9, 16, 25]. Code this as a for loop first, then as a map call, and finally as a list comprehension. Use the sqrt function in the built-in math module to do the calculation (i.e., import math, and say math.sqrt(x)). Of the three, which approach do you like best?

实验目的

  • 分支、循环结构的实现
  • 函数的用法

实验内容

  • 编写一个分支结构,输入一个百分制分数,输出分数的等级(优、良、中、及格、不及格)
  • 编写一个循环结构,对输入的一组数排序算法。
  • 编写一组函数,实现。。。
  • At the Python interactive prompt, write a function that prints its single argument to the screen and call it interactively, passing a variety of object types: string, integer, list, dictionary. Then try calling it without passing any argument. What happens? What happens when you pass two arguments?
  • Write a function called adder in a Python module file. The function adder should accept two arguments and return the sum (or concatenation) of its two arguments. Then add code at the bottom of the file to call the function with a variety of object types (two strings, two lists, two floating points), and run this file as a script from the system command line. Do you have to print the call statement results to see results on your screen?
  • Generalize the adder function you wrote in the last exercise to compute the sum of an arbitrary number of arguments, and change the calls to pass more or less than two. What type is the return value sum? (Hints: a slice such as S[:0] returns an empty sequence of the same type as S, and the type built-in function can test types.) What happens if you pass in arguments of different types? What about passing in dictionaries?
  • Change the adder function from previous exercise to accept and add three arguments:def adder(good, bad, ugly). Now,

provide default values for each argument and experiment with calling the function interactively. Try passing one, two, three, and four arguments. Then, try passing keyword arguments. Does the call adder(ugly=1, good=2) work? Why? Finally, generalize the new adder to accept and add an arbitrary number of keyword arguments, much like Exercise 3, but you'll need to iterate over a dictionary, not a tuple. (Hint: the dict.keys( ) method returns a list you can step through with afor or while.)

  • Define the following six functions (either interactively, or in a module file that can be imported):

def f1(a, b): print a, b          # Normal args
def f2(a, *b): print a, b         # Positional varargs
def f3(a, **b): print a, b        # Keyword varargs
def f4(a, *b, **c): print a, b, c   # Mixed modes
def f5(a, b=2, c=3): print a, b, c # Defaults
def f6(a, b=2, *c): print a, b, c    # Defaults and positional varargs

Now, test the following calls interactively and try to explain each result. Do you think mixing matching modes is a good idea in general? Can you think of cases where it would be useful?

>>> f1(1, 2)
>>> f1(b=2, a=1)
>>> f2(1, 2, 3)
>>> f3(1, x=2, y=3)
>>> f4(1, 2, 3, x=2, y=3)
>>> f5(1)
>>> f5(1, 4)
>>> f6(1)
>>> f6(1, 3, 4)
  • Write code to build a new list containing the square roots of all the numbers in this list: [2, 4, 9, 16, 25]. Code this as a for loop first, then as a map call, and finally as a list comprehension. Use the sqrt function in the built-in math module to do the calculation (i.e., import math, and say math.sqrt(x)). Of the three, which approach do you like best?

思考题

Consider the following code, which uses a while loop and found flag to search a list of powers of 2, for the value of 2 raised to the 5th power (32). It's stored in a module file called power.py.

   1 L = [1, 2, 4, 8, 16, 32, 64]
   2 X=5
   3 found = i = 0
   4 while not found and i < len(L):
   5     if 2 ** X == L[i]:
   6         found = 1
   7     else:
   8         i = i+1
   9 if found:
  10     print 'at index', i
  11 else:
  12     print X, 'not found'

at index 5

As is, the example doesn't follow normal Python coding techniques. Follow the steps below to improve it. For all the transformations, you may type your code interactively or store it in a script file run from the system command line (using a file makes this exercise much easier).

  1. First, rewrite this code with a while loop else, to eliminate the found flag and final if statement.
  2. Next, rewrite the example to use a for loop with an else, to eliminate the explicit list indexing logic. Hint: to get the index of an item, use the list index method (L.index(X) returns the offset of the first X in list L).
  3. Next, remove the loop completely by rewriting the examples with a simple in operator membership expression.
  4. Finally, use a for loop and the list append method to generate the powers-of-2 list (L) instead of hard-coding a list literal.
  5. Deeper thoughts: (1) Do you think it would improve performance to move the 2**X expression outside the loops? How would you code that? (2) Python also includes a map(function, list) tool that can generate the powers-of-2 list too: map(lambda x: 2**x, range(7)). Try typing this code interactively.

Python游戏开发基础/实验3 (2008-02-23 15:34:13由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.