开发者社区> 问答> 正文

Python 平方根

Python 平方根

展开
收起
游客ejnn55cgkof5g 2020-02-14 16:34:35 7634 0
2 条回答
写回答
取消 提交回答
  • # -*- coding: UTF-8 -*-
     
    # Filename : test.py
    # author by : www.runoob.com
     
    num = float(input('请输入一个数字: '))
    num_sqrt = num ** 0.5
    print(' %0.3f 的平方根为 %0.3f'%(num ,num_sqrt))
    
    

    执行以上代码输出结果为:

    $ python test.py 
    请输入一个数字: 4
     4.000 的平方根为 2.000
    
    2020-02-17 13:13:31
    赞同 展开评论 打赏
  • 实例(Python 3.0+)

    -- coding: UTF-8 --

    Filename : test.py

    author by : www.runoob.com

    num = float(input('请输入一个数字: ')) num_sqrt = num ** 0.5 print(' %0.3f 的平方根为 %0.3f'%(num ,num_sqrt)) 执行以上代码输出结果为: $ python test.py 请输入一个数字: 4 4.000 的平方根为 2.000 在该实例中,我们通过用户输入一个数字,并使用指数运算符 ** 来计算该数的平方根。 该程序只适用于正数。负数和复数可以使用以下的方式: 实例(Python 3.0+)

    -- coding: UTF-8 --

    Filename : test.py

    author by : www.runoob.com

    计算实数和复数平方根

    导入复数数学模块

    import cmath

    num = int(input("请输入一个数字: ")) num_sqrt = cmath.sqrt(num) print('{0} 的平方根为 {1:0.3f}+{2:0.3f}j'.format(num ,num_sqrt.real,num_sqrt.imag)) 执行以上代码输出结果为: $ python test.py 请输入一个数字: -8 -8 的平方根为 0.000+2.828j 该实例中,我们使用了 cmath (complex math) 模块的 sqrt() 方法。

    2020-02-14 16:35:51
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载