Numbers, Strings, Lists notes

Keywords: Python

Reference link: An Informal Introduction to Python

Bullshit: Recently I started to read the python3.7 documentation, hoping to write down the confusing knowledge.

  1. Division always returns a floating point number
    >>> 8/2
    4.0
    >>> 4/6
    0.6666666666666666
    >>>
    
  2. You can use / / division to get an integer
    >>> 8/2
    4.0
    >>> 4/6
    0.6666666666666666
    >>>
    
  3. Power transport use**
    >>> 8/2
    4.0
    >>> 4/6
    0.6666666666666666
    >>>
    
  4. Mixed number type transportation result is floating point
    >>> 3*5.3
    15.899999999999999
    >>> 4+5.5
    9.5
  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
    
  6. 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
    
  7. String multiple sign transportation, plus sign operation
    >>> 'ba'*5+'ha'
    'bababababaha'
    >>> 'ba'*5+'aaaaddddda'
    'bababababaaaaaddddda'
    >>>
    
  8. 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
    >>>
    
  9. Index can be negative, starting from the right
    >>> text[-1]
    'c'
    >>> text[-9]
    'a'
    >>>
    
  10. String slicing, the start index element is always included, and the last index element is not
    >>> text="abcd"
    >>> text[0:1]
    'a'
    
  11. 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'
    >>>
  12. 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"
    
  13. 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]
    >>>
    
  14. 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]
    >>>
  15. 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

Posted by whit3fir3 on Sat, 15 Feb 2020 08:23:21 -0800