如何在 Python 中的 for 循环中求和
在 Python 的 for 循环中求和:
- 声明一个新变量并将其设置为 0。
- 使用 for 循环遍历一系列数字。
- 将变量重新分配为其值加上当前数字。
my_list = [2, 4, 6, 8]
# ✅ 使用for循环求和
total = 0
for num in my_list:
total += num
print(total) # 👉️ 20
# ----------------------
# ✅ 使用 for 循环对 range 内的数字求和
total_2 = 0
for num in range(1, 5):
total_2 += num
print(total_2) # 👉️ 10
print(list(range(1, 5))) # 👉️ [1, 2, 3, 4]
# ----------------------
# ✅ 使用 for 循环从用户输入中获取的数字总和
# 👇️ user enters 1 2 3 4
user_input = input('Enter space-separated numbers:')
my_list = list(map(int, user_input.split()))
print(my_list) # 👉️ [1, 2, 3, 4]
total_3 = 0
for num in my_list:
total_3 += num
print(total_3) # 👉️ 10
上面的示例中,我们输入 3 和 6。运行结果如下
我们使用 for 循环来对列表中的数字求和。
第一步是声明一个新变量并将其初始化为 0。
在每次迭代中,我们使用 +=
运算符将变量重新分配为其当前值加上当前数字。
以下 2 行代码实现了相同的结果:
-
total += num
-
total = total + num
这是一个使用较长的重新分配语法的示例。
my_list = [2, 4, 6, 8]
total = 0
for num in my_list:
total = total + num
print(total) # 👉️ 20
如果我们需要使用 for 循环添加某个 range
内的数字,请使用 range()
方法创建该范围。
total_2 = 0
for num in range(1, 5):
total_2 += num
print(total_2) # 👉️ 10
print(list(range(1, 5))) # 👉️ [1, 2, 3, 4]
range
类通常用于在 for 循环中循环特定次数,并采用以下参数:
- start 表示范围开始的整数(默认为 0)
- stop 范围结束的整数,但不包括提供的整数
- step Range 将由从 start 到 stop 的每 N 个数字组成(默认为 1)
如果我们需要在 for 循环中对用户输入的数字求和,请使用 input()
函数。
# 👇️ 用户输入 1 2 3 4
user_input = input('Enter space-separated numbers:')
my_list = list(map(int, user_input.split()))
print(my_list) # 👉️ [1, 2, 3, 4]
total_3 = 0
for num in my_list:
total_3 += num
print(total_3) # 👉️ 10
input
函数接受一个可选的提示参数并将其写入标准输出,而没有尾随的换行符。
注意
:即使用户输入了一个数字,input()
函数也保证返回一个字符串。
我们使用 str.split()
函数在每个空格上拆分字符串。
str.split()
方法使用分隔符将字符串拆分为子字符串列表。
该方法采用以下 2 个参数:
- separator 在每次出现分隔符时将字符串拆分为子字符串
- maxsplit 最多完成最大拆分(可选)
如果在字符串中找不到分隔符,则返回仅包含 1 个元素的列表。
我们在示例中使用了空格分隔符,但我们可以使用适合自己用例的任何其他分隔符。
这是一个在每个逗号上拆分用户提供的字符串的示例。
# 👇️ user enters 1,2,3,4
user_input = input('Enter comma-separated numbers:')
my_list = list(map(int, user_input.split(',')))
print(my_list) # 👉️ [1, 2, 3, 4]
total_3 = 0
for num in my_list:
total_3 += num
print(total_3) # 👉️ 10
拆分字符串后,我们得到一个字符串列表,因此我们使用 map()
函数将列表中的每个字符串转换为整数。
# 👇️ user enters 1,2,3,4
user_input = input('Enter comma-separated numbers:')
# 👇️ ['1', '2', '3', '4']
print(user_input.split(','))
map()
函数将一个函数和一个可迭代对象作为参数,并使用可迭代对象的每个项调用该函数。
map()
函数将每个字符串传递给 int()
类并将其转换为整数。
相关文章
Solution for Flickering During Vue Template Parsing
发布时间:2025/02/21 浏览次数:103 分类:Vue
-
Solution for Flickering During Vue Template Parsing, Recently, while working on a project, I noticed that when the internet speed is slow, the screen flickers and the expression message appears. This happens because when the internet speed i
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。