Data Type-Boolean value
A Boolean value of only true, false, two values, either True or False
Boolean values can be calculated with and, or, and not
A null value is a special value in Python, denoted by none
Python uses single or double quotation marks with a B prefix for data of type bytes:
x = B ' ABC '
Variable
The type of variable itself is called Dynamic language, and it corresponds to static language. Static languages must specify variable types when defining variables
Division takes only the integer part of the result, so Python also provides a remainder operation that can be used to divide the remainder of two integers.
Character encoding
In computer memory, Unicode encoding is used uniformly, and is converted to UTF-8 encoding when it needs to be saved to the hard disk or when it needs to be transferred.
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:
For the encoding of a single character, Python provides an integer representation of the Ord () function to get the character, and the Chr () function converts the encoding to the corresponding character:
>>> Ord (' A ')
65
>>> Ord (' Middle ')
20013
>>> Chr (66)
B
>>> chr (25991)
Text
the STR represented in Unicode can be encoded as a specified bytes by the Encode () method,
>>> ' ABC '. Encode (' ASCII ')
b ' ABC '
>>> ' Chinese '. Encode (' Utf-8 ')
b ' \xe4\xb8\xad\xe6\x96\x87 '
If we read the byte stream from the network or disk, then the data read is bytes. To turn bytes into STR, you need to use the Decode () method
>>> B ' ABC '. Decode (' ASCII ')
' ABC '
>>> B ' \xe4\xb8\xad\xe6\x96\x87 '. Decode (' Utf-8 ')
' Chinese '
The Len () function calculates the number of characters in STR and computes the number of bytes if replaced by the Bytes,len () function.
>>> Len (b ' ABC ')
3
>>> Len (b ' \xe4\xb8\xad\xe6\x96\x87 ')
6
>>> Len (' Chinese '. Encode (' Utf-8 '))
6
Formatting
>>> ' Hello,%s '% ' world '
' Hello, World '
>>> ' Hi,%s, you have $%d. '% (' Michael ', 1000000)
' Hi, Michael, you have $1000000. '
Specifies whether to complement 0 and the number of digits of integers and decimals
Print ('%2d-%02d '% (3, 1))
Print ('%.2f '% 3.1415926)
3-01
3.14
If you're not sure what to use,%s will always work, and it will convert any data type to a string
>>> ' Age:%s. Gender:%s '% (True)
' Age:25. Gender:true '
Escaped, expressed as a% by percent
>>> ' growth rate:%d percent '% 7
' Growth Rate:7% '
Format ()
>>> ' Hello, {0}, result increased {1:.1f}% '. Format (' name ', 17.125)
' Hello, name, the score has increased by 17.1% '
Python character encoding, formatting