如果你有一个序列对象,如 列表,你可以使用 for loop 重复列表中的项目。
for loop的功能与您在其他多种编程语言中所看到的功能没有太大不同。
在本文中,我们将详细探索 Python for loop,并学习在包括列表,tuples 和更多在内的不同序列中迭代。
Python for loop 的基本语法
Python中的for loop的基本语法看起来有点类似于下面提到的语法。
1for itarator_variable in sequence_name:
2 Statements
3 . . .
4 Statements
**让我更好地解释Python的语法。
- 声明的第一个单词以 **关键字
for
开始,这意味着为循环的开始 - 然后我们有 iterator 变量,它在序列中迭代,并可用于循环中执行各种函数
- 接下来是 Python 中的
在
关键字,它告诉循环中的迭代器变量为循环的序列中的元素 - 最后,我们有 序列变量,它可以是列表,tuple,或任何其他类型的迭代器
- 循环的陈述部分是你可以用迭代器玩耍并执行各种函数
1. 使用 for loop 来打印一个字符串的单个字母
Python string是一个字符序列. 如果在你的任何一个编程应用程序中,你需要单独通过一个字符串的字符,你可以在这里使用为循环。
**这就是如何为你工作。
1word="anaconda"
2for letter in word:
3 print (letter)
出发点:
1a
2n
3a
4c
5o
6n
7d
8a
为什么这种循环工作是因为Python将一个字符串
视为字符串,而不是看整个字符串。
2. 使用 for loop 重复 Python 列表或 tuple
列表和 Tuples是可迭代的对象. 让我们看看我们现在可以如何绕过这些对象中的元素。
1words= ["Apple", "Banana", "Car", "Dolphin" ]
2for word in words:
3 print (word)
出发点:
1Apple
2Banana
3Car
4Dolphin
** 现在,让我们继续前进,并在这里绕过一个的元素工作。
1nums = (1, 2, 3, 4)
2
3sum_nums = 0
4
5for num in nums:
6 sum_nums = sum_nums + num
7
8print(f'Sum of numbers is {sum_nums}')
9
10# Output
11# Sum of numbers is 10
3、Python 用于循环
当我们有一个为循环的内部另一个为循环时,它被称为(/community/tutorials/python-for-loop-example)。
考虑上面的列表示例. 对于循环打印单个单词从列表. 但如果我们想要打印单个字符的每个单词在列表中,而不是?
这是一个为循环嵌入的工作更好的地方。第一个循环(父母循环)将通过单词一个接一个,第二个循环(孩子循环)将通过每个单词的字符循环。
1words= ["Apple", "Banana", "Car", "Dolphin" ]
2for word in words:
3 #This loop is fetching word from the list
4 print ("The following lines will print each letters of "+word)
5 for letter in word:
6 #This loop is fetching letter for the word
7 print (letter)
8 print("") #This print is used to print a blank line
出发点( )
Python for loop with range() 函数
Python range()是 嵌入式函数之一。当你想要为循环运行特定次数时,或者你需要指定一个要打印的对象范围时,范围函数非常有效。
1for x in range(3):
2 print("Printing:", x)
3
4# Output
5
6# Printing: 0
7# Printing: 1
8# Printing: 2
范围函数除了开始和停止之外还有另一个参数. 这是 步骤参数. 它告诉范围函数在每个计数之间跳过多少个数字。
在下面的示例中,我使用了数字3作为步骤,你可以看到输出数字是前面的数字 + 3。
1for n in range(1, 10, 3):
2 print("Printing with step:", n)
3
4# Output
5
6# Printing with step: 1
7# Printing with step: 4
8# Printing with step: 7
5、打破声明为 loop
打破声明被用来提前退出为循环,当满足特定条件时被用来打破为循环。
假设我们有一个数字列表,我们想检查一个数字是否存在,我们可以重复数字列表,如果发现了这个数字,就离开循环,因为我们不需要继续重复剩余的元素。
在这种情况下,我们将使用 [Python if else 条件]( / 社区 / 教程 / python-if-else-elif) 以及我们的为循环。
1nums = [1, 2, 3, 4, 5, 6]
2
3n = 2
4
5found = False
6for num in nums:
7 if n == num:
8 found = True
9 break
10
11print(f'List contains {n}: {found}')
12
13# Output
14# List contains 2: True
继续声明 with for loop
我们可以使用 a for loop 内部的连续陈述来跳过对特定条件的 loop 体的执行。
假设我们有一个数字列表,我们想要打印正数的总和,我们可以使用连续陈述来跳过负数的循环。
1nums = [1, 2, -3, 4, -5, 6]
2
3sum_positives = 0
4
5for num in nums:
6 if num < 0:
7 continue
8 sum_positives += num
9
10print(f'Sum of Positive Numbers: {sum_positives}')
Python for loop with an else 与其他块的循环
我们可以使用 else 块与一个 Python for loop. The else 块仅在 for loop 没有通过破断声明结束时执行。
假设我们有一个函数来打印数字的总和,如果并且只有如果所有数字都是平的。
如果存在一个奇怪的数字,我们可以使用 break statement 终止 for loop. 我们可以在其他部分中打印总数,以便只有当 for loop 正常执行时才能打印。
1def print_sum_even_nums(even_nums):
2 total = 0
3
4 for x in even_nums:
5 if x % 2 != 0:
6 break
7
8 total += x
9 else:
10 print("For loop executed normally")
11 print(f'Sum of numbers {total}')
12
13# this will print the sum
14print_sum_even_nums([2, 4, 6, 8])
15
16# this won't print the sum because of an odd number in the sequence
17print_sum_even_nums([2, 4, 5, 8])
18
19# Output
20
21# For loop executed normally
22# Sum of numbers 20
结论
Python 中的 for loop 与其他编程语言非常相似. 我们可以使用 break 和 continue 语句用 for loop 来改变执行。
我希望你从上面的教程中获得了一些有趣的想法. 如果你有任何问题,请在下面的评论中告诉我们。