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: 8Square 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: 8Square of 8 is 64
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: 8Square 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