Python : What is the purpose of a colon in an if statement?

后端 未结 4 1823
暗喜
暗喜 2021-01-29 17:21

I have this piece of python code below.

def m(list):
    v = list[0]
    for e in list:
        if v < e:
            v = e
        return v

values = [[3, 4,         


        
4条回答
  •  旧时难觅i
    2021-01-29 17:46

    In [8]: v = 1
    In [9]: e = 2
    In [10]: if v < e: v = e
    In [11]: v
    Out[11]: 2
    In [12]: e
    Out[12]: 2
    

    is same as:

    In [13]: v = 1
    In [14]: e = 2
    In [15]: if v < e:  # if True execute next statement 
       ....:     v = e
       ....:     
    In [16]: v
    Out[16]: 2
    In [17]: e
    Out[17]: 2
    

提交回复
热议问题