当前位置:网站首页>2.3 other data types
2.3 other data types
2022-07-03 19:59:00 【leeshuqing】
This time, we mainly introduce three data types other than integer types , They are Boolean 、 Floating point numbers and strings .
First, let's look at Boolean . This strange name comes from an English mathematician , In fact, the so-called Boolean , There are only two values for this data type , True or false , stay Python Respectively use True and False To express ( Please note that , Like variable names, there is no need to put single quotation marks before and after ), Also known as logical . generally speaking , We seldom use this type directly , More often, Boolean variables are used to retain some logical judgment results .
flag = 1 > 2
print(flag)Output is :False. there flag What is kept is 1 and 2 The result of size comparison , Obviously, the output is False.
The conditions of these logical operations can also be expressed by some logical comparisons . such as :
flag = 1 >= 2
print(flag)
flag = 1 == 2
print(flag)
flag = 1 != 2
print(flag) Output is :
False
False
True
there 1>=2 Express 1 Greater than or equal to 2, Obviously false . It should be noted that , The comparison operators used to judge equality and inequality are == and !=.
What is different from the algebraic operation of integers is , Boolean can perform logical operations , such as and、or and not Respectively represents and 、 Or the three logical operations of sum and non .
flag1 = True
flag2 = False
print(flag1 or flag2 and not flag1)Output is :True. Here because not Operation priority is the highest , therefore not flag1 by False,and Operation priority is next ,False and False Still for False, And finally or operation ,True or False, The result is True.
Floating point numbers are decimals , The name comes from English float point, Floating decimal point , It is more widely used in various scientific and financial calculations , The common way to define it is to use decimal point to express :
num = 1.5
print(num)Output is :1.5.
For particularly large or small decimals , You can also use exponential notation :
num = 1.5e-5
print(num)Output is :1.5e-05. This num The value of is 0.000015, But the output is still exponential . in fact ,Python It will automatically decide to adopt exponential form for particularly large or small decimals .
One thing to note , Due to the limitation of floating-point number storage digits , Inevitably, there is a loss of accuracy , And many scientific calculations need to be repeated , This small loss of accuracy may produce significant errors after accumulation . such as :
num = 1 / 3
print(num)
The operation interface is :

1/3 Actually, it doesn't mean 0.3333333333333333, As long as the storage bits are limited, some decimal places are truncated 3, There will always be errors . But floating point numbers are Python There is indeed a certain obvious error . This kind of error will bring some very strange effects , Later, we will specifically introduce the relevant processing methods , These will be studied in depth after you learn the following knowledge . For example, this method can control the error , Even multiple operations , And higher accuracy :
import decimal
num = decimal.Decimal(1.78)
print(num)
The operation interface is :

Among these three other types , In fact, string is the most common and commonly used type . Why put it last , It is also because there is more content .
The so-called string refers to the whole connected by multiple characters , So strings and characters are in Python There is no essential difference . Strings usually express various text information .
It has been stated that , It is a sequence of characters enclosed in single quotation marks . You can also use double quotation marks , The following expressions are correct :
strs = ' China '
print(strs)
strs = " China "
print(strs)
Why can both kinds of quotation marks be used ? The reason is that there may also be quotation marks in the string , At this time, in order to avoid ambiguity , You can consider using different quotation marks . For example, there are single quotation marks in the string , You must use double quotation marks to represent the string .
strs = "It's a book"
print(strs)Output is :It's a book.
If you still use single quotation marks , Will go wrong ! Because the single quotation mark in the middle of the string incorrectly divides the string .

If you really have two kinds of quotation marks in the string , We can even use three double quotes to represent a string , After all, the possibility of three consecutive double quotes appearing at the same time is very small .
strs = """It's a book"""
print(strs)The operation interface is :

But anyway , We all need to ensure that the types of quotation marks are consistent ,PyCharm And other code editors often automatically fill in the corresponding quotation marks .
Simpler approach , Is to consider using escape characters . The so-called escape character , Is a character marked with a backslash , By default, the system regards this character as the original meaning of the character .
strs = 'It\'s a book'
print(strs)The operation interface is :

Of course, someone will say , So what if there is a backslash in the string ? The answer is that two backslashes mean a real backslash .
strs = 'C:\\temp\\data.dat'
print(strs)Output is :C:\temp\data.dat. This method is often used in the representation of file directories .
Of course , In order to avoid the above-mentioned expressions , Sometimes you can also add a r
strs = r'C:\temp\data.dat'
print(strs)The output content is the same as . At this point, the output is the original character in single quotation marks , Do not use any escape function . But this way of writing does not completely guarantee that there is no ambiguity , Especially in strings mixed with quotation marks , You can observe more by yourself .
Let's look at an output :
strs = 'C:\temp\newData.dat'
print(strs)
The operation interface is :

Can you understand this output ? Here I wanted to output the full file path , But I didn't write the escape character accidentally , But the result is not wrong , But by coincidence, more escape characters are formed , such as \n Means line break ,\t Express Tab Alignment, etc , So this is the result . These escape characters are very useful , It can often express some characters that are difficult to express , Here we are talking about some of the most common escape characters that are basically enough .
Finally, let's take a look at a common beginner's question :
num = 1.23
print('num')
print(num)
Output is :
num
1.23
You can understand it ?num As variable name , Single quotation marks are not allowed before and after , Once added , It's not a variable , It's a string !
Supporting learning resources 、 MOOC video :
Python Big data analysis - Shu Qing Li
https://www.njcie.com/python/
边栏推荐
- Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro
- 第二章:求a,b的最大公约与最小公倍数经典求解,求a,b的最大公约与最小公倍数常规求解,求n个正整数的的最大公约与最小公倍数
- 2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
- Commands related to files and directories
- 2022-06-28 网工进阶(十三)IS-IS-路由过滤、路由汇总、认证、影响ISIS邻居关系建立的因素、其他命令和特性
- 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
- QT tutorial: signal and slot mechanism
- Phpstudy set LAN access
- Micro service knowledge sorting - cache technology
- Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520
猜你喜欢

2022-07-02 网工进阶(十五)路由策略-Route-Policy特性、策略路由(Policy-Based Routing)、MQC(模块化QoS命令行)

Chapter 1: find the algebraic sum of odd factors, find the same decimal sum s (D, n), simplify the same code decimal sum s (D, n), expand the same code decimal sum s (D, n)

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

05 -- QT OpenGL draw cube uniform

BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply

第一章: 舍罕王失算

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

第一章:递归求n的阶乘n!

Sparse matrix (triple) creation, transpose, traversal, addition, subtraction, multiplication. C implementation

Phpstudy set LAN access
随机推荐
Find a line in a file and remove it
Global and Chinese markets for medical temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
交叉编译Opencv带Contrib
Promethus
Phpstudy set LAN access
Chapter 1: find the algebraic sum of odd factors, find the same decimal sum s (D, n), simplify the same code decimal sum s (D, n), expand the same code decimal sum s (D, n)
Ae/pr/fcpx super visual effects plug-in package fxfactory
第一章:拓广同码小数和s(d, n)
2. Template syntax
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
Global and Chinese market of charity software 2022-2028: Research Report on technology, participants, trends, market size and share
FAQs for datawhale learning!
02 -- QT OpenGL drawing triangle
Native table - scroll - merge function
2022 Xinjiang latest road transportation safety officer simulation examination questions and answers
Sword finger offer 30 Stack containing min function
Parental delegation mechanism
PR FAQ: how to set PR vertical screen sequence?
Geek Daily: the system of monitoring employees' turnover intention has been deeply convinced off the shelves; The meta universe app of wechat and QQ was actively removed from the shelves; IntelliJ pla
原生表格-滚动-合并功能