實驗1 pyhton開發環境使用和編程初體驗
實驗任務1
task1:
task1-1源碼:
1 # print輸出的幾種用法 2 3 # 用法1:用于輸出單個字符串或單個變量 4 print('hey,u') 5 6 # 用法2:用于輸出多個數據項,用逗號分隔 7 print('hey','u') 8 x,y,z = 1,2,3 9 print(x,y,z) 10 11 # 用法3:用戶混合字符串和變量值 12 print('x = %d, y = %d, z = %d' %(x,y,z)) # 方式1:傳統c風格 13 print('x = {}, y = {}, z = {}'.format(x,y,z)) #方式2:s.format()方法 14 print(f'x = {x}, y = {y}, z = {z}') #方式3:f-string方式 15 16 # 其他:輸出后是否換行 17 print(x) # 默認輸出后換一行 18 print(y) 19 print(z) 20 21 print(x, end=' ') #輸出結束后,不換行;通過end指定數據項之間的分隔符 22 print(y, end=' ') 23 print(z)
運行結果:
task2:
task1-2源碼:
1 # 輸出1 2 print('{:-^40}'.format('輸出1')) 3 print('x1 = {}, y1 = {}'.format(x1, y1)) 4 print('x2 = {}, y2 = {}'.format(x2, y2)) 5 6 # 輸出2 7 print('{:-^40}'.format('輸出2')) 8 print('x1 = {:.1f}, y1 = {:.1f}'.format(x1, y1)) 9 print('x2 = {:.1f}, y2 = {:.1f}'.format(x2, y2)) 10 11 # 輸出3 12 print('{:-^40}'.format('輸出3')) 13 print('x1 = {:<15.1f}, y1 = {:<15.1f}'.format(x1, y1)) 14 print('x2 = {:<15.1f}, y2 = {:<15.1f}'.format(x2, y2)) 15 16 # 輸出4 17 print('{:-^40}'.format('輸出4')) 18 print('x1 = {:>15.1f}, y1 = {:>15.1f}'.format(x1, y1)) 19 print('x2 = {:>15.1f}, y2 = {:>15.1f}'.format(x2, y2))
運行結果:

task3:
task1-3源碼:
1 # f-string方式輸出數據并控制格式 2 3 name1, age1 = 'Bill', 19 4 name2, age2 = 'Hellen', 18 5 title = 'Personnel Information' 6 7 print(f'{title:=^40}') 8 print(f'name: {name1:10} age: {age1:3}') 9 print(f'name: {name2:10} age: {age2:3}') 10 print(40*'=')
運行結果:

實驗任務2:
task1:
task2-1源碼:
1 # 內置函數 2 # 功能:把字符串轉換為python表達式 3 # 理解:相當于把圓括號中的內容,兩側引號去掉 4 5 r1 = eval('1 + 2') 6 print(type(r1), r1) 7 8 r2 = eval('1 + 2j') 9 print(type(r2), r2) 10 11 r3 = eval('"python"') 12 print(type(r3), r3) 13 14 r4 = eval('7, 42') 15 print(type(r4), r4)
運行結果:

task2:
task2-2源碼:
1 # 組合使用內置函數eval()和input() 2 3 x,y = eval(input('Enter two oprands:')) 4 ans = x + y 5 6 print(f'{x} + {y} = {ans}') 7 print(f'{type(x)} + {type(y)} = {type(ans)}')
運行結果:

實驗任務3:
task1:
task3-1源碼:
1 # 浮點數簡單運算 2 3 ans1 = 0.1 + 0.2 4 print(f'0.1 + 0.2 = {ans1}') 5 6 import decimal 7 8 ans2 = decimal.Decimal('0.1') 9 print(f'0.1 + 0.2 = {ans2}')
運行結果:

實驗任務4:
task1:
task4-1源碼:
1 # 字符編碼相關 2 # chr()返回unicode編碼對應的字符 3 4 print(chr(0x1f600), end = " ") 5 print(chr(0x1f601), end = " ") 6 print(chr(0x1f602), end = " ") 7 print(chr(0x1f603), end = " ") 8 print(chr(0x1f604)) 9 10 print(chr(10000), end = " ") 11 print(chr(0x025b), end = " ") 12 print(chr(0x2708), end = " ") 13 print(chr(0x00A5), end = " ") 14 print(chr(0x266b)) 15 16 # ord()返回字符的unicode編碼 17 print(ord('a'), end = " ") 18 print(ord('b'), end = " ") 19 print(ord('c')) 20 21 print(ord('A'), end = " ") 22 print(ord('B'), end = " ") 23 print(ord('C')) 24 25 print(ord('0'), end = " ") 26 print(ord('1'), end = " ") 27 print(ord('2'))
運行結果:

實驗任務5:
task1:
task5-1源碼:
1 # 簡單的數學計算 2 import math 3 4 n = float(input('輸入一個數:')) 5 6 # python中計算開平方的三種方式 7 ans1 = n**0.5 8 ans2 = pow(n, 0.5) 9 ans3 = math.sqrt(n) 10 11 print('%.2f的平方根是:%.2f' %(n, ans1)) 12 print('{:.2f}的平方根是:{:.2f}'.format(n, ans2)) 13 print(f'{n:.2f}的平方根是:{ans3:.2f}')
運行結果:

task2:
task5-2源碼:
1 # 神奇的pi, 與大餅 2 3 import math 4 5 text = ''' 6 好奇心是人的天性。 7 理想情況下,學習新東西是讓人愉快的事。 8 但學校里的學習似乎有點像苦役。 9 有時候,需要畫一個大餅,每次嘗試學一些新鮮的,才會每天變得更好一點點。 10 ''' 11 print(text) 12 13 r = float(input('給學習畫一個大餅,大餅要做的很大,半徑要這么大:')) 14 15 circle = 2*math.pi*r 16 17 print(f'繞起來,大餅的圓周有這么長,{circle},夠不夠激發你探索未知的動力......')
運行結果:

實驗任務6:
task1:
task6-1源碼:
1 x = eval(input()) 2 y = pow(x,365) 3 print(f'{x:.2f}的365次方:{y:.2f}')
運行結果:

實驗任務7:
task1:
task7-1源碼:
1 # 煮出一個完美雞蛋 2 p,c,K,Tw,Ty = eval('1.038, 3.7, 5.4*0.001, 100, 70') 3 M,T0 = eval(input()) 4 import math 5 T1 = ((pow(M, 2/3))*c*(pow(p, 1/3)))/(K*(math.pi**2)*(pow(((4*math.pi)/3), 2/3))) 6 T2 = math.log(0.76*((T0-Tw)/(Ty-Tw))) 7 t = T1*T2 8 a = int(t) 9 b = (t-a)*100 10 print(f'在原始溫度T0 = {T0} 0C的條件下, 煮出完美雞蛋需要t = {a}分{b:.0f}秒')
運行結果:

實驗任務8:
task1:
task8-1源碼:
1 """ 2 家用電器銷售系統 3 v1.0 4 """ 5 6 # 歡迎信息 7 print('歡迎使用家用電器銷售系統!') 8 9 # 產品信息列表 10 print('產品和價格信息如下:') 11 print('*'*60) 12 print('%-10s'%'編號','%-10s'%'名稱','%-10s'%'品牌','%-10s'%'價格','%-10s'%'庫存數量') 13 print('-'*60) 14 print('%-10s'%'0001','%-10s' %'電視機','%-10s' %'海爾', '%10.2f' %5999.00, '%10d' %20) 15 print('%-10s'%'0002','%-10s' %'冰箱', '%-10s' %'西門子','%10.2f' %6998.00, '%10d' %15) 16 print('%-10s'%'0003','%-10s' %'洗衣機','%-10s' %'小天鵝', '%10.2f' %1999.00, '%10d' %10) 17 print('%-10s'%'0004','%-10s' %'空調', '%-10s' %'格力', '%10.2f' %3900.00, '%10d' %0) 18 print('%-10s'%'0005','%-10s' %'熱水器','%-10s' %'美的', '%10.2f' %688.00, '%10d' %30) 19 print('%-10s'%'0006','%-10s' %'筆記本','%-10s' %'聯想', '%10.2f' %5699.00, '%10d' %10) 20 print('%-10s'%'0007','%-10s' %'微波爐','%-10s' %'蘇泊爾','%10.2f' %480.50, '%10d' %33) 21 print('%-10s'%'0008','%-10s' %'投影儀','%-10s' %'松下', '%10.2f' %1250.00, '%10d' %12) 22 print('%-10s'%'0009','%-10s' %'吸塵器','%-10s' %'飛利浦','%10.2f' %999.00, '%10d' %9) 23 print('-'*60) 24 25 # 用戶輸入信息 26 product_id = input('請輸入您要購買的產品編號:') 27 price = float(input('請輸入您要購買的產品價格:')) 28 count = int(input('請輸入您要購買的產品數量:')) 29 30 # 計算金額 31 print('購買成功,您需要支付',price*count,'元') 32 33 # 退出系統 34 print('謝謝您的光臨,下次再見!')
運行結果:

task2:
task8-2源碼:
""" 家用電器銷售系統 v1.0 """ # 歡迎信息 print('歡迎使用家用電器銷售系統!') # 產品信息列表 print('產品和價格信息如下:') print('*'*60) print('{:<10s}'.format('編號'),'{:<10s}'.format('名稱'),'{:<10s}'.format('品牌'),'{:<10s}'.format('價格'),'{:<10s}'.format('庫存數量')) print('-'*60) print('{:<10s}'.format('0001'),'{:<10s}' .format('電視機'),'{:<10}'.format('海爾'), '{:<10.2f}'.format(5999.00), '{:<10d}'.format(20)) print('{:<10s}'.format('0002'),'{:<10s}' .format('冰箱'),'{:<10}'.format('西門子'), '{:<10.2f}'.format(6998.00), '{:<10d}'.format(15)) print('{:<10s}'.format('0003'),'{:<10s}' .format('洗衣機'),'{:<10}'.format('小天鵝'), '{:<10.2f}'.format(1999.00), '{:<10d}'.format(10)) print('{:<10s}'.format('0004'),'{:<10s}' .format('空調'),'{:<10}'.format('格力'), '{:<10.2f}'.format(3900.00), '{:<10d}'.format(0)) print('{:<10s}'.format('0005'),'{:<10s}' .format('熱水器'),'{:<10}'.format('美的'), '{:<10.2f}'.format(688.00), '{:<10d}'.format(30)) print('{:<10s}'.format('0006'),'{:<10s}' .format('筆記本'),'{:<10}'.format('聯想'), '{:<10.2f}'.format(5699.00), '{:<10d}'.format(10)) print('{:<10s}'.format('0007'),'{:<10s}' .format('微波爐'),'{:<10}'.format('蘇泊爾'), '{:<10.2f}'.format(480.50), '{:<10d}'.format(33)) print('{:<10s}'.format('0008'),'{:<10s}' .format('投影儀'),'{:<10}'.format('松下'), '{:<10.2f}'.format(1250.00), '{:<10d}'.format(12)) print('{:<10s}'.format('0009'),'{:<10s}' .format('吸塵器'),'{:<10}'.format('飛利浦'), '{:<10.2f}'.format(999.00), '{:<10d}'.format(9)) print('-'*60) # 用戶輸入信息 product_id = input('請輸入您要購買的產品編號:') price = float(input('請輸入您要購買的產品價格:')) count = int(input('請輸入您要購買的產品數量:')) # 計算金額 print('購買成功,您需要支付',price*count,'元') # 退出系統 print('謝謝您的光臨,下次再見!')
運行結果:

task3:
task8-3源碼:
1 """ 2 家用電器銷售系統 3 v1.0 4 """ 5 6 # 歡迎信息 7 print('歡迎使用家用電器銷售系統!') 8 9 # 產品信息列表 10 print('產品和價格信息如下:') 11 print('*'*60) 12 a = ['編號','名稱','品牌','價格','庫存數量'] 13 b = ["0001","0002","0003","0004","0005","0006","0007","0008","0009"] 14 c = ['電視機','冰箱','洗衣機','空調','熱水器','筆記本','微波爐','投影儀','吸塵器'] 15 d = ['海爾','西門子','小天鵝','格力','美的','聯想','蘇泊爾','松下','飛利浦'] 16 e = [5999.00,6998.00,1999.00,3900.00,688.00,5699.00,480.50,1250.00,999.00] 17 f = [20,15,10,0,30,10,33,12,9] 18 19 print(f'{a[0]:10s}{a[1]:10s}{a[2]:10s}{a[3]:10s}{a[4]:10s}') 20 print('-'*60) 21 print(f'{b[0]:10s}{c[0]:10s}{d[0]:10s}{e[0]:10.2f}{f[0]:10d}') 22 print(f'{b[1]:10s}{c[1]:10s}{d[1]:10s}{e[1]:10.2f}{f[1]:10d}') 23 print(f'{b[2]:10s}{c[2]:10s}{d[2]:10s}{e[2]:10.2f}{f[2]:10d}') 24 print(f'{b[3]:10s}{c[3]:10s}{d[3]:10s}{e[3]:10.2f}{f[3]:10d}') 25 print(f'{b[4]:10s}{c[4]:10s}{d[4]:10s}{e[4]:10.2f}{f[4]:10d}') 26 print(f'{b[5]:10s}{c[5]:10s}{d[5]:10s}{e[5]:10.2f}{f[5]:10d}') 27 print(f'{b[6]:10s}{c[6]:10s}{d[6]:10s}{e[6]:10.2f}{f[6]:10d}') 28 print(f'{b[7]:10s}{c[7]:10s}{d[7]:10s}{e[7]:10.2f}{f[7]:10d}') 29 print(f'{b[8]:10s}{c[8]:10s}{d[8]:10s}{e[8]:10.2f}{f[8]:10d}') 30 print('-'*60) 31 32 # 用戶輸入信息 33 product_id = input('請輸入您要購買的產品編號:') 34 price = float(input('請輸入您要購買的產品價格:')) 35 count = int(input('請輸入您要購買的產品數量:')) 36 37 # 計算金額 38 print('購買成功,您需要支付',price*count,'元') 39 40 # 退出系統 41 print('謝謝您的光臨,下次再見!')
運行結果:

浙公網安備 33010602011771號