当前位置:网站首页>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/
边栏推荐
- 第一章:简化同码小数和s(d, n)
- About unregistered transfer login page
- Sparse matrix (triple) creation, transpose, traversal, addition, subtraction, multiplication. C implementation
- unittest框架基本使用
- Day10 ---- 强制登录, token刷新与jwt禁用
- 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
- Rad+xray vulnerability scanning tool
- Basic command of IP address configuration ---ip V4
- 2022 Xinjiang latest road transportation safety officer simulation examination questions and answers
- Unittest framework is basically used
猜你喜欢

BOC protected tryptophan zinc porphyrin (Zn · TAPP Trp BOC) / copper porphyrin (Cu · TAPP Trp BOC) / cobalt porphyrin (cobalt · TAPP Trp BOC) / iron porphyrin (Fe · TAPP Trp BOC) / Qiyue supply

2022 Xinjiang latest construction eight members (standard members) simulated examination questions and answers

PR 2021 quick start tutorial, how to create a new sequence and set parameters?

Chapter 2: find the classical solution of the maximum Convention and the least common multiple of a and B, find the conventional solution of the maximum Convention and the least common multiple of a a

Upgrade PIP and install Libraries

Chapter 2: find the box array, complete number in the specified interval, and improve the complete number in the specified interval

AcWing 1460. Where am i?

Make a simple text logo with DW

Point cloud data denoising

BOC protected alanine porphyrin compound TAPP ala BOC BOC BOC protected phenylalanine porphyrin compound TAPP Phe BOC Qi Yue supply
随机推荐
Micro service knowledge sorting - three pieces of micro Service Technology
Bool blind note - score query
原生表格-滚动-合并功能
Chapter 2: find the box array, complete number in the specified interval, and improve the complete number in the specified interval
IP address is such an important knowledge that it's useless to listen to a younger student?
Rad+xray vulnerability scanning tool
Point cloud data denoising
Teach you how to quickly recover data by deleting recycle bin files by mistake
Win10 share you don't have permission
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
Day10 -- forced login, token refresh and JWT disable
4. Data splitting of Flink real-time project
Global and Chinese market of high purity copper foil 2022-2028: Research Report on technology, participants, trends, market size and share
BOC protected tryptophan zinc porphyrin (Zn · TAPP Trp BOC) / copper porphyrin (Cu · TAPP Trp BOC) / cobalt porphyrin (cobalt · TAPP Trp BOC) / iron porphyrin (Fe · TAPP Trp BOC) / Qiyue supply
How to improve data security by renting servers in Hong Kong
The 15 year old interviewer will teach you four unique skills that you must pass the interview
NFT without IPFs and completely on the chain?
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
第一章:拓广同码小数和s(d, n)
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