当前位置:网站首页>[Python 1-6] Python tutorial 1 -- number
[Python 1-6] Python tutorial 1 -- number
2020-11-08 12:11:00 【osc_hq8y47x】
A number is a written symbol used to represent a number :
Chinese numeral writing 37 、 Thirty seven
Roman numeral writing XXXVII
Arabic numeral binary writing 100101
<!--more-->
The first address of the article
Numbers
In programming , Numbers are often used to record game scores 、 Represent visual data 、 Storage Web Application information, etc .Python Deal with numbers in different ways according to their usage . We usually use integers , Let's take a look at Python How to manage integers .
Integers
stay Python in , You can add integers + reduce - ride * except / operation .
>>> 2 + 3
5
>>> 3 - 2
1
>>> 2 * 3
6
>>> 3 / 2
1.5
At terminal ( Command line ) In the session ,Python Directly return the result of operation .Python Use two multipliers to denote the multiplication operation :
>>> 3 ** 2
9
>>> 3 ** 3
27
>>> 10 ** 6
1000000
Python It also supports the order of operations , So you can use multiple operations in the same expression . Of course, we can also use parentheses to modify the order of operations , Give Way Python Perform operations in the order you specify , As shown below :
>>> 2 + 3*4
14
>>> (2 + 3) * 4
20
Floating point numbers
Python All numbers with decimal points are called floating point numbers . Most programming languages use this term , It points out that A fact : The decimal point can appear anywhere in the number .
Every programming language has to be carefully designed , To handle floating-point numbers properly , Make sure that no matter where the decimal point appears , The behavior of numbers is normal .
To a large extent , When you use floating-point numbers, you don't have to think about their behavior . You just type in the numbers you want to use ,Python They are usually handled the way you want them to be :
>>> 0.1 + 0.1
0.2
>>> 0.2 + 0.2 9 0.4
>>>2 * 0.1
0.2
>>>2 * 0.2
0.4
But it should be noted that , The number of decimal places contained in the result may be uncertain :
>>> 0.2 + 0.1
0.30000000000000004
>>> 3 * 0.1
0.30000000000000004
All languages have this problem , There is nothing to worry about .Python Will try to find a way , As much as possible accurate To express the result , But because of the way numbers are represented inside computers , It's hard in some cases . For now , Just ignore the extra decimal places .
Using functions str() Avoid typos
You often need to use variable values in messages . for example , Suppose you want to wish someone a happy birthday , You might write code like this ( Save the following code as birthday.py):
age = 23
message = "Happy " + age + "rd Birthday!"
print(message)
You might think , The above code will print a simple birthday greeting :Happy 23rd birthday!. But if you run the code , They will be found to cause errors :
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
It's a typological mistake , signify Python Unable to identify the information you are using . In this example ,Python Find out that you make Used a value as an integer (int) The variable of , But it doesn't know how to interpret the value ( see ).Python know , This change A quantity may be a numerical value 23, It could also be a character 2 and 3. When you use integers in a string like above , It needs to be explicitly referred to Out of your hope Python Use this integer as a string . So , Callable functions str(), It makes Python Represents a non string value For the string :
age = 23
message = "Happy " + str(age) + "rd Birthday!"
print(message)
such ,Python You know you're going to put the number 23 Convert to string , And then display the characters in the birthday message 2 and 3. the After the above treatment , The message you expect will be displayed , It doesn't lead to mistakes :
Happy 23rd Birthday!
Most of the time , stay Python It's very simple to use numbers in . If the result is unexpected , Please check Python Press or not The way you want to interpret numbers as numbers or strings .
Homework
6-1 To write 4 Expression , They use addition respectively 、 Subtraction 、 Multiplication and division , But the results are all numbers8.
For the use of print Statement to display the result , Be sure to enclose these expressions in brackets , in other words , You should write 4 Line is similar to the following code :print(5 + 3)
The output should be 4 That's ok , Each row contains only numbers 8. Name iteight.py
6-2 Store your favorite number in a variable , Use this variable to create a message , Point out your favorite number , And then print this message out . Name itnumber.py
If you want to check your homework answers, you can go to my Githu Warehouse

版权声明
本文为[osc_hq8y47x]所创,转载请带上原文链接,感谢
边栏推荐
- android基础-CheckBox(复选框)
- Harbor项目高手问答及赠书活动
- 最全!阿里巴巴经济体云原生实践!(附网盘链接)
- OR Talk NO.19 | Facebook田渊栋博士:基于蒙特卡洛树搜索的隐动作集黑盒优化 - 知乎
- 渤海银行百万级罚单不断:李伏安却称治理完善,增速呈下滑趋势
- 后端程序员必备:分布式事务基础篇
- It's worth seeing! EMR elastic low cost offline big data analysis best practice (with network disk link)
- Windows10关机问题----只有“睡眠”、“更新并重启”、“更新并关机”,但是又不想更新,解决办法
- Don't look! Full interpretation of Alibaba cloud's original data lake system! (Internet disk link attached)
- 你的云服务器可以用来做什么?云服务器有什么用途?
猜你喜欢

第二次作业

年轻一代 winner 的程序人生,改变世界的起点藏在身边

The most complete! Alibaba economy cloud original practice! (Internet disk link attached)

笔试面试题目:求丢失的猪

Share the experience of passing the PMP examination

虚拟机中安装 macOS 11 big sur

阿里撕下电商标签

Adobe Lightroom / LR 2021 software installation package (with installation tutorial)

This time Kwai tiktok is faster than shaking.

运维人员常用到的 11 款服务器监控工具
随机推荐
优化if-else代码的八种方案
Flink从入门到真香(10、Sink数据输出-Elasticsearch)
Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
Installing MacOS 11 Big Sur in virtual machine
为什么 Schnorr 签名被誉为比特币 Segwit 后的最大技术更新
漫画|讲解一下如何写简历&项目
Mozi college SQL injection solution
YGC troubleshooting, let me rise again!
python基础教程python opencv pytesseract 验证码识别的实现
你的云服务器可以用来做什么?云服务器有什么用途?
Adobe Lightroom / LR 2021 software installation package (with installation tutorial)
This year's salary is 35W +! Why is the salary of Internet companies getting higher and higher?
新的目标市场在哪里?锚定的产品是什么?| 十问2021中国企业服务
Major changes in Huawei's cloud: Cloud & AI rises to Huawei's fourth largest BG with full fire
Tight supply! Apple's iPhone 12 power chip capacity exposed
为 Docsify 自动生成 RSS 订阅
当Kubernetes遇到机密计算,看阿里巴巴如何保护容器内数据的安全!(附网盘链接)
AQS解析
Win10 Terminal + WSL 2 安装配置指南,精致开发体验
供货紧张!苹果被曝 iPhone 12 电源芯片产能不足