In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to understand the for loop sentence in the Python foundation, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
The Python for loop can iterate through any sequence of items, such as a list or a string.
The syntax format of the for loop is as follows:
For iterating_var in sequence: statements (s)
Example
#! / usr/bin/python#-*-coding: UTF-8-*-for letter in 'Python': # first instance print' current letter:', letter fruits = ['banana',' apple', 'mango'] for fruit in fruits: # second instance print' current fruit:', fruit print "Good bye!"
The output result of the above example:
Current letter: P current letter: y current letter: t current letter: h current letter: O current letter: n current fruit: banana current fruit: apple current fruit: mangoGood bye!
Iterate through sequential index
Another way to traverse a loop is through an index, as shown in the following example:
#! / usr/bin/python#-*-coding: UTF-8-*-fruits = ['banana',' apple', 'mango'] for index in range (len (fruits)): print' current fruit:', fruits [index] print "Good bye!"
The output result of the above example:
Current fruit: banana current fruit: apple current fruit: mangoGood bye!
For the above example, we use the built-in functions len () and range (), and the function len () returns the length of the list, that is, the number of elements. Range returns the number of a sequence.
Recycle else statements
In python, for... Else means that the statements in for are no different from the ordinary ones, and the statements in else will be executed when the loop is normally executed (that is, the for is not interrupted by jumping out of break), while. It's the same with else.
Example
#! / usr/bin/python#-*-coding: UTF-8-*-for num in range (10Magi 20): # iterations between 10 and 20 numeric for i in range (2Maginnum): # determine the first factor based on factor iteration if num%i = = 0: # determine the first factor j=num/i # calculate the second factor print'% d equals% d *% d'% (num,i J) break # jumps out of the else part of the current loop else: # print num,'is a prime number'
The output result of the above example:
10 equals 2 * 511 is a prime number 12 equals 2 * 613 is a prime number 14 equals 2 * 715 equals 3 * 516 equals 2 * 817 is a prime number 18 equals 2 * 919 is a prime number
After reading the above, have you mastered how to understand the for loop statement in the foundation of Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.