Python Basics: How to Sum Numbers from 1 to 10 (Step-by-Step)
# This program calculates the sum of all whole numbers from 1 to 10.
# Initialize a variable named 'total_sum' to 0 to hold the running total.
total_sum = 0
# Start a 'for' loop that iterates through a sequence of numbers.
# range(1, 11) generates numbers starting from 1 up to (but not including) 11.
for number in range(1, 11):
# Add the current number in the loop to the 'total_sum' variable.
total_sum += number
# Print the final result to the console, embedding the total_sum variable in the string.
print(f"The sum of numbers from 1 to 10 is: {total_sum}")
# Initialize a variable named 'total_sum' to 0 to hold the running total.
total_sum = 0
# Start a 'for' loop that iterates through a sequence of numbers.
# range(1, 11) generates numbers starting from 1 up to (but not including) 11.
for number in range(1, 11):
# Add the current number in the loop to the 'total_sum' variable.
total_sum += number
# Print the final result to the console, embedding the total_sum variable in the string.
print(f"The sum of numbers from 1 to 10 is: {total_sum}")
Comments
Post a Comment