3.1 python学习笔记本

84 阅读2分钟

what it is

-   we don't need to "declare" `spam_amount` before assigning to it
-   we don't need to tell Python what type of value `spam_amount` is going to refer to. In fact, we can even go on to reassign `spam_amount` to refer to a different sort of thing like a string or a boolean.
  • The colon (:) at the end of the if line indicates that a new code block is starting

  • Subsequent lines which are indented are part of that code block.

  • Strings can be marked either by double or single quotation marks.

    • if strings are including a variable(single quote),"" is better.

your function

first to write a docstrings

  • default argument can be added by (num_child = 3)

  • no return function is none = NULL in other language.

  • help() insert the function to help ,which is can be your own coded function.

  • 函数有一个default参数,如果我们传入自己的参数覆盖那么会返回该参数,否则会使用系统默认的参数。因此我们需要先用Help文档查阅参数。

  • use def before naming the function. and end with a:!!!!

  • 函数嵌套,higher-order functions 中文文档我害怕有一天我读不懂英文,突然有一天,不过想一想,英文和中文也差不多一样的烂。

  • conditional operator

      should to be `True` or `False`
      `and or not ` VS `&& || !` in c++
      
      - to  improve the readibility ,we should add more parentheses.
      
    
  • if elif VS else : after each statement

  • invert function transfer the content's type compulsorily , bool(x),except 0 is 1.which means only bool(0) = False,so too does the empty string or char

  • sign function

    to justify the sign of a numerical input .

    print("Splitting", total_candies, "candy" if total_candies == 1 else "candies")
    

python can directly calculate the boolen as (0 or 1) as arithmetic

  • list

       1. different from c++ , 'list string variable is a single or double (=) be a member of list.
       2. not the curly parentheses we use the square brackets[]
       3. mix of all the availble types,help is accessible !!!
       4. the index is from 0,also [-1] [-2]...
       5. slicing [:2] = [0],[1];      [2:] = [2], [3], [4], .......till the end of this list.!     [1:-1] = [1], [2], ....[end]
           [-3:] = [-3], [-2], [end]
       6. bin(int ) return its binary item
       
       
    
    • len(list)
    • sorted(list) in alphabetical
    • sum(list)
    • min/max(list)
  • everything in python is an object

    • attribute: non function of the object

    • method : function of the object ! when we call the funciton ,we should add a parenthese () to the end of the function name

    • list. append(int ) add this int in the end of the list

    • list . pop()

    • list.index()

    • conditional operator in : int in list

  • tuple

    • () not []
    • the elements in the tuple is not modified (inmutable)
    • can be used to represent the multiple value of a object!
a, b = b, a
  • loops

    • tuple,list, string all the element in this iterable things
  • range()

  • for while dont have a ()

  • many sentences can be concluded in a []

  • any return iterable in list to classify any ele in the list

  • string """ """ docstring can cotain a enter (= \n) python auto add a \n ,so we need to add a sep= ' '

  • like a list function but can not to be modified

  • str.split('-')whitespace a list

  • '' .join['s','a','d']

  • +,only str can be sewed to be a long atr

{}",format(a)

keys to be the index nums[key] to get its value

nums.values() nums.keys() nums.items() return key and value

help(str)