In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use the for loop in Python", the content is simple and clear, hoping to help you solve your doubts, the following let the editor lead you to study and learn "how to use the for loop in Python" this article.
3.5.2 for Loop
Hello, hello, everyone. I don't know how you are doing in practicing the while loop in the last section. I believe you are already familiar with the while loop, so now I am here to explain the for loop to you. Everyone must be very confused. Why is there a while loop and a for loop? what is the for loop for? I believe that many friends who have just come into contact with programming must be very confused, . Because I was confused at the time. All right, let's cut the crap. Let's talk about the difference between the for loop and the while loop, and what the for loop is.
The while loop is very powerful, it can complete any kind of loop, technically, it is enough to have a while loop, so why add a for loop? In fact, for some loops, while still needs to write more code, and in order to further simplify the code of the loop, the Python language exits the for loop. Now you know the difference between a while loop and a for loop? Ha ha. It is to let you write less code and spend more time with your girlfriends. ... Well, it's beside the point.
For loops are primarily used to loop through a collection (sequences and other iterable objects), each time an element is taken from the collection and a code block is executed. Know that all the elements in the collection are enumerated (the process of getting each element in the collection is called enumeration, and we'll talk about enumerated values later. The for loop ends (exits the loop).
You need to use the concept of sets when using for loops, and we're not talking about collections here. The collection here is to put all the elements in a box, which we call (list). The main content here is not to talk about this collection, but to let everyone know about it, and we will talk about it later. Here, we only need to know that the collection is a box, instead of asking you to assemble.
Before using the for loop, define a list whose elements are strings. Then use the for loop to output all the element values in the list. Let's first create a list called words.
Words= ['this','is',' black', 'elephant', 'beautiful woman', 'handsome guy'] # define a string list for word in words: # use for loop to output all the elements in the list print (word) output result: thisis black elephant beauty above this for cycle code is very easy to understand, I pinched and calculated, everyone should be able to understand. But I also want to say what it means. In the for loop statement, variables that hold collection elements and collection variables are separated by the in keyword (students who like to learn have to ask again, what is separation, separation is separation, you care so much). In this case, words is a collection, and when the for loop is executed, each time the loop is executed, it will pull an element out of the words list and throw it to you until it is finished and there is no need to throw it away. It's easy to understand and approachable. You may have a new idea. The for loop is to pick out the elements in the list and throw them to me. The while loop is not to pick out the elements in the list and throw them to me. How can we make the for loop as high-end as the while loop, not just the elements of the list? Can you do that? The answer is that it can be done. Let's think about the logic of thinking against heaven. Ah, yes, it is against heaven. The variable sets an initial value outside the loop, such as x = 1, which means that the initial value of an x variable is set to 1. Within the loop, the execution of the control loop is constantly changed by the value of the variable. To put it bluntly, as I said before, "x = 1 whie x"
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.