python loop function

步履不停
Release: 2019-07-01 13:50:00
Original
4069 people have browsed it

python loop function

for loop in python

  1. #for loop format (similar foreach in Java): for identifier in list name:

  2. ##>>> movies = ["movie1","movie2","movie3"]

  3. #>>> for item in movies :

  4. ##print(item)

  5. #movie1
  6. ##movie2
  7. movie3
  8. The for loop in Python is similar to the foreach loop in Java. See the comments for the fixed format

    Among them: for means the beginning of the loop and should appear in front of the identifier; in separates the list to be looped from the identifier; : is placed after the list name to indicate the start of the list processing code; please note that the list processing code must be placed Below the for loop and indented! ! !

    When the for loop starts, Python will assign each value in the list to the identifier, which also means that the value of the identifier is different each time the loop is executed; the loop will continue to iterate until the processing is completed All data in the list.

    while loop in python

    1. while loop format: while loop condition:

    2. #>>>whilecount <len(movies):

    3. ##print(movies[count])

    4. ## count

      = count1

    5. movie1
    6. movie2
    7. movie3

    Compared with the for loop, the while loop has more conditional judgment statements, both You can customize the conditions for the end of the loop

    Python's judgment

    1. if Satisfy a certain condition :

    2. ##Execute certain statements

    3. ##else :

    4. Execute certain statements

    5. Note: if Statements and else statements must be added with: at the end, and the code to be executed after if and else must be indented

    Python's function

    1. >>> new_movies = ["movie1","movie2","movie3",["movie4_1","movie4_2",["movie_4_3_1","movie_4_3_2" ]],"movie5"]

    2. ##>> >

      def listItems(items):

    3. for item in items :

    4. ifisinstance(item,list) :

    5. listItems(item)

    6. else :

    7. print(item)

    8. >>> listItems(new_movies)

    9. movie1

    10. movie2

    11. ##movie3

    12. movie4_1

    13. ##movie4_2

    14. movie_4_3_1

    15. ##movie_4_3_2

    16. movie5

      Related tutorial recommendations:
    17. Python video tutorial

The above is the detailed content of python loop function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!