python怎么输出数字
python数字运算
Python解释器可以作为一个简单的计算器:您可以在解释器里输入一个表达式,它将输出表达式的值。
表达式的语法很直白:+,-,*和/和在许多其它语言(如Pascal或C)里一样;括号可以用来为运算分组。例如:
>>>2+2
4
>>>50-5*6
20
>>>(50-5*6)/4
5.0
>>>8/5#总是返回一个浮点数
1.6
注意:在不同的机器上浮点运算的结果可能会不一样。之后我们会介绍有关控制浮点运算输出结果的内容。
用Python输出数字,可以直接使用print()语句来直接输出。
在整数除法中,除法(/)总是返回一个浮点数,如果只想得到整数的结果,丢弃可能的分数部分,可以使用运算符//:
>>>17/3#整数除法返回浮点型
5.666666666666667
>>>
>>>17//3#整数除法返回向下取整后的结果
5
>>>17%3#%操作符返回除法的余数
2
>>>5*3+2
17
等号('=')用于给变量赋值。赋值之后,除了下一个提示符,解释器不会显示任何结果。
>>>width=20
>>>height=5*9
>>>width*height
900
Python可以使用**操作来进行幂运算:
>>>5**2#5的平方
25
>>>2**7#2的7次方
128
变量在使用前必须先"定义"(即赋予变量一个值),否则会出现错误:
>>>#尝试访问一个未定义的变量
...n
Traceback(mostrecentcalllast):
File"",line1,in
NameError:name'n'isnotdefined
浮点数得到完全的支持;不同类型的数混合运算时会将整数转换为浮点数:
>>>3*3.75/1.5
7.5
>>>7.0/2
3.5
在交互模式中,最后被输出的表达式结果被赋值给变量_。这能使您在把Python作为一个桌面计算器使用时使后续计算更方便,例如:
>>>tax=12.5/100
>>>price=100.50
>>>price*tax
12.5625
>>>price+_
113.0625
>>>round(_,2)
113.06
此处,_变量应被用户视为只读变量。不要显式地给它赋值——这样您将会创建一个具有相同名称的独立的本地变量,并且屏蔽了这个内置变量的功能。
以上内容为大家介绍了python怎么输出数字,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python中ndarray与list转换的方法
python中ndarray与list转换的方法在python数据类型中,list元素可以任意类型组合,而ndarray元素类型必须相同,但是ndarray可以更方便的对多维...详情>>
2023-11-10 08:51:14
python中OrdereDict如何使用?
python中OrdereDict如何使用?我们在对数据进行处理时,如果能够使它们保存顺序的排序,那么用起来会非常的便利。不过字典本身就是没有顺序的,...详情>>
2023-11-10 07:05:00
python中如何使用scipy.fftpack模块计算快速傅里叶变换?
python中如何使用scipy.fftpack模块计算快速傅里叶变换?在编程中,快速傅里叶变换是工程中非常有价值的一类算法,它可以将时域和频域的信号相互...详情>>
2023-11-10 05:50:23
zip()函数如何在python中遍历多个列表?
zip()函数如何在python中遍历多个列表?下面我们就python中zip的说明、语法、使用注意点进行讲解,然后带来遍历多个列表的实例。1、说明zip()函...详情>>
2023-11-10 05:38:33热门推荐
python concat函数有何用法?
沸python里函数装饰器怎么使用?
热如何实现python中的continue语句?
热python中的冒泡排序是什么?
新python中str函数是什么
如何使用python实现项目进度显示?
python中ndarray与list转换的方法
python复制文件的方法整理
Python中numpy数组如何添加元素
python有几种可视化图形库?
python中xlwings是什么?
python上下文管理器的基本介绍
Python自动化测试基础必备知识点一
python中OrdereDict如何使用?
技术干货






