PS:PDF在線地址:http://bcmi.sjtu.edu.cn/~zhaohai/ptm2012/data/Python-kernel.programming.v2.pdfpython
2-1 變量,print 和字符串格式化運算符。啓動交互式解釋器,給一些變量賦值(字符串,數值等等)並經過輸入變量名顯示它們的值。再用 print 語句作一樣的事。這兩者有何區別? 也嘗試着使用字符串格式運算符%,多作幾回,慢慢熟悉。git
# -*- coding:utf-8 -*- >>>a = 123 >>>b = '123' >>>a 123 >>>b '123' >>>print a 123 >>>print b 123 >>>print '儘管print語句輸出的a和b是同樣的,但他們仍然不同,a是%d,是整型;b是%s,是字符串。' % (a,b) 儘管print語句輸出的a和b是同樣的,但他們仍然不同,a是123,是整型;b是123,是字符串。
可見,在交互式解釋器中,經過輸入變量名稱顯示值是直接顯示各變量包含變量類型的值,即字符串帶有字符串的標誌''引號;經過print語句,則字符串引號''被去掉。函數
2-2 程序輸出ui
(a) 這段腳本用來計算 1 + 2 * 4 的值spa
(b)這段腳本保存後再運行不會輸出任何項code
(c)保存爲腳本運行後,沒有任何輸出,與預想的同樣。究其緣由,是由於交互式解釋器和腳本運行的不一樣對象
(d)單獨執行這段代碼,不會有任何輸出;在交互式解釋器中輸入 1 + 2 * 4 ,會輸出"9".以下:blog
#!/usr/bin/env python 1 + 2 * 4
(e)使用print語句便可three
2-3 略utf-8
2-4 (a)
1 # -*- coding:utf-8 -*- 2 string = raw_input("Please enter something:") 3 print string
輸出:
Please enter something:Python is number one! Python is number one!
(b)
# -*- coding:utf-8 -*- string = raw_input("Please enter a number:") print "Your enter is %d" % int(string)
輸出:
Please enter a number:123 Your enter is 123
2-5 (a)
i = 0 while i <= 10: print i i += 1
(b)
for i in range(11): print i
2-6 (1)
# -*- coding:utf-8 -*- num = 10 if num > 0: print u"它是正數!" elif num < 0: print u"它是負數!" else: print u"它是0"
(2)
# -*- coding:utf-8 -*- num = int(raw_input("輸入一個數字:".decode("utf-8").encode("gbk"))) if num > 0: print u"它是正數!" elif num < 0: print u"它是負數!" else: print u"它是0"
2-7 (1) while
strings = raw_input("Please enter something:") i = 0 while i < len(strings): print strings[i],'(%d)' % i i += 1
(2) for
strings = raw_input("Please enter something:") for i,string in enumerate(strings): print string,"(%d)" % i
2-8
(1)
list1 = [123,45,678,91,124] i = 0 sum = 0 while i < len(list1): sum = sum + list1[i] i += 1 print sum sum = 0 for num in list1: sum = sum + num print sum
(2)
num_one = int(raw_input("Please enter the first number:")) num_two = int(raw_input("Please enter the second numebr:")) num_three = int(raw_input("Please enter the third number:")) num_four = int(raw_input("Please enter the fourth number:")) num_five = int(raw_input("Please enter the fifth number:")) list1 = [num_one,num_two,num_three,num_four,num_five] i = 0 sum = 0 while i < len(list1): sum = sum + list1[i] i += 1 print sum sum = 0 for num in list1: sum = sum + num print sum
2-9
number = [12,345,67,890,123] sum = 0 for num in number: sum = sum + num average = float(sum) / 5 print "%f is the average of these five numbers." % average
2-10
while True: num = int(raw_input("Please enter a number bewteen 1 and 100:")) if num >= 1 and num <= 100: print "Well Done.You done it." break else: print "Man,you are wrong!Please learn to read the prompt!!!" continue
2-11
# -*- coding:utf-8 -*- def calc_sum(): global sum sum = 0 i = 0 while i < 5: num = int(raw_input("請依次輸入五個數字:".decode("utf-8").encode("gbk"))) sum = sum + num i += 1 return sum while True: choice = raw_input("1求和,2求平均數,X退出,請輸入你的選擇:".decode("utf-8").encode("gbk")) if choice == 'X': print u"程序將退出" break elif choice.isdigit() and int(choice) == 1:
#isdigit()用來判斷一個對象是不是數字,是數字返回True,不然返回False。如123.isdigit()返回結果True print u"計算五個數的和" calc_sum() print u"這五個數的和是:",sum elif choice.isdigit() and int(choice) == 2: print u"計算五個數的平均值" calc_sum() print u"這五個數的平均值是:",float(sum) / 5 else: print u"請從新輸入"
2-12
(a) dir()會顯示全部全局變量
(b) dir不加括號返回<built-in function dir>,對dir的基礎介紹
(c) type(dir)返回<type 'builtin_function_or_method'>,意思是內建的函數或方法
2-13
2-14
原語句:
print -2 * 4 + 3 ** 2
改寫:
print -2 * 4 + (3 ** 2)
2-15/2-16 存疑,待補