Unicode string
The string also has an encoding problem.
Because a computer can only handle numbers, if you are working with text, you must convert the text to a number before processing it. The oldest computer was designed with 8 bits (bit) as a byte (byte), so the largest integer that a Word energy saver represents is 255 (binary 11111111 = decimal 255), and 0-255 is used to denote uppercase and lowercase letters, numbers, and some symbols. This Code table is called ASCII encoding, such as the code for capital A is 65, and the lowercase z is encoded as 122.
If you want to express Chinese, obviously a byte is not enough, need at least two bytes, and also can't and ASCII encoding conflict, so, China has developed GB2312 code, used to put Chinese into.
Similarly, other languages such as Japanese and Korean also have this problem. In order to unify all text encoding, Unicode came into being. Unicode unifies all languages into a set of encodings, so there is no more garbled problem.
Unicode usually uses two bytes to represent a character, the original English encoding from a single byte into a double-byte, only need to fill the high byte all 0 can be.
Because Python was born earlier than the Unicode standard, the earliest Python only supported ASCII encoding, and the normal string ' ABC ' was ASCII-encoded inside python.
Python later added support for Unicode, with a Unicode-represented string expressed in U ' ... ', for example:
Print U ' Chinese '
Note: without u, Chinese will not display properly.
In addition to a single Unicode string u
, the escape character and multiline notation are still valid, with no difference to the normal string:
Escape:
U ' chinese \ n \ japanese \ Korean '
MultiRow
U ' ' first line second line '
raw+ Multiple lines:
ur ' python's Unicode string supports "Chinese", "Japanese", "Korean" and many other languages "
If the Chinese string encounters unicodedecodeerror in a Python environment, this is because there is a problem with the format saved by the. py file. You can add comments on the first line
#-*-Coding:utf-8-*-
The purpose is to tell the Python interpreter to read the source code with UTF-8 encoding. Then save with notepad++ as ... and select UTF-8 format to save.
Integers and floating-point numbers
Python supports four blending of integers and floating-point numbers directly, and the arithmetic rules are exactly the same as the arithmetic rules in mathematics.
Basic operations:
1 + 2 + 3 # ==> * 5-6 # ==> 147.5/8 + 2.1 # ==> 3.0375
Using parentheses can elevate precedence, which is exactly the same as math, and note that only parentheses can be used, but parentheses may nest many layers:
(1 + 2) * 3 # ==> 9 (2.2 + 3.3)/(1.5 * (9-0.3)) # ==> 0.42145593869731807
Unlike mathematical operations, the result of a Python integer operation is still an integer, and the result of the floating-point operation is still a floating-point number:
1 + 2 # ==> integer 31.0 + 2.0 # ==> Floating point 3.0
But the result of mixed integer and floating-point numbers becomes a floating-point number:
1 + 2.0 # ==> Floating point 3.0
Why do you differentiate between integer and floating-point arithmetic? This is because the result of an integer operation is always accurate, and the result of the floating-point operation is not necessarily accurate, because the computer memory is large, and can not accurately represent an infinite loop of decimals, for example, a binary 0.1
representation is an infinite loop decimal.
Is the result not a floating-point number when the division operation of the integer is encountered in addition to the endless? Let's try it out:
11/4 # ==> 2
To the surprise of many beginners, the integer division of Python, even if it is not endless, the result is still an integer, the remainder is directly thrown away. However, Python provides a calculation of the remainder of the operation% that can be computed:
% 4 # ==> 3
If we want to calculate the exact result of 11/4, the "integer and floating-point mixed operation results are floating-point number" law, the two numbers of one into a floating point and then the operation is no problem:
11.0/4 # ==> 2.75
Boolean type
We've learned that Python supports Boolean types of data, with only a Boolean type True
and False
two values, but the Boolean type has the following operations:
and Operation : Only two Boolean values are true when the result is true.
True and True # ==> Truetrue and False # ==> Falsefalse and True # ==> Falsefalse and False # ==& Gt False
or Operation : evaluates to true as long as there is a Boolean value of true.
True or True # ==> truetrue or False # ==> TrueFalse or True # ==> TrueFalse or False # ==> Fa Lse
Non-operation : Turns true to false, or false to true:
Not True # ==> falsenot False # ==> True
Boolean operations are used in a computer to make conditional judgments, and the computer can automatically execute different follow-up code, depending on the result of the calculation as true or false.
In Python, Boolean types can also do and, or, and not with other data types, see the following code:
A = Trueprint A and ' a=t ' or ' a=f '
The result of the calculation is not a Boolean type, but the string ' a=t ', which is why?
Because Python regards 0
, 空字符串‘‘
and None
is treated as False, other numeric values and non-empty strings are considered True, so:
True and ' a=t ' evaluates to ' a=t ' continue to calculate ' a=t ' or ' a=f ' results or ' a=t '
To explain the above results, it also involves an important law of the and and or operations: short-circuit calculation.
1. In the calculation a and b
, if A is false, then according to the algorithm, the whole result must be false, so it returns a; If A is True, the entire result must depend on B, and therefore returns B.
2. In the calculation a or b
, if A is true, the whole result must be true, according to the or algorithm, and therefore return a; If A is False, the whole result must depend on B, and therefore return B.
So when the Python interpreter is doing a Boolean operation, as long as the calculation results can be determined in advance, it will not go back and return the result directly.
Python Beginner notes (iii)