当前位置:网站首页>【Python 1-6】Python教程之——数字
【Python 1-6】Python教程之——数字
2020-11-08 12:11:00 【osc_wip0vvls】
数字是一种用来表示数的书写符号:
中文数字写作三十七、卅七
罗马数字写作XXXVII
阿拉伯数字二进制写作100101
<!--more-->
文章首发地址
数字
在编程中,经常使用数字来记录游戏得分、表示可视化数据、存储Web应用信息等。Python 根据数字的用法以不同的方式处理它们。我们平时最常见的就是整数,下面就先来看看Python是如何管理整数的。
整数
在Python中,可对整数执行加+
减-
乘*
除/
运算。
>>> 2 + 3
5
>>> 3 - 2
1
>>> 2 * 3
6
>>> 3 / 2
1.5
在终端(命令行)会话中,Python直接返回运算结果。Python使用两个乘号表示乘方运算:
>>> 3 ** 2
9
>>> 3 ** 3
27
>>> 10 ** 6
1000000
Python还支持运算次序,因此你可在同一个表达式中使用多种运算。当然我们也可以使用括号来修改运算次序,让Python按你指定的次序执行运算,如下所示:
>>> 2 + 3*4
14
>>> (2 + 3) * 4
20
浮点数
Python将带小数点的数字都称为浮点数。大多数编程语言都使用了这个术语,它指出了这样 一个事实:小数点可出现在数字的任何位置。
每种编程语言都须细心设计,以妥善地处理浮点数, 确保不管小数点出现在什么位置,数字的行为都是正常的。
从很大程度上说,使用浮点数时都无需考虑其行为。你只需输入要使用的数字,Python通常都会按你期望的方式处理它们:
>>> 0.1 + 0.1
0.2
>>> 0.2 + 0.2 9 0.4
>>>2 * 0.1
0.2
>>>2 * 0.2
0.4
但需要注意的是,结果包含的小数位数可能是不确定的:
>>> 0.2 + 0.1
0.30000000000000004
>>> 3 * 0.1
0.30000000000000004
所有语言都存在这种问题,没有什么可担心的。Python会尽力找到一种方式,以尽可能精确地表示结果,但鉴于计算机内部表示数字的方式,这在有些情况下很难。就现在而言,暂时忽略多余的小数位数即可。
使用函数str()避免类型错误
你经常需要在消息中使用变量的值。例如,假设你要祝别人生日快乐,可能会编写类似于下面的代码(将下面代码保存为birthday.py
):
age = 23
message = "Happy " + age + "rd Birthday!"
print(message)
你可能认为,上述代码会打印一条简单的生日祝福语:Happy 23rd birthday!
。但如果你运行这些代码,将发现它们会引发错误:
Traceback (most recent call last):
File "birthday.py", line 2, in <module>
message = "Happy " + age + "rd Birthday!"
TypeError: Can't convert 'int' object to str implicitly
这是一个类型错误,意味着Python无法识别你使用的信息。在这个示例中,Python发现你使 用了一个值为整数(int)的变量,但它不知道该如何解读这个值(见)。Python知道,这个变 量表示的可能是数值23,也可能是字符2和3。像上面这样在字符串中使用整数时,需要显式地指 出你希望Python将这个整数用作字符串。为此,可调用函数str(),它让Python将非字符串值表示 为字符串:
age = 23
message = "Happy " + str(age) + "rd Birthday!"
print(message)
这样,Python就知道你要将数值23转换为字符串,进而在生日祝福消息中显示字符2和3。经 过上述处理后,将显示你期望的消息,而不会引发错误:
Happy 23rd Birthday!
大多数情况下,在Python中使用数字都非常简单。如果结果出乎意料,请检查Python是否按 你期望的方式将数字解读为了数值或字符串。
小作业
6-1 编写 4 个表达式,它们分别使用加法、减法、乘法和除法运算,但结果都是数字8
。
为使用 print 语句来显示结果,务必将这些表达式用括号括起来,也就是说,你应该编写4行类似于下面的代码:print(5 + 3)
输出应为 4 行,其中每行都只包含数字8。命名为eight.py
6-2 将你最喜欢的数字存储在一个变量中,再使用这个变量创建一条消息,指出你最喜欢的数字,然后将这条消息打印出来。命名为number.py
想查看作业答案可以去我的Githu仓库
版权声明
本文为[osc_wip0vvls]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4271269/blog/4708084
边栏推荐
- Bohai bank million level fines continue: Li Volta said that the governance is perfect, the growth rate is declining
- On the confirmation of original data assets
- 2 days, using 4 hours after work to develop a test tool
- Hematemesis! Alibaba Android Development Manual! (Internet disk link attached)
- If you don't understand the gap with others, you will never become an architect! What's the difference between a monthly salary of 15K and a monthly salary of 65K?
- 虚拟机中安装 macOS 11 big sur
- Python Gadgets: code conversion
- Why is Schnorr Signature known as the biggest technology update after bitcoin segwit
- 2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
- Istio traffic management -- progress gateway
猜你喜欢
Harbor项目高手问答及赠书活动
VC + + specified directory file output by time
Istio流量管理--Ingress Gateway
On the confirmation of original data assets
Q & A and book giving activities of harbor project experts
Python basic syntax
2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...
This paper analyzes the top ten Internet of things applications in 2020!
Rust : 性能测试criterion库
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
随机推荐
一个方案提升Flutter内存利用率
BCCOIN告诉您:年底最靠谱的投资项目是什么!
How TCP protocol ensures reliable transmission
为 Docsify 自动生成 RSS 订阅
Service architecture and transformation optimization process of e-commerce trading platform in mogujie (including ppt)
分布式文档存储数据库之MongoDB基础入门
阿里教你深入浅出玩转物联网平台!(附网盘链接)
解析Istio访问控制
Ubuntu20.04 access FTP server garbled problem + upload files
Or talk No.19 | Facebook Dr. Tian Yuandong: black box optimization of hidden action set based on Monte Carlo tree search
Entry level! Teach you how to develop small programs without asking for help (with internet disk link)
laravel8更新之速率限制改进
不多不少,大学里必做的五件事(从我的大一说起)
Understanding design patterns
PMP experience sharing
Web novice problem of attacking and defending the world
Harbor项目高手问答及赠书活动
Python basic syntax
仅用六种字符来完成Hello World,你能做到吗?
在51CTO学院Get到PMP证书