您现在的位置是:首页 > 技术教程 正文

python for循环20例

admin 阅读: 2024-03-24
后台-插件-广告管理-内容页头部广告(手机)

python for循环20例

以下是20个关于Python中for循环的例子及其中文说明:

1.遍历列表:

fruits = [‘apple’, ‘banana’, ‘cherry’]
for fruit in fruits:
print(fruit)
# 输出:apple、banana、cherry
2.遍历字符串:

str = ‘Hello, World!’
for char in str:
print(char)
# 输出:H、e、l、l、o、、、W、o、r、l、d、!

3.遍历数字范围:

for i in range(10):
print(i)
# 输出:0、1、2、3、4、5、6、7、8、9

4.遍历用户输入:

for user_input in input("Please enter something: ").split():
print(user_input)
# 用户输入:hello world,输出:hello、world

5.遍历字典:

person = {‘name’: ‘John’, ‘age’: 28, ‘city’: ‘New York’}
for key, value in person.items():
print(key, “:”, value)
# 输出:name: John、age: 28、city: New York

6.列表解析式(一):

numbers = [1, 2, 3, 4, 5]
squares = []
for num in numbers:
squares.append(num ** 2)
print(squares)
输出:[1, 4, 9, 16, 25]

7.列表解析式(二):

numbers = [1, 2, 3, 4, 5]
squares = [num ** 2 for num in numbers]
print(squares)
输出:[1, 4, 9, 16, 25]

8.嵌套for循环:

fruits = [‘apple’, ‘banana’, ‘cherry’]
for fruit in fruits:
for i in range(3):
print(fruit)
输出:apple、apple、apple、banana、banana、banana、cherry、cherry、cherry,每个水果各输出三次。

9.使用break提前结束循环:

for i in range(10):
if i == 5:
break
print(i)
#输出:0、1、2、3、4
这个例子中,当i等于5时,使用break语句提前结束循环,因此只输出了0到4。

10.使用continue跳过当前循环:

for i in range(10):
if i == 5:
continue
print(i)
输出:0、1、2、3、4、6、7、8、9
这个例子中,当i等于5时,使用continue语句跳过当前循环,因此输出了0到4以及6到9。

11.使用enumerate遍历列表并获取索引:

fruits = [‘apple’, ‘banana’, ‘cherry’]
for index, fruit in enumerate(fruits):
print(“Index:”, index, “Fruit:”, fruit)
输出:Index: 0 Fruit: apple、Index: 1 Fruit: banana、Index: 2 Fruit: cherry
这个例子中,使用enumerate函数遍历列表并同时获取索引和元素的值。

12.使用zip函数同时遍历两个列表:

list1 = [‘a’, ‘b’, ‘c’]
list2 = [1, 2, 3]
for item1, item2 in zip(list1, list2):
print(“Item1:”, item1, “Item2:”, item2)
输出:Item1: a Item2: 1、Item1: b Item2: 2、Item1: c Item2: 3
这个例子中,使用zip函数将两个列表组合成一个元组序列,并同时遍历两个列表的元素。

13.range函数的使用:
for i in range(-5, 5):
print(i, end=’ ')
# 输出:-5、-4、-3、-2、-1、0、1、2、3、4。共10个。

14.列表解析式(三):

numbers = [1, 2, 3, 4, 5]
squares = [num ** 2 for num in numbers if num % 2 == 0]
print(squares)
#输出:[4, 16]
这个例子中,使用列表解析式创建了一个新的列表squares,其中包含了numbers列表中所有偶数的平方。

15.嵌套列表解析式:

numbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
squares = [[num ** 2 for num in sublist] for sublist in numbers]
print(squares)
输出:[[1, 4, 9], [16, 25, 36], [49, 64, 81]]
这个例子中,使用嵌套列表解析式创建了一个新的列表squares,其中包含了numbers列表中每个子列表中所有元素的平方。

16.使用lambda表达式创建匿名函数:

fruits = [‘apple’, ‘banana’, ‘cherry’]
sorted_fruits = sorted(fruits, key=lambda fruit: fruit[0])
print(sorted_fruits)
输出:[‘apple’, ‘banana’, ‘cherry’]
这个例子中,使用lambda表达式创建了一个匿名函数,该函数根据水果名称的第一个字母进行排序。

17.使用map函数将函数应用于列表的每个元素:

numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda num: num ** 2, numbers))
print(squared)
输出:[1, 4, 9, 16, 25]
这个例子中,使用map函数将一个匿名函数应用于numbers列表的每个元素,并将结果存储在新的列表squared中。

18.使用filter函数过滤列表中的元素:

numbers = [1, 2, 3, 4, 5]
even_numbers = list(filter(lambda num: num % 2 == 0, numbers))
print(even_numbers)
输出:[2, 4]
这个例子中,使用filter函数过滤出numbers列表中的偶数,并将结果存储在新的列表even_numbers中。

19.使用reduce函数对列表进行累积计算:

from functools import reduce
numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(product)
输出:120
这个例子中,使用reduce函数对numbers列表中的所有元素进行累积乘法计算,并将结果存储在变量product中。

20.使用生成器函数创建生成器对象:

def fibonacci(n):
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, a + b

fib = fibonacci(10)
print(next(fib))
print(next(fib))
print(next(fib))
print(next(fib))
print(next(fib))
#输出:0、1、1、2、3
这个例子中,我们定义了一个生成器函数fibonacci,它使用yield语句返回每个斐波那契数列的值,并使用一个循环来生成前n个斐波那契数列的值。我们使用next函数从生成器对象中获取下一个值。

如有任何疑问,私信沟通互相学习

标签:
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

在线投稿:投稿 站长QQ:1888636

后台-插件-广告管理-内容页尾部广告(手机)
关注我们

扫一扫关注我们,了解最新精彩内容

搜索
排行榜