第5课 控制流
判断语句:if;
循环循环:for, while
1.判断语句 if
a= 10
if a> 10:
print('a> 10')
elif a== 10: //`==`是判断符号;`=`是赋值符号
print('a= 10')
else:
print('a< 10')
//the result is
a= 10
2.循环语句 while
while
count= 0
while count< 10:
count +=1
print('the sum is: ', count)
//the result is
the sum is: 1
the sum is: 2
the sum is: 3
the sum is: 4
the sum is: 5
the sum is: 6
the sum is: 7
the sum is: 8
the sum is: 9
the sum is: 10
while break
count= 0
while count< 10:
count +=1
if count== 5:
break
print('the sum is: ', count)
//the result is from 1 to 4
//the result is
the sum is: 1
the sum is: 2
the sum is: 3
the sum is: 4
while continue
count= 0
while count< 10:
count +=1
if count== 5:
continue
print(' the sum is: ', count)
//the result doesn't include the number 5
//the result is
the sum is: 1
the sum is: 2
the sum is: 3
the sum is: 4
the sum is: 6
the sum is: 7
the sum is: 8
the sum is: 9
the sum is: 10
3.循环语句 for
Int
for i in range(10):
print(i)
// the result is
0
1
2
3
4
5
6
7
8
9
List
a= ['a', 'b', 'c']
for i in a:
print(i)
// the result is
a
b
c
Dict
dict= {
'a': 1,
'b': 2,
'c': 'abc'
}
for k,v in dict.items():
print(k,v)
// the result is
a 1
b 2
c abc
网友评论