#函数,循环,条件,类定义等后面加:冒号
一,条件和分支:if...else..; if...elif...else if后可以添加多重条件如,and,or,not,例如:
if
条件一:
成立执行代码
else
条件二:
成立执行代码
else
:
以上均不成立时执行代码
age_of_older =56 count = 0 while count < 3: guess_age =int(input("guess age:")) if guess_age==age_of_oldbay: print("yes,you got it.") break elif guess_age>age_of_oldbay: print("think smaller...") else: print("think bigger!") count +=1 else: print("you have tried too many times..fuck off")
#break跳出该层循环;continue跳出本次循环进入下一次循环
return就是直接退出程序或函数返回了
return>break>continue
二。循环语句
while循环
while
条件:
# 循环体 条件为真时执行循环体
计数控制
1
2
3
4
|
count
=
1
while
count<
10
:
print
(count)
count
+
=
1
|
for循环
1
2
3
4
|
for
变量
in
序列:
执行代码
else
:
循环结束执行代码
|
age_of_oldboy = 56 count = 0 while count < 3: guess_age = int(input("guess_age:")) if guess_age ==age_of_oldboy: print("yes,you got it.") break elif guess_age > age_of_oldboy: print("think smaller...") else: print("think bigger!") count +=1 if count == 3: continue_confirm= input("do you want keep guessing..?") if continue_confirm !='n': count = 0
for循环
for i in range(10): print("loop",i)range()
函数原型:range(start, end, scan):
参数含义:start:计数从start开始。默认是从0开始。例如range(5)等价于range(0, 5);
end:技术到end结束,但不包括end.例如:range(0, 5) 是[0, 1, 2, 3, 4]没有5
scan:每次跳跃的间距,默认为1。例如:range(0, 5) 等价于 range(0, 5, 1)