[Python] Condition & Loop

Conditional statements

  • do not add()
  • Add at the end:
  • elif else and if used in pairs
  • Omit judgment condition
    • String: The empty string is False, and the rest are True
    • int: 0 is False, the rest is True
    • Bool: True is True, False is False
    • list / tuple / dict / set: iterable is empty and parsed as False, the rest is True
    • Object: None resolves to False, the rest is True

loop statement

  • The iterable data structure can be traversed as follows
    • for item in <iterable>:
    • ...
  • Dictionary traversal
1 d = {'name' : 'jason', 'dob': '2000-01-01', 'gender': 'male' }
2 for k in d:
3     print(k)
4 print(" ")
5 for v in d.values():
6     print(v)
7 print(" ")
8 for k,v in d.items():
9     print('key:{}, value:{}'.format(k,v))
View Code

  • Traversal by index
 1 # 方法1
 2 l = [1,2,3,4,5,6,7]
 3 for index in range(0, len(l)):
 4     if index < 5:
 5         print(l[index])
 6 
 7 # 方法2
 8 l = [1,2,3,4,5,6,7]
 9 for index,item in enumerate(l):
10     if index < 5:
11         print(item)
View Code
  • continue和break
1  # screened price is less than 1000, and the color is not red all "products - Color" composition 
2  # not used Continue 
. 3 name_price = { ' book_1 ' : 120, ' book_2 ' : 500, ' book_3 ' : 2000, ' book_4 ' : 200 }
 4 name_color = { ' book_1 ' : [ ' red ' , ' blue ' ], ' book_2 ' : [ ' green ' , ' blue '],'book_3':['red','blue']}
 5 
 6 for name, price in name_price.items():
 7     if price < 1000:
 8         if name in name_color:
 9             for color in name_color[name]:
10                 if color != 'red':
11                     print('name:{},color:{}'.format(name,color))
12         else:
13             print('name:{},color:{}'.format(name,'None'))
14 
15 # 使用continue
16 for name, price in name_price.items():
17     if price >= 1000:
18         continue
19     if name not in name_color:
20         print('name:{},color:{}'.format(name,'None'))
21         continue
22     for color in name_color[name]:
23         if color == 'red':
24             continue
25         print('name:{},color:{}'.format(name,color))
View Code

  • effectiveness
    • for: range () is written in C language, with high efficiency
    • while: i + = 1 will design object creation and deletion (equivalent to i = new int (i + 1))
  • Simplified writing
1  # divided by commas words, the first null character is removed, filtered word length less than or equal to 5, and returns a list of words consisting of 
2 text = '   Today, IS, the Sunday ' 
. 3 text_list = [s.strip () for S in text .split ( ' , ' ) if len (s.strip ())> = 5 ]
 4  print (text_list)
View Code

 1 # 计算函数值y = 2*|x| + 5
 2 # expression1 if condition else expression2 for item in iterable
 3 # 等价于:
 4 # for item in iterable:
 5 #     if condition:
 6 #         expression1
 7 #     else:
 8 #         expression2
 9 x = [-1,0,1,2,3,4]
10 y = [value * 2 + 5 if value > 0 else -value * 2 + 5 for value in x]
11 print(y)
View Code

 

Guess you like

Origin www.cnblogs.com/cxc1357/p/12695276.html