简单的Python语句

280 阅读1分钟

· if语句

if elif elif else

if x < 0:
...     x = 0
...     print('Negative changed to zero')
... elif x == 0:
...     print('Zero')
... elif x == 1:
...     print('Single')
... else:
...     print('More')

for语句具有迭代功能,

for i in range(1,9):
    print(i)

break退出迭代的过程

continue 退出本次循环,进行下一次循环

pass 一个占位符,表示什么也不做

python 函数

def 函数名(arg1,arg2...):
	函数体

函数的调用

函数名(参数)

函数默认值的设置

total=0
def count(start=1,end=100):
    total=0
    for i in range(start,end):
        total+=i
    return total

total=count(1,10)
print(total)
total=count()
print(total)

函数可以在参数列表中定义默认值,如果不传人参数,会默认使用默认 参数