python中的for循环
<span class="com">#for循环格式(类似Java中的foreach):for 标识符 in 列表name称: 스팬></span>
<span class="com">#for循环格式(类似Java中的foreach):for 标识符 in 列表名称 :</span>
<span class="pun">>>></span><span class="pln"> movies </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="str">"movie1"</span><span class="pun">,</span><span class="str">"movie2"</span><span class="pun">,</span><span class="str">"movie3"</span><span class="pun">]</span>
<span class="pun">>>></span><span class="pln"> </span><span class="kwd">for</span><span class="pln"> item </span><span class="kwd">in</span><span class="pln"> movies </span><span class="pun">:</span>
<span class="pln"></span><span class="kwd">print</span><span class="pun">(</span><span class="pln">item</span><span class="pun">)</span>
<span class="pln"></span>
<span class="pln">movie1</span>
<span class="pln">movie2</span>
<span class="pln">movie3</span>
<span class="pun">>>></span><span class="pln"> 영화 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="str">"movie1"</span>,<span class="str">"movie2"</span><span class="pun">,</span><span class="str">"movie3 "</span><span class="pun">]</span>
🎜🎜<span class="pun">>>></span><span class="pln"> </span><span class="kwd">for<span class="pln"> 항목 </span><span class="kwd"></span><span class="pln"> 영화 </span><span class="pun"> :</span></span>
🎜🎜🎜🎜<span class="pln"></span>인쇄(항목)
🎜🎜🎜🎜
🎜🎜🎜🎜<span class="pln"></span>
🎜🎜🎜🎜<span class="pln">movie1</span>
🎜🎜🎜🎜<span class="pln">movie2</span>
🎜🎜🎜🎜<span class="pln">movie3</span>
🎜🎜🎜🎜python中的for循环类似Java中的foreach循环,固determin格式见注释🎜그 중: for는 루프의 시작을 의미하며 식별자 앞에 나타나야 하며 루프할 목록을 식별자와 구분합니다. : 목록 처리 코드의 시작을 나타내기 위해 목록 이름 뒤에 배치됩니다. 목록 처리 코드는 이를 for 루프 아래에 배치하고 들여쓰기해야 한다는 점에 유의하세요! ! !
for 루프가 시작되면 Python은 목록의 각 값을 식별자에 할당합니다. 이는 루프가 계속 반복될 때마다 식별자의 값이 다르다는 것을 의미합니다. 목록의 모든 데이터가 처리될 때까지.
Python의 while 루프
<span class=" com">#while 루프 형식: while 루프 조건:</span>
<span class="com">#while循环格式:while 循环条件 :</span>
<span class="pun">>>></span><span class="pln"> </span><span class="kwd">while</span><span class="pln"> count </span><span class="pun"><</span><span class="pln"> len</span><span class="pun">(</span><span class="pln">movies</span><span class="pun">)</span><span class="pln"> </span><span class="pun">:</span>
<span class="pln"></span>print(movies[count])
<span class="pln"> count </span><span class="pun">=</span><span class="pln"> count </span><span class="pun">+</span><span class="pln"> </span><span class="lit">1</span>
<span class="pln"></span>
<span class="pln">movie1</span>
<span class="pln">movie2</span>
<span class="pun"> > ;>></span><span class="pln"> </span><span class="kwd">동안</span><span class="pln"> 개수 </span><span><</span><span class="pln"> len</span><span class="pun">(</span><span class="pln">영화</span > <span class="pun">)</span><span class="pln"> </span><span class="pun">:</span>
#🎜🎜# #🎜🎜##🎜🎜#<span class="pln"></span>인쇄(영화[개수] )
#🎜🎜##🎜🎜##🎜🎜#<span class="pln"> 개수 </span><span class="pun" > =</span><span class="pln"> 개수 </span><span class="pun">+</span><span class="pln"> </span><span class="lit " >1</span>
#🎜🎜##🎜🎜##🎜🎜#
#🎜🎜##🎜🎜##🎜 🎜 #<span class="pln"></span>
#🎜🎜##🎜🎜##🎜🎜#<span class="pln " >movie1</span>
#🎜🎜##🎜🎜##🎜🎜#<span class="pln">movie2</span>
# 🎜 🎜#<span class="pln">movie3</span>
<span class="pln">movie3</span>
while循环与for循环相比,多了条件判断的语句,既可以自定义循环结束的条件
python的判断
<span class="kwd">if</span><span class="pln"> </span><span class="pun">满足某个条件</span><span class="pln"> </span><span class="pun">:</span>
<span class="pln"> </span><span class="pun">执行某些语句</span>
<span class="kwd">else</span><span class="pln"> </span><span class="pun">:</span>
<span class="pln"> </span><span class="pun">执行某些语句</span>
注意:if语句与else语句最后都要加上:,且if与else后面要执行的代码都要缩进
python的函数
<span class="pun">>>></span><span class="pln"> new_movies </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="str">"movie1"</span><span class="pun">,</span><span class="str">"movie2"</span><span class="pun">,</span><span class="str">"movie3"</span><span class="pun">,[</span><span class="str">"movie4_1"</span><span class="pun">,</span><span class="str">"movie4_2"</span><span class="pun">,[</span><span class="str">"movie_4_3_1"</span><span class="pun">,</span><span class="str">"movie_4_3_2"</span><span class="pun">]],</span><span class="str">"movie5"</span><span class="pun">]</span>
<span class="pun">>>></span><span class="pln"> </span><span class="kwd">def</span><span class="pln"> listItems</span><span class="pun">(</span><span class="pln">items</span><span class="pun">)</span><span class="pln"> </span><span class="pun">:</span>
<span class="kwd">if<span class="pln"> </span><span class="pun">특정 조건 충족</span><span class="pln"> </span><span class="pun">:</span></span>
🎜🎜🎜🎜<span class="pln"> </span><span class="pun">특정 문 실행</span>
🎜🎜🎜🎜 <span class="kwd">else</span><span class="pln"> </span><span class="pun">:</span>
🎜🎜🎜 🎜<span class="pln"> </span><span class="pun">특정 명령문 실행</span>
🎜🎜<span class="pun">>>></span><span class="pln"> new_movies </span><span class="pun">=</span><span> </span><span class="pun">[</span><span class="str">"movie1"</span><span class="pun">,</span> <span class=" str">"movie2"</span><span class="pun">,</span><span class="str">"movie3"</span><span class="pun">,[</span><span class="str">"movie4_1"</span><span class="pun">,</span><span class="str">"movie4_2"</span> <span class=" pun">,[</span><span class="str">"movie_4_3_1"</span><span class="pun">,</span><span class="str"> "movie_4_3_2"</span><span class="pun">]],</span><span class="str">"movie5"</span><span class="pun">]</span>
🎜 🎜🎜🎜<span class="pun">>>></span><span class="pln"> </span><span class="kwd">def</span><span class="pln"> listItems</span><span class="pun">(</span><span class="pln">항목</span><span class=" 말장난">)<span class="pln"> </span><span class="pun">:</span></span>
🎜<span class="pln"> </span><span class="kwd">for</span><span class="pln"> 항목 </span><span class="kwd"></span><span class="pln"> 항목 </span><span class="pun">:</span>
<span class="pln"> </span><span class="kwd">for</span><span class="pln"> item </span><span class="kwd">in</span><span class="pln"> items </span><span class="pun">:</span>
<span class="pln"> </span><span class="kwd">if</span><span class="pln"> isinstance</span><span class="pun">(</span><span class="pln">item</span><span class="pun">,</span><span class="pln">list</span><span class="pun">)</span><span class="pln"> </span><span class="pun">:</span>
<span class="pln"> listItems</span><span class="pun">(</span><span class="pln">item</span><span class="pun">)</span>
<span class="kwd">else</span><span class="pln"> </span><span class="pun">:</span>
<span class="pln"> </span><span class="kwd">print</span><span class="pun">(</span><span class="pln">item</span><span class="pun">)</span>
<span class="pln"></span>
<span class="pun">>>></span><span class="pln"> listItems</span><span class="pun">(</span><span class="pln">new_movies</span><span class="pun">)</span>
<span class="pln">movie1</span>
<span class="kwd">if</span><span class="pln"> isinstance</span><span class="pun">(</span>항목<span class="pun">,</span><span class="pln">목록</span><span class="pun">)</span><span class="pln"> </span><span class="pun">:</span>
🎜🎜🎜🎜<span class="pln"> listItems</span><span class="pun">(</span><span class="pln">항목</span>)
🎜🎜🎜🎜<span class="pln"> </span><span class="kwd">else</span><span class="pln"> </span><span>:</span>
🎜🎜🎜🎜<span class="pln"> </span><span class="kwd">인쇄</span><span class="pun">(</span><span class="pln">항목</span><span class="pun">)</span>
🎜🎜🎜🎜
🎜🎜🎜🎜<span class="pln"></span>
🎜🎜🎜🎜<span class="pun">>>></span><span class="pln"> listItems</span><span class="pun">( </span><span class="pln">new_movies</span><span class="pun">)</span>
🎜🎜🎜🎜<span class="pln">movie1</span>
🎜<span class="pln">movie2</span>
<span class="pln">movie2</span>
<span class="pln">movie3</span>
<span class="pln">movie4_1</span>
<span class="pln">movie4_2</span>
<span class="pln">movie_4_3_1</span>
<span class="pln">movie_4_3_2</span>
<span class="pln">movie5</span>
<span class="pln">movie3</span>
<span class="pln">movie4_1</span>
🎜🎜🎜🎜<span class="pln">movie4_2</span>
🎜🎜🎜🎜<span class="pln">movie_4_3_1</span>
🎜🎜🎜🎜<span class="pln">movie_4_3_2</span>
🎜🎜🎜🎜<span class="pln">movie5</span>
🎜🎜🎜🎜상关教程推荐:🎜Python视频教程🎜🎜위 내용은 파이썬 루프 함수의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!