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的用法。它能夠用於把函數的返回值賦值給一個變量。