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种方法计算给定数字的平方。
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
Python | 计算给定数字的平方(3种不同方式)相关推荐
- python计算给定的日期的星期_Python计算给定日期的周内的某一天
先理一下思路: 1.weekday会根据某个日期返回0到6的一个数字来表示星期几对吧,0==星期一 我们来列一个表:[0,1,2,3,4,5,6] 2.知道了星期几之后,你可以计算出那一周相对于这个0 ...
- C++/python描述 898. 数字三角形 (四种实现方法)
C++/python描述 898. 数字三角形 (四种实现方法) 大家好,我叫亓官劼(qí guān jié ),在CSDN中记录学习的点滴历程,时光荏苒,未来可期,加油~博主目前仅在CSDN中写 ...
- python中的content方法_content最新:python计算Content-MD5并获取文件的Content-MD5值方式_爱安网 LoveAn.com...
关于"content"的最新内容 聚合阅读 这篇文章主要介绍了python计算Content-MD5并获取文件的Content-MD5值方式,具有很好的参考价值,希望对大家有所帮助 ...
- python 计算数字位数,Python | 计算一个数字的总位数
先决条件: Python中的二进制数系统 给定一个数字,我们必须使用Python查找二进制值的总位数来表示该数字. 示例 Input: num = 61 Binary value of 61 is = ...
- python求输入数字的平方、如果平方运算后小于50则退出_Python练习题(三)
44.两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵 import numpy # pip install numpy 需要安装模块 ,支持大量的维度数组与矩阵运算 x = n ...
- python 计算给定日期是该年的第几天数
#coding=utf-8 #写一个函数,计算给定日期是该年的第几天. def count(year,month,day): count = 0 #判断该年是平年还是闰年 if ...
- python请输入_python中的三种输入方式
python中的三种输入方式 python2.X python2.x中以下三个函数都支持: raw_input() input() sys.stdin.readline() raw_input( )将 ...
- python创建单例模式_Python单例模式的四种创建方式实例解析
单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...
- python selenium 等待元素出现_Selenium 3种等待方式
加入等待时间,主要是考虑到网页加载需要时间,可能由于网速慢,或者使用了 ajax 技术实现了异步加载等,如果程序找不到指定的页面元素,就会导致报错发生. 常用的有3种等待方式:强制等待 隐式等待 显示 ...
- python智能模块_Python模块常用的几种安装方式
Python模块安装方法 一.方法1: 单文件模块 直接把文件拷贝到 $python_dir/Lib 二.方法2: 多文件模块,带setup.py 下载模块包,进行解压,进入模块文件夹,执行: pyt ...
最新文章
- 2015级C++第15周程序阅读 范型程序设计
- LeetCode-1423:可获得的最大点数
- 促销海报设计需要的PSD素材|缤纷气球!任意搭配,气氛燃起
- 【Java从0到架构师】Zookeeper - 安装、核心工作机制、基本命令
- 双手无法敲代码的程序员,该如何编程?
- ORACLE功能GREATEST功能说明具体实例
- WPF备忘录(7)WPF图片资源路径介绍
- JSP教程第3讲笔记
- 学生用计算机如何clean,windows installer clean up,教您电脑如何使用清理实用工具
- fanuc换刀宏程序详解_FANUC宏程序使用举例
- 视频质量评价 VMAF,为何让人又喜又忧?
- Win10 UWP版《芒果TV》v2.4.0直播超女,芒果台综艺一网打尽
- matlab 三维数组 二维,matlab三维数组变二维
- 上传本地文件到服务器:not a regular file
- 安卓开发———简易音乐播放器。Timer,SeekBar,mediaPlayer
- Tax Multiplier and Govenment Spending Multiplier
- 顺序表——有序顺序表的插入
- 如何区分MNO和MVNO
- 企业微信之——扫码登录
- 给apple老师建议