Reference link: An Informal Introduction to Python
Bullshit: Recently I started to read the python3.7 documentation, hoping to write down the confusing knowledge.
- Division always returns a floating point number
>>> 8/2 4.0 >>> 4/6 0.6666666666666666 >>>
- You can use / / division to get an integer
>>> 8/2 4.0 >>> 4/6 0.6666666666666666 >>>
- Power transport use**
>>> 8/2 4.0 >>> 4/6 0.6666666666666666 >>>
- Mixed number type transportation result is floating point
>>> 3*5.3 15.899999999999999 >>> 4+5.5 9.5
- If there are many and complex symbols in a string, you can use print to produce more readable output
>>> 3*5.3 15.899999999999999 >>> 4+5.5 9.5
- If you do not want some characters to be interpreted as special characters, you can use R or r before quotation marks
>>> print('C:\some\name') C:\some ame >>> print(r'C:\some\name') C:\some\name
- String multiple sign transportation, plus sign operation
>>> 'ba'*5+'ha' 'bababababaha' >>> 'ba'*5+'aaaaddddda' 'bababababaaaaaddddda' >>>
- You can automatically connect strings by using quotation marks and quotation marks, which is suitable for breaking the writing of long strings
'dddddddaa' >>> test=('heiheiaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ... 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb') >>> print(test) heiheiaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb >>>
- Index can be negative, starting from the right
>>> text[-1] 'c' >>> text[-9] 'a' >>>
- String slicing, the start index element is always included, and the last index element is not
>>> text="abcd" >>> text[0:1] 'a'
- Normally, an exception occurs when the index is out of range, but the slice does not
>>> text="abcde" >>> text[5] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range >>> text[9:90] '' >>> text[2:90] 'cde' >>>
- String cannot be changed, need new different string, recreate
>>> text="opq" >>> text[0]='a' Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment >>> text="apq"
- list supports + (connection), * operation (repeated connection)
>>> a=[2]*3 >>> a [2, 2, 2] >>> a[0]=3 >>> a [3, 2, 2] >>> b=[4]*3 >>> b [4, 4, 4] >>> a+b [3, 2, 2, 4, 4, 4] >>>
- Slicing can change the size of the list, or even empty it completely (e.g: nested list)
>>> a=[3,3,4,5,5,6,677,7] >>> a[1:3] [3, 4] >>> a=[3,3,4,5,5,6,677,7] >>> a[1:5]=[] >>> a [3, 6, 677, 7] >>> a[:]=[] >>> a []
>>> a=[3,2,3,4,4]
>>> a[1:3]=[[0],[2,4]]
>>> a
[3, [0], [2, 4], 4, 4]
>>> - Anything of non-zero value and non-zero length can be of type True (e.g: sequence and string of at least one element, 0.0, 2), otherwise it can be of type False (e.g: None, 0, [], ())
>>> bool('1') True >>> bool('0') True >>> bool('aaa') True >>> bool('') False >>> bool('[2,3]') True >>> bool('[]') True >>> bool('0.0')
True