#for循环格式(类似Java中的foreach):for 标识符 in 列表名称 :
>>> movies = ["movie1","movie2","movie3"]
>>> for item in movies :
print(item)
movie1
movie2
movie3
The For loop in Python is similar to a foreach loop in Java, with a fixed format in comments where: for indicates the start of the loop, to appear before the identifier, and in the list to be looped separated from the identifier;: placed after the list name, indicating that the processing list code starts; The list processing code must be placed below the FOR loop and indented!!! When the For loop starts, Python assigns each value in the list to an identifier, which also means that the value of each loop identifier is different; The loop iterates until all the data in the list is processed.
#while循环格式:while 循环条件 :
>>> while count < len(movies) :
print(movies[count])
count = count + 1
movie1
movie2
movie3
While loop has more conditional-judged statements than a for loop, you can customize the condition of the loop end
If-else format
if 满足某个条件 :
执行某些语句
else :
执行某些语句
NOTE: The IF statement and the Else statement are finally added: and the code to be executed after if and else is indented
>>> new_movies = ["movie1","movie2","movie3",["movie4_1","movie4_2",["movie_4_3_1","movie_4_3_2"]],"movie5"]
>>> def listItems(items) :
for item in items :
if isinstance(item,list) :
listItems(item)
else :
print(item)
>>> listItems(new_movies)
movie1
movie2
movie3
movie4_1
movie4_2
movie_4_3_1
movie_4_3_2
movie5
From for notes (Wiz)
Python loops, judgments and functions