首页 > 编程知识 正文

Python 程序:计算数字平方

时间:2023-11-24 15:33:37 阅读:308795 作者:PYXC

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

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。