本文目錄一覽:
python簡單的函數定義和用法實例
python簡單的函數定義和用法實例
這篇文章主要介紹了python簡單的函數定義和用法,實例分析了Python自定義函數及其使用方法,具有一定參考借鑒價值,需要的朋友可以參考下
具體分析如下:
這裡定義了一個溫度轉換的函數及其用法。
def convertTemp(temp, scale):
if scale == “c”:
return (temp – 32.0) * (5.0/9.0)
elif scale == “f”:
return temp * 9.0/5.0 + 32
temp = int(input(“Enter a temperature: “))
scale = input(“Enter the scale to convert to: “)
converted = convertTemp(temp, scale)
print(“The converted temp is: ” + str(converted))
希望本文所述對大家的Python程序設計有所幫助。
Python函數簡單Demo案例
目錄
一、寫一個函數求三個數的和,並返回結果
二、寫一個函數求三個數的平均值,並返回結果
三、再寫一個函數求每個數與平均值之間的差,並返回結果
python-第十三課-函數實例-萬花筒
本節中的萬花筒通過彩色隨機螺旋線來實現。我們首先定義一個函數draw(),這個函數用來繪製一個螺旋線,函數中的畫筆起始位置的坐標為函數的兩個形參。然後調用函數的時候使用一個for循環來實現多次調用函數,同時,函數的兩個實參由random模塊生成的隨機數組成。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/255147.html