【ICS_32A】Lecture2Week2

Python

   
   

@ZYX 写于2019年10月11日

列表List

  1. append
  2. extend
  3. +=
  4. list(xx)
    list(range(10))
    list(1,2,3)
    list("Boo")
  5. 剪切slice:
    x[1:3]
    list[start:end+1:step=1]
    x[4:-2]
    x[-5:8
    x[-5:-1]
    x[:-5]
    x[4:]
    x[len(x)+1:]

Type Annotation

def len_at_least(s:str,min_length:int) -> bool:
    return len(s)>=min_length
  • 但是这并不是强制的,你仍然可以传(1,"xx")进去,当然会出错
  • 元组tuple: (int,str)
  • 列表List: List[int]
  • When all else fails, you can use a string litera
  • None

    • 所有方法都会返回一个值,只不过没写的都是返回None
    • type(None) => Nonetype

File

  1. file.seek(0),回到第一行

Tips:

  1. Even though Python does not check types until run-time, THAT DOES NOT mean you shouldn’t think about them until run time. That also doesn’t mean you shouldn’t still want to say something about them in your program.