当前位置:网站首页>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/
边栏推荐
- 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)
- 第一章:喝汽水,阶梯电费计算,阶梯电费计算函数,个人所税,求解平方根不等式,简化求解平方根不等式,求解调和级数不等式,解不等式:d<1+1/2-1/3+1/4+1/5-1/6+..士1/n
- 交叉编译Opencv带Contrib
- 01 - QT OpenGL display OpenGL window
- Day10 ---- 强制登录, token刷新与jwt禁用
- Day11 ---- 我的页面, 用户信息获取修改与频道接口
- Leetcode daily question solution: 540 A single element in an ordered array
- 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-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
- Leetcode 1189. Maximum number of balloons (special character count)
猜你喜欢
Detailed and not wordy. Share the win10 tutorial of computer reinstallation system
Chapter 2: find the number of daffodils based on decomposition, find the number of daffodils based on combination, find the conformal number in [x, y], explore the n-bit conformal number, recursively
PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?
Chapter 1: simplify the same code decimal sum s (D, n)
Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520
Part 28 supplement (XXVIII) busyindicator (waiting for elements)
IP address is such an important knowledge that it's useless to listen to a younger student?
Native table - scroll - merge function
第二章:基于分解的求水仙花数,基于组合的求水仙花数, 兰德尔数,求[x,y]内的守形数,探求n位守形数,递推探索n位逐位整除数
03 -- QT OpenGL EBO draw triangle
随机推荐
Chapter 1: simplify the same code decimal sum s (D, n)
Commands related to files and directories
BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply
2022-06-30 网工进阶(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
03 -- QT OpenGL EBO draw triangle
2. Template syntax
Day11 ---- 我的页面, 用户信息获取修改与频道接口
4. Data splitting of Flink real-time project
Global and Chinese market of two in one notebook computers 2022-2028: Research Report on technology, participants, trends, market size and share
2022 Xinjiang latest road transportation safety officer simulation examination questions and answers
Leetcode daily question solution: 540 A single element in an ordered array
Part 28 supplement (XXVIII) busyindicator (waiting for elements)
Find a line in a file and remove it
2022-07-02 网工进阶(十五)路由策略-Route-Policy特性、策略路由(Policy-Based Routing)、MQC(模块化QoS命令行)
BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply
Explore the internal mechanism of modern browsers (I) (original translation)
PR 2021 quick start tutorial, how to create a new sequence and set parameters?
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
Promethus
Sword finger offer 30 Stack containing min function