Python中的for循环的基础

简介: Python中的for循环的基础

Python中的for循环的基础

1.1 for循环的语法

在Python中,for循环的基本语法如下:

for 变量 in 可迭代对象: 
# 循环体 
# 对每个元素执行的操作

这里,变量在每次迭代时会被赋予可迭代对象中的下一个元素的值,直到遍历完所有元素。

1.2 示例:遍历列表

fruits = ['apple', 'banana', 'cherry'] 
for fruit in fruits: 
print(fruit)

这段代码会依次打印出列表fruits中的每个水果名称。

二、for循环的进阶使用

2.1 使用range()函数

range()函数是Python中用于生成数字序列的函数,常与for循环结合使用以进行数值迭代。

# 打印0到4(不包括4)的数字 
for i in range(5): 
print(i) 

# 指定起始值和结束值 
for i in range(2, 5): 
print(i) 

# 指定起始值、结束值和步长 
for i in range(0, 10, 2): 
print(i)

2.2 遍历字符串

字符串在Python中也是可迭代对象,因此可以使用for循环遍历字符串中的每个字符。

greeting = "Hello, World!" 
for char in greeting: 
print(char)

2.3 列表推导式(List Comprehension)

虽然列表推导式本身不是for循环的扩展,但它基于for循环的概念,提供了一种更简洁的方式来创建列表。

# 使用列表推导式生成平方数的列表 
squares = [x**2 for x in range(10)] 
print(squares) 

# 过滤条件 
even_squares = [x**2 for x in range(10) if x % 2 == 0] 
print(even_squares)

三、for循环的嵌套

在for循环中,我们还可以嵌套另一个或多个for循环,以实现对复杂数据结构(如二维列表)的遍历。

3.1 遍历二维列表

matrix = [ 
[1, 2, 3], 
[4, 5, 6], 
[7, 8, 9] 
] 

for row in matrix: 
for item in row: 
print(item, end=' ') 
print() # 换行

四、for循环中的break和continue

4.1 break语句

break语句用于立即退出循环体,即使循环条件仍然为真。

for i in range(1, 10): 
if i == 5: 
break 
print(i) 
# 输出1到4

4.2 continue语句

continue语句用于跳过当前循环的剩余语句,并继续下一次循环。

for i in range(1, 10): 
if i % 2 == 0: 
continue 
print(i) 
# 输出1, 3, 5, 7, 9

五、for循环与字典

字典是Python中另一种重要的数据结构,它存储键值对。for循环可以用来遍历字典的键、值或键值对。

5.1 遍历字典的键

person = {'name': 'John', 'age': 30, 'city': 'New York'} 
for key in person: 
print(key)

5.2 遍历字典的值

for value in person.values(): 
print(value)

5.3 遍历字典的键值对

for key, value in person.items(): 
print(f"{key}: {value}")

六、列表推导式与生成器表达式

列表推导式(List Comprehension)和生成器表达式(Generator Expression)提供了简洁的方式从现有的可迭代对象中创建列表和生成器,它们在背后也是利用了for循环的原理。
列表推导式示例

# 使用列表推导式创建平方列表 
numbers = [1, 2, 3, 4, 5] 
squared = [x**2 for x in numbers] 
print(squared) 

# 条件列表推导式 
squared_if_odd = [x**2 for x in numbers if x % 2 != 0] 
print(squared_if_odd)

生成器表达式示例

# 生成器表达式,按需生成值,节省内存 
numbers = (1, 2, 3, 4, 5) 
squared_gen = (x**2 for x in numbers) 
for square in squared_gen: 
print(square)

for循环在文件处理中的应用
在Python中,for循环经常用于逐行读取文件内容。这是通过迭代文件对象(打开文件后返回的)来实现的。
示例:读取文件内容

with open('example.txt', 'r') as file: 
for line in file: 
print(line.strip()) # strip()去除行尾的换行符

七、for循环在字典遍历中的应用

字典(dict)是Python中另一种重要的数据结构,它存储键值对。for循环可以直接遍历字典的键、值或键值对。
示例:遍历字典

person = {'name': 'Alice', 'age': 30, 'city': 'New York'} 

# 遍历键 
for key in person: 
print(key) 

# 遍历值 
for value in person.values(): 
print(value) 

# 遍历键值对 
for key, value in person.items():
相关文章
|
3月前
|
Python
掌握Python循环:从基础到应用的完整指南
掌握Python循环:从基础到应用的完整指南
|
4月前
|
存储 Python
Python中的for循环
Python中的for循环
|
4月前
|
Python 容器
Python中的for循环用法详解,一文搞定它
Python中的for循环用法详解,一文搞定它
103 2
|
4月前
|
存储 Python
Python for循环
Python for循环
34 0
|
4月前
|
Python
在Python中for循环
在Python中for循环
33 1
|
4月前
|
Python
python中的for循环
python中的for循环
48 7
|
10月前
|
Python
13 python - for循环
13 python - for循环
24 0
|
数据库管理 Python
Python|对for循环的基本运用
Python|对for循环的基本运用
57 0
|
Python
Python(11)--for循环
Python(11)--for循环
146 0
Python(11)--for循环
|
Java 程序员 Shell
python中for循环的详细用法
python使用过程中for循环的详细用法。教会你深度理解python中的for循环
556 0