>백엔드 개발 >파이썬 튜토리얼 >파이썬 루프 함수

파이썬 루프 함수

步履不停
步履不停원래의
2019-07-01 13:50:004159검색

파이썬 루프 함수

python中的for循环

  1. <span class="com">#for循环格式(类似Java中的foreach):for 标识符 in 列表name称: 스팬></span><span class="com">#for循环格式(类似Java中的foreach):for 标识符 in 列表名称 :</span>

  2. <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>

  3. <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>

  4. <span class="pln"></span><span class="kwd">print</span><span class="pun">(</span><span class="pln">item</span><span class="pun">)</span>

  5. <span class="pln"></span>

  6. <span class="pln">movie1</span>

  7. <span class="pln">movie2</span>

  8. <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 루프

  1. <span class=" com">#while 루프 형식: while 루프 조건:</span><span class="com">#while循环格式:while 循环条件 :</span>

  2. <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>

  3. <span class="pln"></span>print(movies[count])

  4. <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>

  5. <span class="pln"></span>

  6. <span class="pln">movie1</span>

  7. <span class="pln">movie2</span>

  8. #🎜🎜#<span class="pun"> &gt ;>></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># 🎜 🎜#
  9. <span class="pln">movie3</span><span class="pln">movie3</span>

while循环与for循环相比,多了条件判断的语句,既可以自定义循环结束的条件

python的判断

  1. <span class="kwd">if</span><span class="pln"> </span><span class="pun">满足某个条件</span><span class="pln"> </span><span class="pun">:</span>

  2. <span class="pln"> </span><span class="pun">执行某些语句</span>

  3. <span class="kwd">else</span><span class="pln"> </span><span class="pun">:</span>

  4. <span class="pln"> </span><span class="pun">执行某些语句</span>

注意:if语句与else语句最后都要加上:,且if与else后面要执行的代码都要缩进

python的函数

  1. <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>

  2. <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>

🎜 for 루프와 비교하여 while 루프에는 더 많은 조건부 판단 문이 있습니다. 자동으로 사용 루프 종료 조건 정의🎜🎜파이썬 판단
🎜
    🎜🎜<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>🎜🎜
🎜참고: if 문과 else 문은 끝에 다음을 추가해야 하며, if와 else 이후에 실행될 코드는 들여쓰기를 해야 합니다🎜🎜파이썬 함수🎜
    🎜🎜<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>🎜
  1. <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>

  2. <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>

  3. <span class="pln">                listItems</span><span class="pun">(</span><span class="pln">item</span><span class="pun">)</span>

  4.            <span class="kwd">else</span><span class="pln"> </span><span class="pun">:</span>

  5. <span class="pln">                </span><span class="kwd">print</span><span class="pun">(</span><span class="pln">item</span><span class="pun">)</span>

  6. <span class="pln"></span>

  7. <span class="pun">>>></span><span class="pln"> listItems</span><span class="pun">(</span><span class="pln">new_movies</span><span class="pun">)</span>

  8. <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>🎜
  9. <span class="pln">movie2</span><span class="pln">movie2</span>

  10. <span class="pln">movie3</span>

  11. <span class="pln">movie4_1</span>

  12. <span class="pln">movie4_2</span>

  13. <span class="pln">movie_4_3_1</span>

  14. <span class="pln">movie_4_3_2</span>

  15. <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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.