当前位置:网站首页>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/
边栏推荐
- 2022-06-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】
- 第一章:求同吗小数和s(d, n)
- Upgrade PIP and install Libraries
- 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
- 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
- How to improve data security by renting servers in Hong Kong
- Phpstudy set LAN access
- Chapter 1: sum of three factorials, graph point scanning
- Blue Bridge Cup: the fourth preliminary - "simulated intelligent irrigation system"
- Microservice framework - frequently asked questions
猜你喜欢

Teach you how to quickly recover data by deleting recycle bin files by mistake
![Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles](/img/70/6fd00146418e5d481e951d51428990.png)
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles

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

Explore the internal mechanism of modern browsers (I) (original translation)

02 -- QT OpenGL drawing triangle

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

Change deepin to Alibaba image source

HCIA-USG Security Policy

2022-06-30 網工進階(十四)路由策略-匹配工具【ACL、IP-Prefix List】、策略工具【Filter-Policy】

The 15 year old interviewer will teach you four unique skills that you must pass the interview
随机推荐
P5.js development - setting
PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?
MPLS configuration
第一章:喝汽水,阶梯电费计算,阶梯电费计算函数,个人所税,求解平方根不等式,简化求解平方根不等式,求解调和级数不等式,解不等式:d<1+1/2-1/3+1/4+1/5-1/6+..士1/n
Chapter 1: simplify the same code decimal sum s (D, n)
WPF format datetime in TextBlock- WPF format DateTime in TextBlock?
03 -- QT OpenGL EBO draw triangle
About callback function and hook function
2022-06-25 网工进阶(十一)IS-IS-三大表(邻居表、路由表、链路状态数据库表)、LSP、CSNP、PSNP、LSP的同步过程
Microservice framework - frequently asked questions
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)
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
Wechat applet quick start (including NPM package use and mobx status management)
Network security Kali penetration learning how to get started with web penetration how to scan based on nmap
第一章:求n的阶乘n!
Chapter 1: drinking soft drinks, step tariff calculation, step tariff calculation function, personal income tax, solving square root inequality, simplifying solving square root inequality, solving dem
Leetcode daily question solution: 540 A single element in an ordered array
2022 Xinjiang latest road transportation safety officer simulation examination questions and answers
Explore the internal mechanism of modern browsers (I) (original translation)
Win10 share you don't have permission