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

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种方法计算给定数字的平方。

  1. By multiplying numbers two times: (number*number)

    将数字乘以两倍:( 数字*数字)

  2. By using Exponent Operator (**): (number**2)

    通过使用指数运算符( ** ):( 数字** 2)

  3. 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<

你可能感兴趣的:(python,java,算法,mysql,c++)