《 程式設計導論——以Python為舟》第一章原始碼
阿新 • • 發佈:2018-12-16
第一章原始碼
#<程式:Hello World>
print("Hello world!")
##################################################################
#<程式:變數輸出例項>
a = 1 #第一次給a賦值
a = 2 #第二次給a賦值
print(a) #輸出2
a = "Hello!" #第三次給a賦值
print(a) #輸出Hello!
##################################################################
#<程式:加法運算例項>
a = 999998888877777666665555544444333332222211111
b = 123456789098765432101234567890987654321012345
sum = a + b
print(sum)
###############################################################################
#<程式:乘法運算例項>
a = 999998888877777666665555544444333332222211111
b = 123456789098765432101234567890987654321012345
product = a * b
print(product)
###############################################################################
#<程式:求平均成績例項>
grade1 = 90 #第一門成績
grade2 = 89 #第二門成績
sum = grade1 + grade2 #兩門成績總和
average = sum / 2 #兩門成績平均數
print(“average = ”,average) #輸出結果為:average = 89.5
###############################################################################
#<程式:除法運算例項>
a = 8; b = 2;c = a/b
print(c) #輸出結果為:4.0
###############################################################################
#<程式:春遊坐車問題1>
student = 70; seat = 30 #學生數和每輛大巴容量
num = student // seat + 1
print("We need at least ",num," buses") #輸出為:We need at least 3 buses
###############################################################################
#<程式:春遊坐車問題2>
student = 70; seat = 30 #學生數和每輛大巴容量
num = student // seat
if student % seat == 0:
print("We need at least ",num," buses")
else:print("We need at least ",num+1," buses")#輸出為:We need at least 3 buses
###############################################################################
#<程式:春遊坐車問題3>
student = 70; seat = 30
num = student // seat
r = student % seat
if r != 0: #如果在學生坐滿大巴後還有一些學生
num = num + 1
ave = student // num
if r == 0: #如果學生恰好能將大巴都坐滿
print(num,"buses. Each has ",ave," students.")
else:
lastbus = student-(num-1)*ave
print(num,"buses. One bus has ",last bus," students.")
print("Each of other buses has ",ave," students.")
###############################################################################
#<程式:求二維線性方程組示例1>
a0 = 2; b0 = 1; c0 = 4
a1 = 3; b1 = -2; c1 = -1 #方程組係數
a2 = a0 * a1; b2 = b0 * a1;c2 = c0 * a1 #等式1兩邊同時擴大a1倍
a3 = a1 * a0; b3 = b1 * a0;c3 = c1 * a0 #等式2兩邊同時擴大a0倍
a = a2 - a3; b = b2 - b3; c = c2 - c3 #等式1減等式2
#改進的if條件語句會加在此處
y = c / b
x = (c0 - b0 * y)/a0
print("x =",x, " y =",y)
###############################################################################
#<程式:求二維線性方程組例子2>
#前面程式省略不寫,可以參考前例
if ((b == 0) and (c == 0)):
print("Infinite Solution!")
elif (b == 0 ): print("No Solution!")
else:
y = c / b
x = (c0 - b0 * y)/a0
print("x = %.5f, y = %.5f"%(x,y))
###############################################################################
#<程式:布林型別例子>
b = 100<101
print(b)
###############################################################################
#<程式:序列索引>
L=[1,1.3,"2","China",["I","am","another","list"]]
print(L[0]+L[1])
###############################################################################
#<程式:列表append方法>
L = [1,1.3,"2","China",["I","am","another","list"]]
L.append(3)
print(L)
###############################################################################
#<程式:刪除序列元素>
L = [1,1.3,"2","China",["I","am","another","list"]]
L.remove(1)
print(L)
#L.remove(3) #報錯
###############################################################################
#<程式:判斷元素在不在序列中>
L = [1,1.3,"2","China",["I","am","another","list"]]
print("China" in L) #輸出為True
print("I" in L) #輸出為False
###############################################################################
#<程式:解二維線性方程組3>
#求解2x+y=4,3x-2y=-1
A = [[2,1],[3,-2]];B = [4,-1]
if A[0][0]==0:
y = B[0]/A[0][1];x = (B[1]-A[1][1]*y)/A[1][0]
elif A[1][0]==0:
y = B[1]/A[1][1];x = (B[0]-A[0][1]*y)/A[0][0]
else:
b = A[0][1]*A[1][0]-A[1][1]*A[0][0] #b為相減之後y的係數
c = B[0]*A[1][0]-B[1]*A[0][0] #c為相減之後等號右邊的常數項
if ((b == 0) and (c == 0)): print("Infinite Solution!")
elif (b == 0 ): print("No Solution!")
y = c/b; x = (B[0]-A[0][1]*y)/A[0][0]
print("x = ",x,"y = ",y)
###############################################################################
#<程式:if語句示例>
a=10; b=11
if a<b: print("a<b") #輸出結果為:a<b
###############################################################################
#<程式:if-else語句示例>
a=11; b=10
if a<b: print("a<b")
else: print("a>=b") #輸出結果為:a>=b
###############################################################################
#<程式:if-elif-else例子>
a = 34;b = 271;c = 88
if a>b:
if a>c:
print("The max number is:",a)
else:
print("The max number is:",c)
elif b>c:
print("The max number is:",b)
else:
print("The max number is:",c)
###############################################################################
#<程式:成績等級if運用示例>
s = 78
if s > 100 or s<0: print('The score is error!')
elif s >= 90: print('The score is 優秀')
elif s >= 60: print('The score is 及格')
else: print('The score is 不及格')
###############################################################################
#<程式:成績獎勵與懲罰if例子(有缺陷)>
p = 85;q = 95
if p >= 80:print('紅心')
elif q >= 90:print('巧克力')
else: print('家長簽名')
###############################################################################
#<程式:成績獎勵與懲罰if例子2>
p = 85;q = 95
if p >= 80 and q >= 90:print('紅心 巧克力')
elif p >= 80:print('紅心')
elif q >= 90:print('巧克力')
else: print('家長簽名')
###############################################################################
#<程式:for迴圈例子>
for i in range(1, 6):
print(i)
###############################################################################
#<程式:break例子1>
L = [3,7,-2,4,5]
for i in L:
if i <= 0: print("Not all positive!");break
else: print("All positive!")
###############################################################################
#<程式:break例子2>
L = [3,7,-2,4,5];flag = True
for i in L:
if i <= 0: flag = False; break
if flag: print("All positive!")
else: print("Not all positive!")
###############################################################################
#<程式:continue例子>
L = [3,7,-2,4,5]
for i in L:
if i <= 0: continue
print(i)
###############################################################################
#<程式:for迴圈例子1>
k=10
for i in range(0,k): #也可寫成range(k)
for j in range(1,6):
print(j,end='')
print('\n')
###############################################################################
#<程式:for迴圈例子2>
n = 15; k = 10
for i in range(0,k):
for j in range(1,n+1):
print(j,end='')
print('\n')
###############################################################################
#<程式:for迴圈例子3>
n = 15
for i in range(0,n):
for j in range(1,i+2):
print(j,end='')
print('\n')
###############################################################################
#<程式:for迴圈求平均數例子>
L = [12,32,45,78,22]
sum = 0
for e in L: # for i in range(len(L)):
sum = sum + e # sum = sum + L[i]
ave = sum/len(L)
print("The average is ",ave)
###############################################################################
#<程式:for迴圈列表分組例子>
L = [4,2,-11,3,1,5]
a = L[0];L1 = []; L2 = []
for i in range(1,len(L)):
if L[i]>a:
L2.append(L[i])
else: L1.append(L[i])
print("The list is ",L1+[a]+L2)
###############################################################################
#<程式:for迴圈字串“1”個數奇偶性例子>
S = "21314151116"
num = 0
for i in range(len(S)):
if S[i] == '1':
num = num +1
if num%2 == 0:
print("The number of '1' in S is even number")
else: print("The number of '1' in S is odd number")
###############################################################################
#<程式:for迴圈多項式展開例子>
n = 10; L = [1,1]
for i in range(1,n):
L0 = [0] + L; L = L + [0]
for j in range(len(L)):
L[j] = L[j] + L0[j]
print(L)
###############################################################################
#<程式:while迴圈例子1>
i = 1
while i <= 5:
print(i)
i=i+1
###############################################################################
#<程式:判一個數是否為質數>
num = 7;a = num//2
while a>1:
if num % a==0:
print('num is not prime');break
a = a - 1
else: #沒有執行break,則執行else
print('num is prime')
###############################################################################
#<程式:while迴圈例子2>
i = 1
while True:
print(i,'printing')
i=i+1
###############################################################################
#<程式:while迴圈例子3>
x=10
while True:
if(x<=0): break
print(2*x,end=' ')
x=x-1
###############################################################################