编写一个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))
