Is there a for function in python?

爱喝马黛茶的安东尼
Release: 2019-06-19 11:50:02
Original
3545 people have browsed it

Is there a for function in python?

There is a for function in Python, which is generally used in three situations. The following will introduce it to you in detail:

Python for loop statement

Python for loop can traverse any sequence of items, such as a list or a string.

Grammar:

The syntax format of for loop is as follows:

for iterating_var in sequence:
   statements(s)
Copy after login

Example

#!/usr/bin/python
# -*- coding: UTF-8 -*-
for letter in 'Python':     # 第一个实例
    print '当前字母 :', letter
fruits = ['banana', 'apple',  'mango']
for fruit in fruits:        # 第二个实例
   print '当前水果 :', fruit
print "Good bye!"
Copy after login

The output result of the above example:

当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h
当前字母 : o
当前字母 : n
当前水果 : banana
当前水果 : apple
当前水果 : mango
Good bye!
Copy after login

Iterate through sequence index

Related recommendations: "python video tutorial"

Another way to perform loop traversal is through Index, the following example:

Example

#!/usr/bin/python
# -*- coding: UTF-8 -*-
fruits = ['banana', 'apple',  'mango']
for index in range(len(fruits)):   
    print '当前水果 :', fruits[index]
print "Good bye!"
Copy after login

The output result of the above example:

当前水果 : banana
当前水果 : apple
当前水果 : mango
Good bye!
Copy after login

In the above example we used the built-in functions len() and range(), functions len() returns the length of the list, that is, the number of elements. range returns a sequence of numbers.

Loop using else statements

In python, for...else means this. The statements in for are no different from ordinary ones. The statements in else will be in It is executed when the loop is executed normally (that is, for is not interrupted by break), and the same is true for while...else.

Example

#!/usr/bin/python# -*- coding: UTF-8 -*-
 for num in range(10,20):  # 迭代 10 到 20 之间的数字
   for i in range(2,num): # 根据因子迭代
      if num%i == 0:      # 确定第一个因子
         j=num/i          # 计算第二个因子
         print '%d 等于 %d * %d' % (num,i,j)
         break            # 跳出当前循环
   else:                  # 循环的 else 部分
      print num, '是一个质数'
Copy after login

Output result of the above example:

10 等于 2 * 5
11 是一个质数
12 等于 2 * 6
13 是一个质数
14 等于 2 * 7
15 等于 3 * 5
16 等于 2 * 8
17 是一个质数
18 等于 2 * 9
19 是一个质数
Copy after login

The above is the detailed content of Is there a for function in python?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template