---restore content starts---
1, in the computer memory, the unified use of Unicode encoding, when the need to save to the hard disk or need to transfer, the conversion to UTF-8 encoding.
When editing with Notepad, the UTF-8 characters read from the file are converted to Unicode characters into memory, and when the edits are complete, the conversion of Unicode to UTF-8 is saved to the file.
2, for the encoding of a single character, Python provides a ord()
function to get the integer representation of the character, the chr()
function converts the encoding to the corresponding character:
3, because the string type of Python is str
, in memory in Unicode, a character corresponding to a number of bytes. If you want to transfer on a network, or save to disk, you need to turn it str
into bytes bytes
.
4, %
the operator is used to format the string. Inside the string, the representation is replaced by a string, which %s
%d
is replaced with an integer, there are several %?
placeholders, followed by a number of variables or values, the order to correspond well. If there is only one %?
, the parentheses can be saved. ' Hello%s,you has%s money ' (' Mike ', 1000) error ' Hello%s,you has%s money '% (' Mike ', 1000) correct!
5, escaped, used %%
to denote a%。
' %2d-%02d ' % (3, 1)' 3-01'%.2f' % 3.1415926' 3.14'
6, List if you want to take the last element, in addition to the calculation of the index location, you can also use -1
the index to get the last element directly
7. The data type of the elements in the list can also be different; The list element can also be another list.
8, another ordered list called tuples: tuple. Tuple and list are very similar, but a tuple cannot be modified once initialized
classmates = (‘Michael‘, ‘Bob‘, ‘Tracy‘)
9. What is the meaning of immutable tuple? Because the tuple is immutable, the code is more secure. If possible, you can use a tuple instead of a list as much as possible.
Each element of a tuple, pointing to never change. That is, pointing ‘a‘
, cannot be changed to point ‘b‘
, point to a list, can not be changed to point to other objects, but the list itself is variable!
---restore content ends---
Liao Xue python excerpt 2