**python** === --- # Function hw6(溫度攝氏轉華氏): def temperature(celsius): #設計一function fahrenheit=celsius*1.8+32 #先測試公式成功 return fahrenheit #傳回答案 x=input("請輸入攝氏溫度") #向使用者取值 print("華氏溫度為:%.1f"%(temperature(float(x)))) #把值丟到funcion裡並印出 --- hw5: list1=[1,2,3,4,5,6,10] list2=[] for a in list1: a=a**2 list2.append(a) print(list2) --- listname.reverse() #串列值倒轉顯示(函數方式,永遠倒轉) print(listname[::-1]) #串列值倒轉顯示(切片方式,並非永遠倒轉) --- HW: cartoon=["天竺鼠車車","柯南","迷豆子","魯夫"] x=len(cartoon) while x>0: print("目前在串列的卡通是",cartoon) print("卡通串列的長度是=",x) del cartoon[0] x-=1 print("刪除成功") print("串列內已沒有任何卡通") ![](https://i.imgur.com/4v7G8mc.png) # list 增(append,insert) list=["a","b","c"] list.append(“d”) #預設加在串列最後面 print(list) list=["a","b","c"] list.insert(0,"d") #加在串列的指定位置 print(list) # list 刪(del,pop,remove) list=["a","b","c"] del list[0] print(list) --- list=["a","b","c"] vap_p=list.pop(2) print(vap_p) --- del直接槓掉變數,會直接消失 Pop並沒有槓掉,會保留值在後台 --- #remove串列中若有重複值,只刪除第一個遇到的值 list=["周杰倫","羅志祥","蕭敬騰","羅志祥"] badboy="羅志祥" list.remove(badboy) print(f'被我刪除的偶像是:{badboy},因為太渣了') print(list) # list 改 list=["a","b","c"] list[0]="A” print(list) # list 查 # (list.index("想找的值")) # (count算出現次數) player=["林志傑",20,15,17,31,20,3] maxscore=max(player[1:]) i=player.index(maxscore) print(f"{player[0]}在第{i}場的最高得分是{maxscore}分") //林志傑在第4場的最高得分是31分 //抓出第一個想找尋的值位於串列中的位置 # list mylist_3=["羅志祥","蕭敬騰","江品萱"] num1,num2,num3=mylist_3 print(num1,num2,num3) --- 將句子內逗號的地方視為拆開的依據 將句子內空白的地方視為拆開的依據 ![](https://i.imgur.com/v30PuVH.png) --- # 迴圈 作業:**(改只用一個參數?)** for x in range(1,4): for y in range(1,4): print("(%d, %d)"%(x,y),end=" ") print("") ![](https://i.imgur.com/eFqhacf.png) end=" " 座標與座標之間用空格隔開 print("") 跑出第一個迴圈,進入下一個迴圈前,先換行 --- good=["高","富","帥"] bad=["嚼檳榔","懶惰","衣冠禽獸"] for x in good: for y in bad: print(x,y) --- for digit in range(1,11,2): if digit==5: break print(digit,end=",") --- for val in "string": if val=="i": continue print(val) print("The end") for val in "string": if val=="i": break print(val) print("The end") --- a=50000 r=0.015 for n in range(6): ans=a*(1+r)**n print("第%d年的本利合為:%d"%(n,ans)) n+=1 --- a=50000 r=0.015 n=1 while n<6: ans=a*(1+r)**n print("第%d年的本利合為:%d"%(n,ans)) n+=1 sum=0 for i in range(1,11): sum=sum+i print("加總後為%d"%(sum)) #================================== sum=0 i=1 while (i<11): sum+=i i+=1 print("加總後為%d"%(sum)) --- song_list=["少女時代","everglow","btob"] for i in song_list: print(f'我最喜歡的團體是{i}') for i in range(0,6000,1000): #起始值,終止值,每一次增加多少 (終止值只會做到要求值的上一個) print("This is",i) --- ind=0 while ind<=10: ind+=1 if ind %2: #取餘數的結果為1或0 程式預設1(false)0(true) 所以若解為0(true) 就會往下執行 continue continue print(ind) --- n=1 while n<=5: print("*"*n) n+=1 --- salary=500000 year=1 while salary<=600000: salary=salary*1.05 year+=1 print("第%d年的薪資會超過6000000元"%(year)) --- a="我是鸚鵡,你想說些啥?" b="輸入q可以結束對話" msg=a+"\n"+b+"\n"+"=>" input_mag="" while input_mag!="q": input_mag=input(msg) if input_mag=="q": break print(input_mag) --- a="我是鸚鵡,你想說些啥?" b="輸入q可以結束對話" msg=a+"\n"+b+"\n"+"=>" input_mag="" while input_mag!="q": input_mag=input(msg) if input_mag!="q": print(input_mag) --- a="我是鸚鵡,你想說些啥?" b="輸入q可以結束對話" msg=a+"\n"+b+"\n"+"=>" active=True while active: input_msg=input(msg) if input_msg!="q": print(input_msg) else: active=False --- a="我是鸚鵡,你想說些啥?" b="輸入q可以結束對話" msg=a+"\n"+b+"\n"+"=>" input_mag="" while input_mag!="q": input_msg=input(msg) #將msg秀給使用者,讓使用者知道要輸入甚麼內容 print(input_msg) #將使用者輸入的內容丟入input_msg ![](https://i.imgur.com/3xFOaBk.png) --- i=10 while i>=0: print(i) i-=1 print("Happy new year") ![](https://i.imgur.com/3SAhqhI.png) --- # if else a=input("Please enter your gender?F or M:") b=input("Is python an interpreted language?yes or no?") if(a=="F" or a=="f"): if(b=="yes" or b=="Yes"): print("hey girl you are right") else: print("hey girl you are wrong") else: if(b=="yes" or b=="Yes"): print("hey boy you are right") else: print("hey boy you are wrong") --- a=input("Please enter your year of birth:") x=int(a)-1900 if(x%12==0): print("十二生肖為鼠") elif(x%12==1): print("十二生肖為牛") elif(x%12==2): print("十二生肖為虎") elif(x%12==3): print("十二生肖為兔") elif(x%12==4): print("十二生肖為龍") elif(x%12==5): print("十二生肖為蛇") elif(x%12==6): print("十二生肖為馬") elif(x%12==7): print("十二生肖為羊") elif(x%12==8): print("十二生肖為猴") elif(x%12==9): print("十二生肖為雞") elif(x%12==10): print("十二生肖為狗") else(x%12==11): print("十二生肖為豬") --- a=input('Please enter your score:') score=int(a) if(score>=90 and score<=100): print('Your level is A') elif(score>=80 and score<90): print('Your level is B') elif(score>=70 and score<80): print('Your level is C') elif(score>=60 and score<70): print('Your level is D') else: print('Your level is F') --- a=input('Please enter the first number:') b=input('Please enter the second number:') if(a>b): print('max is',a) else: print('max is',b) --- a=input('pls enter a number:') b=int(a) print(b,'is an even number') if(b%2==0) else print(b,'is an odd number') #method1 if(b%2==0): #method2 print(b,'is an even number') else: print(b,'is an odd number') --- name='霸子' score=100 message=f'我是{name}' print(message) message2=f'我的成績是{score}' print(message2) name='Tiger' score=90 print("{}'s score is {}".format(name,score)) name='Tiger' score=90 print("{0}'s score is {1}".format(name,score)) name='Tiger' score=90 test="{}'s score is {}" print(test.format(name,score)) name='Tiger' score=90 print("%s's score is %d"%(name,score)) --- x=input('Enter your name:') y=input('Enter your math score:') z=input('Enter your english score:') score=int(y)+int(z) print("Hi,"+x+"!"+" Your total score is "+str(score)+".") --- #數字還原成ASCII chr 數字轉字母 ord 字母轉數字 A~Z 65~90 a~z 97~122 0~9 48~57 a=input("Please enter a character:") x=ord(a) if(x>=65 and x<=90): print("輸入值為大寫英文字母") elif(x>=97 and x<=122): print("輸入值為小寫英文字母") elif(x>=48 and x<=57): print("輸入值為數字0~9") else: print("輸入值為特殊字元") --- #關於字串的函式 str="Today is Thursday,it's a boring day!" print(str.split(',')) #遇到逗號就切 print(str.split()) #遇到空格就切 print(str.capitalize()) #第一個字首大寫 print(str.title()) #所有字首大寫 print(str.upper()) #所有轉大寫 print(str.lower()) #所有轉小寫 print(str.swapcase()) #大小寫互轉 --- #替換字元 name="Henny" print(name.replace('H','P')) #method1 print("P"+name[1:]) #method2 print(len(name)) #字串長度 --- #輸入身分證字號,跳出出生地 id=input("請輸入身分證:") place=id[0] if(len(id)==10): if(place=="F"): print("出生地為台北") else: print("wrong") else: print("Your id is wrong") --- #計算兩點距離 #方法1 import math x1,y1=1,8 x2,y2=3,10 a=((x2-x1)**2+(y2-y1)**2) distance=math.sqrt(a) print("兩點的距離是\n",distance) #方法2 x1,y1=1,8 x2,y2=3,10 distance=((x2-x1)**2+(y2-y1)**2)**(1/2) print("兩點的距離是\n",distance) --- #simultaneous assignments x,y=1,2 print("x=",x,"y=",y) x,y=y,x print("x=",x,"y=",y) --- #新的值會把舊的質蓋掉 score1=35 score2=10 score3=10 score2=27 score3=score2 print("第一節分數",score1) #35 print("第二節分數",score2) #27 print("第三節分數",score3) #27 --- #設定環境變數 設定-->環境變數系統設定-->path新增python目標路徑(不要複製"python.exe") 新增成功即可在cmd呼叫python開始執行 變數命名不得數字開頭、保留字、除了"_"其他符號都不行 保留字 help("keyword"): ![](https://i.imgur.com/1WM6UMW.png) --- #得知變數的型態 type() a=1 #int b=2.9 #float c=a+b #float print(type(a)) print(type(b)) print(type(c)) ![](https://i.imgur.com/W5Dht6j.png) --- #計算總分跟平均成績 chi=input("請輸入國文成績:") math=input("請輸入數學成績:") eng=input("請輸入英文成績:") total=int(chi)+int(math)+int(eng) average=int(total/3) print("總分為:"+str(total)) print("平均為:"+str(average)) ![](https://i.imgur.com/zJmVU4R.png) --- #輸入密碼驗證 password=input("請輸入密碼:") if(password=="1234"): print("密碼正確") else: print("密碼錯誤") ![](https://i.imgur.com/TFjzg3u.png) --- #判斷成績等第 score=input("請輸入成績:") if(int(score)>=90): print("優等") elif(int(score)>=80): print("乙等") elif(int(score)>=70): print("丙等") else: print("待加強") ![](https://i.imgur.com/wY9aXwd.png) --- #百貨公司折價戰 pay=int(input("請輸入購買金額:")) if(int(pay)>=10000): if(int(pay)>=50000): pay=str(pay*0.85) elif(int(pay)>=30000): pay=str(pay*0.9) elif(int(pay)>=100000): pay=str(pay*0.8) else: pay=str(pay*0.95) print("折扣後金額:"+str(pay)) else: print(("折扣後金額:"+str(pay))) ![](https://i.imgur.com/zh97IrE.png) --- ## 迴圈 1.for(執行固定次數的迴圈) #計算正整數總和 a=int(input("請輸入整數值:")) #數值設為整數 sum=0 #要設定初值 for s in range(1,a+1): sum+=s print("1到"+str(a)+"的整數和為"+str(sum)) ![](https://i.imgur.com/5htqnDi.png) #巢狀迴圈九九乘法表 a=0 for i in range(1,10): for j in range(1,10): a=i*j print("%d*%d=%-2d " %(i,j,a),end="") print() #內層迴圈執行完後換行 ![](https://i.imgur.com/gGjGw5y.png) #樓層命名(continue) floor=int(input("請輸入樓層數:")) print("本大樓具有的樓層為:") for i in range(1,floor+1): if(i==4): continue print(i,end=" ") print() ![](https://i.imgur.com/00hzd1K.png) #判斷質數(break) a=int(input("請輸入比1大的整數:")) if(a == 2): print(str(a)+"是質數") else: for i in range(2,a): if(a%i==0): print(str(a)+"不是質數") break else: print(str(a)+"是質數") ![](https://i.imgur.com/reaRPFs.png) 2.while(執行非固定次數的迴圈) #計算全班成績 total = person = score = 0 while(score!=-1): person+=1 total+=score score=int(input("請輸入第%d同學成績:" % person)) average=total/(person-1) print("本般總成績為%d分,平均為%5.2f分" %(total,average)) ![](https://i.imgur.com/VeEAFQb.png) #串列初值設定 score=[85,79,93] print("國文成績:"+str(score[0])) print("數學成績:"+str(score[1])) print("英文成績:"+str(score[2])) ![](https://i.imgur.com/nKiNHAJ.png) --- ``` sudo chmod -R 777 ./ ```