boucle for en python
<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>
La boucle for en python est similaire à la boucle foreach en Java Voir le commentaire
pour le format fixe.Parmi eux : for signifie le début de la boucle et doit apparaître devant l'identifiant ; in sépare la liste à boucler de l'identifiant : est placé après le nom de la liste pour indiquer le début du code de traitement de la liste ; veuillez noter que le code de traitement de la liste doit être placé sous la boucle for et en retrait ! ! !
Lorsque la boucle for démarre, Python attribuera chaque valeur de la liste à l'identifiant, ce qui signifie également que la valeur de l'identifiant est différente à chaque fois que la boucle est exécutée ; le traitement est terminé. Toutes les données de la liste.
boucle while en python
<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="pln">movie3</span>
Par rapport à la boucle for, la boucle while contient davantage d'instructions de jugement conditionnel et vous pouvez personnaliser les conditions de fin de la boucle. boucle
Le jugement de 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>
Remarque : l'instruction if et l'instruction else doit être ajouté à la fin : , et le code à exécuter après if et else doit être indenté
fonction 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="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="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>
Recommandations de didacticiels associées : Tutoriel vidéo Python
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!