当前位置:网站首页>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.
边栏推荐
- Parental delegation mechanism
- About callback function and hook function
- 5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes
- Chapitre 1: le roi de shehan a mal calculé
- Chapter 1: simplify the same code decimal sum s (D, n)
- BOC protected phenylalanine zinc porphyrin (Zn · TAPP Phe BOC) / iron porphyrin (Fe · TAPP Phe BOC) / nickel porphyrin (Ni · TAPP Phe BOC) / manganese porphyrin (Mn · TAPP Phe BOC) Qiyue Keke
- About unregistered transfer login page
- P5.js development - setting
- 第二章:4位卡普雷卡数,搜索偶数位卡普雷卡数,搜索n位2段和平方数,m位不含0的巧妙平方数,指定数字组成没有重复数字的7位平方数,求指定区间内的勾股数组,求指定区间内的倒立勾股数组
- Ruby replaces gem Alibaba image
猜你喜欢

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

10 smart contract developer tools that miss and lose

The 15 year old interviewer will teach you four unique skills that you must pass the interview

Detailed and not wordy. Share the win10 tutorial of computer reinstallation system

2022-06-27 advanced network engineering (XII) IS-IS overhead type, overhead calculation, LSP processing mechanism, route revocation, route penetration

Typora charges, WTF? Still need support

Native table - scroll - merge function

第一章:喝汽水,阶梯电费计算,阶梯电费计算函数,个人所税,求解平方根不等式,简化求解平方根不等式,求解调和级数不等式,解不等式:d<1+1/2-1/3+1/4+1/5-1/6+..士1/n

第一章: 舍罕王失算

Chapter 2: 4-digit Kaplan number, search even digit Kaplan number, search n-digit 2-segment sum square number, m-digit ingenious square number without 0, specify the number to form a 7-digit square nu
随机推荐
Network security Kali penetration learning how to get started with web penetration how to scan based on nmap
第一章:简化同码小数和s(d, n)
Chapter 1: sum of three factorials, graph point scanning
BOC protected alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC supplied by Qiyu
2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
NFT without IPFs and completely on the chain?
Pat grade B 1009 is ironic (20 points)
Xctf attack and defense world crypto advanced area best_ rsa
Popularize the basics of IP routing
QT tutorial: signal and slot mechanism
Part 28 supplement (XXVIII) busyindicator (waiting for elements)
Microsoft: the 12th generation core processor needs to be upgraded to win11 to give full play to its maximum performance
PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?
03 -- QT OpenGL EBO draw triangle
Commands related to files and directories
Nerfplusplus parameter format sorting
第一章:拓广同码小数和s(d, n)
Upgrade PIP and install Libraries
Parental delegation mechanism
Part 27 supplement (27) buttons of QML basic elements