In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what's the difference between for cycle and while cycle in Python". The explanation in this article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "what's the difference between for cycle and while cycle in Python"?
In Python, the whilestatement and for statement are used to represent the loop execution of a certain piece of code.
While is followed by a condition, or a sequence (list, tuple, etc.). If the sequence is empty, it jumps out of the loop, otherwise the loop continues.
The for loop is followed by a sequence, and the number of loops is the length of the sequence
The while loop can add an else statement that executes the else when you jump out of the while
Give an example
A = 3
While a > 0:
Print (a)
A-= 1
one
two
three
four
Output:
three
two
one
Shoplist = ['apple',' mango', 'carrot',' banana']
While shoplist:
Print (3)
one
two
three
Output: output 3 until you press ctrl+c
Shoplist = ['apple',' mango', 'carrot',' banana']
While shoplist:
Print (3)
Shoplist = shoplist [: (len (shoplist)-1)] # each loop removes the last element
one
two
three
four
Output:
three
three
three
three
Shoplist = ['apple',' mango', 'carrot',' banana']
While shoplist:
Print (3)
Shoplist = shoplist [: (len (shoplist)-1)]
Else:
Print ('finished') # else statement
one
two
three
four
five
six
Output:
three
three
three
three
Finished
Shoplist = ['apple',' mango', 'carrot',' banana']
For i in shoplist:
Print (I) # where I will be assigned to the elements in shoplist one by one
one
two
three
Output:
Apple
Mango
Carrot
Banana
Shoplist = ('apple',' mango', 'carrot',' banana')
For i in shoplist:
Print (I) # where shoplist is a tuple and a for loop can also follow a tuple
one
two
three
Output:
Apple
Mango
Carrot
Banana
Thank you for your reading, the above is the content of "what is the difference between for cycle and while cycle in Python". After the study of this article, I believe you have a deeper understanding of the difference between for cycle and while cycle in Python. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.