-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathif_for_while.py
More file actions
58 lines (53 loc) · 1.06 KB
/
if_for_while.py
File metadata and controls
58 lines (53 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding:utf-8 -*-
a = "12345"
if a == '12345':
print("a == 12345")
elif a != "abcd":
print("a != abcd")
else:
print('other')
if '1' in a and '2' in a or 'b' not in a:
print('find 1 and 2 and not find b')
if 2:
print('true')
else:
print('false')
a = None
if a:
print('None')
else:
print('not None')
if a is None:
print('None')
else:
print('not None')
# 只有可迭代对象才能循环
a = '123456'
for sub_str in a:
print sub_str
print("-------------")
a = [1, 2, 3, 4, 5, 6]
for item in a:
print item
print("-------------")
a = {'name': "zhang", "age": 30}
for key in a:
print(key, a.get(key))
print("-------------")
for key, value in a.iteritems():
print(key, value)
print("-------------")
# break (退出循环),contiune(用于跳过该次循环)
a = '123456'
for item in a:
if item == '5':
break
if item == '3':
continue
print(item)
print("-------------")
# while 条件为真进入循环,知道为假是跳出循环
index = 0
while index != 5:
print(index)
index += 1