Python和java 的区别笔记(未完成)

1.  非  boolean not

Python     not 

java           !

 2.函数中的缺省值

如果函数中有一个值是 常用的例如下图的 score  合格分数线

def  overScoreStudents(studentScoreList, score):
    count = 0

    # 下面的代码用到了循环和判断,后面章节会学习到
    for ss in studentScoreList:
        if ss >= score:
            count += 1
    
    return count

那么可以写为

def  overScoreStudents(studentScoreList, score=60):

那么 如果没有定于score时 ,直接  overScoreStudents(ssList)  此时score默认为60

如果需要定义时 overScoreStudents(ssList, 70) 此时score为70

 

注意:arg4 前面的参数 arg3 已经有缺省值,所以必须也要有缺省值,比如

def  func(arg1, arg2, arg3=3, arg4='hello'):

 

指定参数名调用函数

这样的一个函数

def  func(arg1, arg2, arg3=3, arg4='hello'):
    print(arg1)
    print(arg2)
    print(arg3)
    print(arg4)

我们调用的时候,可以这样

func(1,2,3,'hello')

也可以这样 指定参数名 去调用

func(arg1=1,arg2=2,arg3=3,arg4='hello')

指定参数名调用的时候,可以颠倒参数的次序

func(arg2=1,arg3=2,arg1=3,arg4='hello')

也可以这样混合使用

func( 1, 2, arg3=3,arg4='hello')


但是一旦某个参数指定了参数名,后面所有的参数必须指定参数名

像下面这样是不可以的

func( 1, 2, arg3=3, 'hello') # 错误的调用方式

 

posted @ 2020-11-09 11:28  好好学习_liu  阅读(82)  评论(0编辑  收藏  举报