寫一個 Python 程序,用算術運算符和函數計算一個數的平方,並舉例說明。
計算數字平方的 Python 程序
這個 Python 程序允許用戶輸入任何數值。接下來,Python 使用算術運算符 找到該數字的平方
# Python Program to Calculate Square of a Number
number = float(input(" Please Enter any numeric Value : "))
square = number * number
print("The Square of a Given Number {0} = {1}".format(number, square))
數字輸出的 Python 平方
Please Enter any numeric Value : 9
The Square of a Given Number 9.0 = 81.0
Python 程序求一個數的平方示例 2
這個 Python 平方的一個數字例子同上。但是,這次我們使用的是指數運算元。
number = float(input(" Please Enter any numeric Value : "))
square = number ** 2
print("The Square of a Given Number {0} = {1}".format(number, square))
Please Enter any numeric Value : 10
The Square of a Given Number 10.0 = 100.0
用函數求一個數的平方的 Python 程序
在這個 Python 程序的例子中,我們定義了一個函數,它返回一個數字的平方。
def square(num):
return num * num
number = float(input(" Please Enter any numeric Value : "))
sqre = square(number)
print("The Square of a Given Number {0} = {1}".format(number, sqre))
原創文章,作者:ZIJW9,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/128654.html