当前位置:网站首页>2.4 conversion of different data types
2.4 conversion of different data types
2022-07-03 20:00:00 【leeshuqing】
We often need to convert between different data types . Common conversions are centered on integers , Let's look at integers and three other types of conversions .

Let's first look at how to convert from floating-point numbers to integers . There are generally two basic transformations : One is called rounding , That is, only the whole number of decimals is retained , Do not automatically round , You can use int Function .
num = 1.65
print(int(num))Output is :1. Of course , Rounding also has different forms , For example, you can also round up ,1.65 Rounding off 2. Then we need to use ceil function ,ceil It means ceiling , It's very figurative , That is, the smallest integer greater than the floating-point number .
import math
num = 1.05
print(math.ceil(num))Output is :2. This is the first time we use functions in other modules . stay Python in , In addition to various standard ready-made functions , We can also import other modules , Like here math modular ( Mathematical calculation module ), Get more functions , The way to use it is to add the name of the import module before the function name .
however , What if I can't remember clearly or it's too troublesome to write ? About how to be in PyCharm Auto import library , Here are PyCharm An important shortcut key for , namely “Alt+Enter”, If there is no import of necessary modules in the code , Can be in PyCharm Click... At the relevant code in the editing environment , And press this shortcut key , The system will prompt :

choice “Import this name( Import this name )” You can further select the required module , Enter again to confirm , You can automatically complete the writing of the import statement .

The second is called rounding , have access to round Function :
num = 1.65
print(round(num))Output is :2.
Let's look at a strange question :
print(round(1.65, 1))
print(round(1.75, 1)) Output is :
1.6
1.8
There's a detail to note here , Round to the nearest Python In the “ Round to the nearest pair ” The rules of , A more balanced carry design can be achieved , Five into pairs means that if the high order is singular, it will go forward 1, If the high order is an even number, there is no carry . So it produces the above effect .
Let's look at converting from integer to floating point number . This is simpler .
num = 2
print(float(num))Output is :2.0.
float Function can not only convert integers to floating-point numbers , For any other type that can be converted , For example, strings can also be converted :
num = '3.14'
print(float(num))Output is :3.14.
In numerical calculation , Integers and floating-point numbers are often mixed , here Python By default, integers are automatically converted to floating-point numbers .
r = 2
pi = 3.14
print(r * r * pi)Output is :12.56, It can be used to express the calculation of circle area .
More precise pi You can use it directly math Modular pi Constant values :
import math
r = 2
print(r * r * math.pi)Output is :12.566370614359172. The numerical accuracy of the output results is higher .
In fact, Boolean value is also an integer in storage , Fake is 0, True or false 0, Usually, the 1 Express . Let's watch two exercises :
flag = 1 > 2
print(flag + 1)Output is :1.
num = 1
print(num + True)Output is :2.
so , Boolean and integer can indeed be mixed . Of course, we don't recommend that you do this , But in later code , When it comes to true or false judgment , Remember 0 It's fake . Indicates that the judgment conditions are not met , No 0 It means true , But the other way around , False correspondence 0, And truth only corresponds to 1.
边栏推荐
- 第一章:递归求n的阶乘n!
- Chapter 1: find all factorial sums, Grand Prix site unified programming, three factorial sums, graphic point scanning, recursive factorial n of n!, Find the factorial n of n!, King Shehan miscalculate
- Rd file name conflict when extending a S4 method of some other package
- Chapter 2: find the box array, complete number in the specified interval, and improve the complete number in the specified interval
- CMD implements the language conversion of locale non Unicode programs
- 5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes
- 第一章:求奇因数代数和,求同吗小数和s(d, n),简化同码小数和s(d, n),拓广同码小数和s(d, n)
- Detailed explanation of shuttle unity interworking principle
- Virtual machine installation deepin system
- 2022-06-28 advanced network engineering (XIII) IS-IS route filtering, route summary, authentication, factors affecting the establishment of Isis neighbor relations, other commands and characteristics
猜你喜欢

Wechat applet quick start (including NPM package use and mobx status management)

5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes

FPGA 学习笔记:Vivado 2019.1 工程创建

Chapter 1: simplify the same code decimal sum s (D, n)

Ae/pr/fcpx super visual effects plug-in package fxfactory

第一章: 舍罕王失算

Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!

Commands related to files and directories

2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)

AcWing 1460. Where am i?
随机推荐
Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
6. Data agent object Defineproperty method
2022 Xinjiang latest construction eight members (standard members) simulated examination questions and answers
第一章:三位阶乘和数,图形点扫描
BOC protected amino acid porphyrins TAPP ala BOC, TAPP Phe BOC, TAPP Trp BOC, Zn · TAPP ala BOC, Zn · TAPP Phe BOC, Zn · TAPP Trp BOC Qiyue
Chapitre 1: le roi de shehan a mal calculé
05 -- QT OpenGL draw cube uniform
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
交叉编译Opencv带Contrib
Global and Chinese market of two in one notebook computers 2022-2028: Research Report on technology, participants, trends, market size and share
Chapter 1: simplify the same code decimal sum s (D, n)
02 -- QT OpenGL drawing triangle
Meso tetra [P - (p-n-carbazole benzylidene imino)] phenylporphyrin (tcipp) /eu (tcipp) [pc( α- 2-oc8h17) 4] and euh (tcipp) [pc (a-2-oc8h17) 4] supplied by Qiyue
Bright purple crystal meso tetra (4-aminophenyl) porphyrin tapp/tapppt/tappco/tappcd/tappzn/tapppd/tappcu/tappni/tappfe/tappmn metal complex - supplied by Qiyue
2022-06-27 网工进阶(十二)IS-IS-开销类型、开销计算、LSP的处理机制、路由撤销、路由渗透
Realize user registration and login
How to check the permission to write to a directory or file- How do you check for permissions to write to a directory or file?
2022-06-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
2022-06-25 advanced network engineering (XI) IS-IS synchronization process of three tables (neighbor table, routing table, link state database table), LSP, cSNP, psnp, LSP
01 - QT OpenGL display OpenGL window