In Python, data types can be processed directly are the following:
1. integer
Multi-use hexadecimal by the prefix 0x, 0-9, af composition. Example: 0xff00
2. Float (decimal)
Scientific notation written: 1.23x10 ^ 9 -> 1.23e + 9
3. String
With '/' "any text enclosed
(1) If the string contains both internal '
and contains ",
can escape character \
is identified, for example: 'I\'m \"OK\"!'
To simplify, Python also allows r''
represented by ''
the string does not escape the inside of the default
If the internal string has a lot of line breaks, with \n
write one line is not good reading, in order to simplify, Python allows '''...'''
format represents a number of lines
4. Boolean value
A Boolean value only True
, False
two kinds of value
and
Operation with operation, only if all areTrue
,and
the operation result isTrue ——短路逻辑
or
A calculation or operation, as long as one isTrue
,or
the operation result isTrue
not
Calculating a non-operation, which is a unary operator, putTrue
intoFalse
,False
intoTrue
5. null
Null is a special value in Python, with None
representation. None
Can not understand it is 0
, because 0
it makes sense, but None
is a special null value.
Python, there are two division a division is/:
/
Floating-point division calculation result, even if the two exactly divisible integers, floating point result is
Another division is//:
Called floor addition, two integers is an integer division still
Character Encoding
character | ASCII | Unicode | UTF-8 |
---|---|---|---|
A | 01000001 | 00000000 01000001 | 01000001 |
in | x | 01001110 00101101 | 11100100 10111000 10101101 |
Unified computer memory using Unicode encoding
To be saved to the hard disk or transmitted when required, it is converted to UTF-8 encoding
String
ord ( 'A') = 65 - Get character integer
chr (66) = 'B' - transcoding the corresponding character
- Python is a string type str, a Unicode representation in memory, to be transmitted over the network, saved to disk, str -> (in bytes) bytes
Python of bytes
data types tape b
represented prefix single or double quotation marks:
x = b'ABC'
To distinguish between 'ABC'
and b'ABC'
, the former str
, although the latter have to display the contents of the former and the same, but bytes
each character only one byte.
-
In Unicode
str
by theencode()
method may be coded as specifiedbytes
, for example:>>> 'ABC'.encode('ascii') b'ABC' >>> '中文'.encode('utf-8') b'\xe4\xb8\xad\xe6\x96\x87'
- Plain English
str
can beASCII
encoded asbytes
the content is the same, containing the Chinesestr
can beUTF-8
encoded asbytes
-
If we read from the disk or network byte stream, then the data is read
bytes
. Shouldbytes
becomestr
, we need to usedecode()
methods:>>> b'ABC'.decode('ascii') 'ABC' >>> b'\xe4\xb8\xad\xe6\x96\x87'.decode('utf-8') '中文'
-
Since Python source code is a text file, so when your source code contains Chinese, and when you save the source code, you need to be sure to specify saved as UTF-8 encoding. When the Python interpreter to read the source code to make it according to UTF-8 code reading, we usually write two lines in the beginning of the file:
#!/usr/bin/env python3 # -*- coding: utf-8 -*-
-
The first line is a comment to tell Linux / OS X system, which is a Python executable, Windows will ignore the comment;
The second line comments is to tell the Python interpreter, according to the UTF-8 encoding to read the source code, or else, you write the source code in Chinese output may be garbled.
Affirming the UTF-8 encoding does not mean your
.py
file is UTF-8 encoded, and must be sure that the text editor is using UTF-8 without BOM code:
format
Common placeholders are:
Placeholder | Replace content |
---|---|
%d | Integer |
%f | Float |
%s | String |
%x | Hexadecimal integer |
>>>print('%.2f ' %3.1415926)
>>>3.14
- If the string in% is an ordinary character, you need to escape
- >>>%d %% %7
- >>>7%
format () - a placeholder
>>> 'Hello, {0}, 成绩提升了 {1:.1f}%'.format('小明', 17.125)
'Hello, 小明, 成绩提升了 17.1%'
Operator precedence:
- Exponentiation **
- + Sign -
- Arithmetic operators * / // + -
- Comparison operators <<=>> = ==! =
- Logical operators and or not
The conditional expression (ternary operator)
x, y = 4.5
if x<y:
small = x
else:
small = y
————>
small = x if x<y else y
small =(x if x<y and x<z) else (y if y<z else z)
Assert assertion
When the assert condition is false, the program automatically ignored crash --AssertionError-- Conversely - [into] Checkpoint