In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to understand the FOR loop in Python". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the FOR loop in Python.
Preface
A cycle refers to doing something repeatedly and continuously, for example, the earth rotates once on its axis for a day, and revolves around the sun for a year. Over and over again, round and round, again and again.
In the process of programming, there is often code to be written repeatedly, such as introducing the record merge operation to import three CSV files, that is, three statements, if you want to import 100 or even 10, 000 CSV files? In this way, repetitive work can be left to the computer, and loop statements can be used.
The basic syntax of the For loop is shown in the following figure:
It is important to note that colons cannot be omitted and that the loop syntax block below For needs to be indented so that Python can recognize that subsequent code blocks with the same indentation belong to the For statement.
Python uses code indentation and colons to distinguish the hierarchy between code blocks. The colon at the end of the line and the indentation of the next line indicate the beginning of a code block, and the end of the indent indicates the end of the code block. Code with the same indentation is considered a code block.
The flow of the For loop is shown in the following figure:
Let's first use the For loop to iterate through the string "Python" and enter the following code:
After execution, you can get the following results:
As you can see, the for loop fetches each character in the string "Python", and then the loop code block prints out each character taken, for a total of six characters, the loop is executed six times.
Next, we use the For loop to iterate through a list, entering the following code:
After execution, you can get the following results:
As you can see, the for loop fetches each string in the a list, and then the loop code block prints each string, a total of four strings, and executes the loop four times.
In addition, the range function provided by Python can create a specified list of integers, which is generally used in conjunction with for loops. The syntax of the range function is as follows:
Start: start the number. The default is 0. For example, range (5) is equivalent to range (0meme 5)
Stop: ends the number, but does not include the ending number itself. For example, range (0P5) is [0, 1, 2, 3, 4] there is no 5 position step: step size, default is 1. For example, range is equivalent to range (0prime5)
Let's use the range function in conjunction with the For loop and enter the following code:
After execution, you can get the following results:
As you can see, the list generated by range (5) is [0,1,2,3,4], starting from 0, ending with 4, not containing 5, a total of elements, so you want to For loop execution several times, just enter a few in range () parentheses.
At this point, I believe you have a deeper understanding of "how to understand the FOR loop in Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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: 240
*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.