Routine usage
Grammar memory methods
if+ Space + Conditions + The colon
tab Indent code body
tab Indent code body
tab Indent code body
Three mesh operation
Results satisfying the conditions if Conditions else The result of not meeting the conditions
More branches
if Conditions 1:
code1
elif Conditions 2:
code1
elif Conditions 3:
code1
else:
code1
principle
(1) not The highest priority , It is to reverse the result of the following condition , therefore not It is inseparable from the following conditions
(2) If all of the statements use and Connect , Or all of them or Connect , Then it can be calculated from left to right
(3) If there is both and Also have or, So let's start with brackets and The left and right conditions of , And then we do the calculation
and And ( priority 3)
Connect the left and right conditions , Only when the two conditions hold at the same time will the final result be True
or or ( priority 2)
Connect the left and right conditions , If one condition holds, the final result will be True
not Not ( priority 1)
Take the opposite by true
for Variable name in Data to cycle :
for Loop through Dictionary
namess = {'name1':'dahai','name2':'xialuo','name3':'xishi'}
Default traversal key value
for i in namess:
print(i)
The second kind of traversal key value
for i in namess.keys():
print(i)
Traverse value value
for i in namess:
print(namess[i])
print(i)
Direct fetch traversal value value
for i in namess.values():
print(i)
Traversal key value pairs
for i in namess.items():
# take key
print(i[0])
# take value
print(i[1])
for Loop through the list
for a in names:
print(a)
range(0 start ,10 End ,2 step )
It's an iterator range and list Equivalent , however range Smaller memory footprint .
List to heavy
list1=['aaa','bbb','ccc']
list2 = ['ccc']
for i in list1:
if i in list2:
print(' Already there. ')
else:
list2.append(i)
enumeration enumerate
goods_list=[
['coffee',30],
['chicken',20],
['iphone',10000],
['car',100000],
['building',200000]
]
for i,item in enumerate(goods_list):
print(i,item)
while Conditions :
while Conditions :
code1
code2
code3
while True:
while True:
print('111')
print('222')
**continue Skip this cycle **
**break Represents the end of the loop **