Exercises 18python
代码less
# this one is like your scripts with argv def print_two(*args): arg1, arg2 = args print "arg1: %r, arg2: %r" % (arg1,arg2) # OK, that *args is actually pointless, we can just do this def print_two_again(arg1,arg2): print "arg1: %r, arg2: %r" % (arg1,arg2) # this just takes one argument def print_one(arg1): print "arg1: %r" % arg1 # this one takes no arguments def print_none(): print "I got nothin'." print_two("Jer","Chou") print_two_again("Jer","Chou") print_one("First!") print_none()
输出ide
Notes:函数
①注意函数的定义命令def及冒号。函数命名方法相似变量命名,名称能够随便取,但最好函数名称可以体现函数功能。
this
②圆括号"()"中的是函数的参数,函数能够接受0个、1个或多个参数,能够经过解包的方式给出,也能够直接给出,后者更为简便。spa
③函数名称和参数肯定之后,用冒号结束本行,开始下一行缩进。缩进通常使用4个空格,缩进的语句构成函数主体。code
④调用函数时,适用函数的名称并给定相应数量的参数对象
Exercise 19
three
代码ip
def cheese_and_crackers(cheese_count, boxes_of_crackers): print "You have %d cheeses!" % cheese_count print "You have %d boxes of crackers!" % boxes_of_crackers print "Man that's enough for a party!" print "Get a blanket. \n" print "We can just give the function numbers directly:" cheese_and_crackers(20, 30) print "OR, we can use variables from our script:" amount_of_cheeses = 10 amount_of_crackers = 50 cheese_and_crackers(amount_of_cheeses, amount_of_crackers) print "We can even do math inside too:" cheese_and_crackers(10 + 20, 5 + 6) print "And we can combine the two, vareables and math:" cheese_and_crackers(amount_of_cheeses + 100, amount_of_crackers + 1000)
输出
Notes:
①局部变量和全局变量的区别。函数内定义的变量无特殊声明时均为局部变量,对函数体外的变量赋值无影响。除非必要,尽可能避免定义相同的全局变量名称和函数变量名称。
②能够直接给定函数的参数,能够经过数学运算,能够经过变量,也能够经过其余函数的返回结果做为函数的参数。
③加分习题
def print_whatever(arguement): print arguement, "\n" #运行函数的不一样方法 print_whatever("The first way to call the function.") print_whatever(123456789) print_whatever("The third way. %s" % "Third") i = 123456789 print_whatever(i) def plus(x, y): return x + y print_whatever(plus(12, 34)) in_put = raw_input("Enter something: \n") print_whatever(in_put) print_whatever(i + 100000000) j = 11 print_whatever(j + plus(12, 34)) print_whatever(56 + plus(12, 34)) print_whatever(plus(12, 34) + plus(56, 78))
Exercise 20
代码
from sys import argv script, input_file = argv def print_all(f): print f.read() def rewind(f): f.seek(0) def print_a_line(line_count, f): print line_count, f.readline() current_file = open(input_file) print "First let's print the whole file:\n" print_all(current_file) print "Now let's rewind, kind of like a tape." rewind(current_file) print "Let's print three lines:" current_line = 1 print_a_line(current_line, current_file) current_line = current_line + 1 print_a_line(current_line, current_file) current_line = current_line + 1 print_a_line(current_line, current_file)
输出
Notes:
①文件对象也能够做为函数的参数参与函数的执行过程
② += 是一种简写,相似的有 x -= y; x *= y; x /= y; x %= y
x += y #即x = x + y
③seek()用来调整文件操做标记的位置,关于文件及文件夹的操做笔记中已有记录
Exercise 21
代码
def add(a, b): print "Adding %d + %d" % (a, b) return a + b def subtract(a, b): print "SUBTRACTING %d - %d" % (a, b) return a - b def multiply(a, b): print "MULTIPLYING %d * %d" % (a, b) return a * b def divide(a, b): print "DIVIDING %d / %d" % (a, b) return a / b print "Let's do some maths with just functions!" age = add(20, 3) height = subtract(180, 5) weight = multiply(65, 2) iq = divide(100, 2) print "Age: %d, Height: %d, Weight: %d, Iq: %d." % (age, height, weight, iq) # A puzzle for the extra credit, type it anyway. print "Here is a puzzle." what = add(age, subtract(height, multiply(weight, divide(iq, 2)))) print "That becomes: ", what, "Can you do it in hand?"
输出
Notes:
①注意return的用法。它能够用于把函数的返回值赋值给一个变量。