前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Python | 计算给定数字的平方(3种不同方式)

Python | 计算给定数字的平方(3种不同方式)

作者头像
用户7886150
修改于 2021-02-07 02:44:05
修改于 2021-02-07 02:44:05
2.2K0
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: C++程序计算数字的幂

Given a number, and we have to calculate its square in Python

  给定一个数字,我们必须在Python中计算其平方。  

 Example: 

  例:  

     Input:

    Enter an integer numbers: 8

    Output:

    Square of 8 is 64

 Calculating square is a basic operation in mathematics; here we are calculating the square of a given number by using 3 methods. 

  计算平方是数学中的基本运算。 在这里,我们使用3种方法计算给定数字的平方。  

 By multiplying numbers two times: (number*number) 将数字乘以两倍:( 数字*数字) By using Exponent Operator (**): (number**2) 通过使用指数运算符( ** ):( 数字** 2) By using math.pow() method: (math.pow(number,2)  通过使用math.pow()方法: (math.pow(number,2) 

  1)将数字相乘两次:(数字*数字) (1) By multiplying numbers two times: (number*number)) 

 To find the square of a number - simple multiple the number two times. 

  要查找数字的平方-将数字简单乘以两次。  

 Program: 

  程序:  

 # Python program to calculate square of a number

# Method 1 (using  number*number)

# input a number 

number = int (raw_input ("Enter an integer number: "))

# calculate square

square = number*number

# print

print "Square of {0} is {1} ".format (number, square)

 Output 

  输出量  

     Enter an integer number: 8

    Square of 8 is 64 

  2)通过使用指数运算符(**):(数字** 2) (2) By using Exponent Operator (**): (number**2)) 

 The another way is to find the square of a given number is to use Exponent Operator (**), it returns the exponential power. This operator is represented by ** 

  另一种查找给定数字平方的方法是使用指数运算符 ( ** ),它返回指数幂。 该运算符由**表示  

 Example: Statement m**n will be calculated as "m to the power of n". 

  示例:语句m ** n将计算为“ m乘以n 的幂” 。  

 Program: 

  程序:  

 # Python program to calculate square of a number

# Method 2 (using  number**2)

# input a number 

number = int (raw_input ("Enter an integer number: "))

# calculate square

square = number**2

# print

print "Square of {0} is {1} ".format (number, square)

 Output 

  输出量  

     Enter an integer number: 8

    Square of 8 is 64 

    .minHeight{

    min-height: 250px;

    }

    @media (min-width: 1025px){

    .minHeight{

    min-height: 90px;

    }

    }

    .minHeight{

    min-height: 250px;

    }

    @media (min-width: 1025px){

    .minHeight{

    min-height: 90px;

    }

    }

  3)通过使用math.pow()方法:(math.pow(number,2) (3) By using math.pow() method: (math.pow(number,2)) 

 pow(m,n) is an inbuilt method of math library, it returns the value of "m to the power n". To use this method, we need to import math library in the program. 

  pow(m,n)是数学库的一种内置方法,它将“ m的值返回给幂n” 。 要使用此方法,我们需要在程序中导入数学库。  

 The statement to import math library is import math. 

  导入数学库的语句是import math 。  

 Program: 

  程序:  

 # Python program to calculate square of a number

# Method 3 (using math.pow () method)

# importing math library 

import math 

# input a number 

number = int (raw_input ("Enter an integer number: "))

# calculate square

square = int(math.pow (number, 2))

# print

print "Square of {0} is {1} ".format (number, square)

 Output 

  输出量  

     Enter an integer number: 8

    Square of 8 is 64 

  Recommended posts 

   推荐的帖子  

  Python | Write functions to find square and cube of a given number. Python | 编写函数以查找给定数字的平方和立方。 Python program to find the power of a number using loop Python程序使用循环查找数字的幂 Python program to find the power of a number using recursion Python程序使用递归查找数字的幂 Python program to find addition of two numbers (4 different ways) Python程序查找两个数字的加法(4种不同方式) Python | Find factorial of a given number (2 different ways). Python | 查找给定数字的阶乘(2种不同方式)。 Python program for simple interest 简单感兴趣的Python程序 Python program for compound interest 复利的Python程序 Python program to check the given year is a leap year or not 检查给定年份是否为a年的Python程序 Simple pattern printing programs in Python Python中的简单图案打印程序 Create a function to check EVEN or ODD in Python 创建一个函数来检查Python中的偶数或奇数 Create a function to return the absolute the given value in Python 创建一个函数以在Python中返回给定的绝对值 Python program to find power of a number using exponential operator Python程序使用指数运算符查找数字的幂 Python program to reverse a given number (2 different ways) Python程序反转给定数字(2种不同方式) Python program to find floor division Python程序查找地板划分 Python | Calculate discount based on the sale amount. Python | 根据销售额计算折扣。 Python | Calculate discount based on the sale amount using Nested if else. Python | 如果不是,则使用嵌套计算基于销售额的折扣。 Python | Design a simple calculator using if elif (just like switch case) Python | 使用if elif设计一个简单的计算器(就像开关盒一样) Python | Find the factorial of a number using recursion Python | 使用递归找到数字的阶乘 Python | Compute the net amount of a bank account based on the transactions. Python | 根据交易计算银行帐户的净额。 Python program to check prime number Python程序检查素数 

  翻译自: https://www.includehelp.com/python/calculate-square-of-a-given-number.aspx

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
kotlin 判断数字_Kotlin程序显示范围之间的阿姆斯壮数字
An Armstrong number is a number such that the sum of the nth power of its digits is equal to the number itself, where n is the number of digits in the number (taken here to mean positive integer).
用户7886150
2021/04/28
1.2K0
python程序执行时间_用于在Python中查找程序执行时间的程序
The execution time of a program is defined as the time spent by the system to execute the task. As we all know any program takes some execution time but we don't know how much. So, don't worry, in this tutorial we will learn it by using the datetime module and also we will see the execution time for finding the factorial of a large number. A large number will be provided by the user and we have to calculate the factorial of a number, also we have to find the execution time of the factorial program. Before going to write the Python program, we will try to understand the algorithm.
用户7886150
2021/01/28
2.2K0
kotlin 两个数字相加_Kotlin程序查找两个数字的LCM
LCM stands for the "Least Common Multiple" / "Lowest Common Multiple", or can also be said "Smallest Common Multiple". LCM is the smallest positive integer that is divisible by both numbers (or more).
用户7886150
2021/01/28
2.3K0
【Python入门第六讲】贴近生活数据类型 | 数字
在编程中,经常使用数字来记录、可视化数据、存储Web应用等...。 Python根据数字的用法,以不同的方式处理它们。
不惑
2024/01/25
2340
【Python入门第六讲】贴近生活数据类型 | 数字
数值的整数次方
在JavaScript中有一个库函数(Math.pow())可以对一个数进行次方运算,本文将实现一个类似pow功能的函数,欢迎各位感兴趣的开发者阅读本文。
神奇的程序员
2022/04/10
5850
数值的整数次方
Python数据分析(中英对照)·Modules and Methods 模块和方法
1.1.3: Modules and Methods 模块和方法 让我们谈谈模块。 Let’s talk a little bit about modules. Python模块是代码库,您可以使用import语句导入Python模块。 Python modules are libraries of code and you can import Python modules using the import statements. 让我们从一个简单的案例开始。 Let’s start with
数媒派
2022/12/01
3950
Python编程 数值类型 数学计算
对于数学计算,除了前面提到过的简单的加减乘除等等,更多的科学计算需要 导入 math 这个标准库(不需要安装,但是要导入),它包含了绝大多数我们可能需要的科学计算函数。
网络豆
2022/11/20
9080
【Python】Math--数学函数(详细附解析~)
注意 frexp() 和 modf() 具有与它们的C等价函数不同的调用/返回模式:它们采用单个参数并返回一对值,而不是通过 '输出形参' 返回它们的第二个返回参数(Python中没有这样的东西)。
小言从不摸鱼
2024/09/10
3100
Python 实战:猜数字与智能计算
经过前面几篇文章对 Python 的学习,我们已经掌握了一些编程的基础知识。现在我们来完成一篇实战文章。可能有些知识还没有讲到过,但我相信聪明的你一定能够理解,并可以通过查找资料来将知识点牢牢掌握。在本文中,通过两个有趣的案例——“猜数字”和“智能计算”,带您走进计算的奇妙世界。
剑指工控
2023/11/29
1860
03 python -数字 math
str(x )                 将对象 x 转换为字符串 string
用户7886150
2020/12/24
1K0
Python Number(数字)
数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的值,将重新分配内存空间。
小小工匠
2021/08/16
1.3K0
Python基础练习100题 ( 21
这十道题的代码在我的github上,如果大家想看一下每道题的输出结果,可以点击以下链接下载:
py3study
2020/01/06
5230
《Python基础教程》第六章--读书
本章会介绍如何将语句组织成函数。还会详细介绍参数(parameter)和作用域(scope)的概念,以及递归的概念及其在程序中的用途。
py3study
2020/01/03
7620
Python 例题 -- 巴比伦平方根算
  5.判断新的猜测值和原猜测值是否相同,相同则跳转至步骤2,不同则该猜测值为原数平方根
py3study
2020/01/09
1.4K0
Python基本类型
     Python是一门动态语言,解释执行,所有错误都是运行时产生的,即使有错误和异常,只要没有被执行到也不会有错,比如调用不存在的方法;类型是隐式的,也即无需变量类型声明;类型是动态,运行时根据变量指向的内容来决定类型,但是Python是强类型语言,即每个变量都是有类型的。
py3study
2020/01/06
9390
POJ 2209 The King(简单贪心)
The King Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7499 Accepted: 4060 Description Once upon a time in a country far away lived a king and he had a big kingdom. He was a very clever king but he had one weakness -- he could co
Angel_Kitty
2018/04/08
6160
《牛客网刷题之零基础入门前端之JavaScript》
请补全JavaScript函数,要求以字符串的形式返回参数的类型。 注意:只需检测基本数据类型。
叶茂林
2023/07/30
2490
Python中的函数式编程教程,学会用一行代码搞定
在本文中,您将了解什么是函数范型,以及如何在Python中使用函数式编程。在Python中,函数式编程中的map和filter可以做与列表相同的事情。这打破了Python的禅宗规则之一,因此函数式编程的这些部分不被认为是“Python式的”。但是由于函数式编程高阶编程的必经之路,所以我们需要了解甚至熟练掌握。
HuangWeiAI
2020/05/01
1.3K0
Python高频写法总结:精简代码,提高效率
今天为大家分享 Python高频写法总结:精简代码,提高效率,全文3400字,阅读大约12分钟。
老表
2023/12/13
3650
Python高频写法总结:精简代码,提高效率
30个神奇的Python技巧来编写更好的代码(上)
Python因其代码的简单性和可读性而成为一种非常流行的语言。 它是您选择的最简单的语言之一。 如果您是python基本概念的初学者,那么这是学习编写更好代码的最佳时间。
陈晨135
2021/12/17
6660
30个神奇的Python技巧来编写更好的代码(上)
相关推荐
kotlin 判断数字_Kotlin程序显示范围之间的阿姆斯壮数字
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档