發表文章

目前顯示的是有「Python」標籤的文章

【 Python學習 】簡易計算機 calculator

num1 = float ( input ( " 請輸入第一個數字 :" )) op = input ( " 請輸入運算符號 (+-*/):" ) num2 = float ( input ( " 請輸入第二個數字 :" )) # 加法 if op == "+" : plus = num1 + num2 print ( " 結果為 " , plus) # 減法 elif op == "-" : minus = num1 - num2 print ( " 結果為 " , minus) # 乘法 elif op == "*" : multiply = num1 * num2 print ( " 結果為 " , multiply) # 除法 elif op == "/" : divide = num1 / num2 print ( " 結果為 " , divide) else : print ( " 您輸入有誤,無法計算! " ) 請輸入第一個數字:9 請輸入運算符號(+-*/):* 請輸入第二個數字:5.5 結果為 49.5

【 Python學習 】lambda function 匿名函式與 def之比較

# 傳統寫法 def di (n1,n2): return n1*n2 mul = di( 3 , 5 ) print ( " 傳統寫法 " ,mul) # lambda 寫法 multiply = lambda n1,n2:n1*n2 result = multiply( 3 , 5 ) print ( "lambda 寫法 " ,result) 傳統寫法 15 lambda寫法 15

【 Python學習 】裝飾器工廠 Decorator Factory

  # 計算 1+2+3+...+max def calFactory (max): def calculate (cb): def run (): result = 0 for n in range (max+ 1 ): result += n cb(result) #cb(result) 傳遞 result , showing(result) 才能正確處理數值 return run return calculate @calFactory ( 10 ) def showing (result): print ( " 加總結果為 " , result) showing() @calFactory ( 100 ) # 需要加括號,否則會被當作裝飾器函式,而非裝飾器工廠 def showing (result): print ( " 加總結果為 " , result) showing() 加總結果為 55 加總結果為 5050

【 Python學習 】網路連線程式、公開資料串接

  臺北市垃圾車點位路線資訊 資料來源:臺北市資料大平台 https://data.taipei/api/v1/dataset/a6e90031-7ec4-4089-afb5-361a4efe7202?scope=resourceAquire import urllib.request as request import json x = "https://data.taipei/api/v1/dataset/a6e90031-7ec4-4089-afb5-361a4efe7202?scope=resourceAquire" with request.urlopen(x) as response: data = json.load(response) print (data) taipei_list = data[ "result" ][ "results" ] print ( len (taipei_list)) with open ( "taipei_garbage.txt" , "w" , encoding = "utf-8" ) as file: for clist in taipei_list: place = clist.get( " 里別 " ) location = clist.get( " 地點 " ) arrive_time = clist.get( " 抵達時間 " ) leave_time = clist.get( " 離開時間 " ) print ( "------------------------" ) output_data = ( f" { " 里別 " } : { place }\n { " 地點 " } : { location }\n { " 抵達時間 " } : { arrive_time }\n { " 離開時間 ...

【 Python學習 】if else語法應用於四則運算

  # 四則運算 n1 = float ( input ( " 請輸入第一個數字: " )) op = input ( " 請輸入運算符號 (+, -, *, /) : " ) n2 = float ( input ( " 請輸入第二個數字: " )) if op == "+" : print ( " 結果為 :" ,n1 + n2) elif op == "-" : print ( " 結果為 :" ,n1 - n2) elif op == "*" : print ( " 結果為 :" ,n1 * n2) elif op == "/" : print ( " 結果為 :" ,n1 / n2) else : print ( " 不支援此運算 " ) 請輸入第一個數字:9 請輸入運算符號(+, -, *, /):+ 請輸入第二個數字:7.5 結果為: 16.5

【 Python學習 】map 應用台幣美金轉換

# map( 函式 , 可迭代的列表 ) 台 幣美金轉 換 calligraphy = [( ' 七字春聯 ' , 300 ), ( ' 四字春條 ' , 150 ), ( ' 單字斗方 ' , 60 )] print (calligraphy) to_usd = lambda data:(data[ 0 ], data[ 1 ] / 30 ) calligraphy_new = list ( map (to_usd, calligraphy)) print (calligraphy_new) [('七字春聯', 300), ('四字春條', 150), ('單字斗方', 60)] [('七字春聯', 10.0), ('四字春條', 5.0), ('單字斗方', 2.0)]

【 Python學習 】物件導向__init__

class animal: def __init__ ( self , name): self .name = name def run ( self ): print ( self .name + " 正在跑步 " ) def stop ( self ): print ( self .name + " 跑不動了 " ) animal1 = animal( " 老鼠米奇 " ) animal2 = animal( " 小豬佩奇 " ) animal1.run() animal2.stop() 執行結果: 老鼠米奇正在跑步 小豬佩奇跑不動了

【 Python學習 】剪刀石頭布程式 玩家與電腦

import random player = None computer = None running = True options = ( " 剪刀 " , " 石頭 " , " 布 " ) while running: while True : player = input ( " 請輸入剪刀、石頭、布: " ) if player not in options: print ( "input error, try again" ) else : break # 電腦隨機選擇 computer = random.choice(options) print ( f"player: { player } , computer: { computer } " ) # 判斷輸贏 if player == computer: print ( " 平手 " ) elif player == " 剪刀 " and computer == " 布 " : print ( " 玩家勝利 " ) elif player == " 石頭 " and computer == " 剪刀 " : print ( " 玩家勝利 " ) elif player == " 布 " and computer == " 石頭 " : print ( " 玩家勝利 " ) else : print ( " 電腦勝利 " ) player_again = input ( " 再玩一局? (y/n)" ).lower() if not player_again == ...

【 Python學習 】print輸出

print ( "hello world" ) print ( " 你好 " )