To sum the integers from 1 to n in Python is a relatively simple topic. Here are some ways you can refer to and apply. Hope the article is helpful to you.
Sum the integers from 1 to n in Python
Use the sum() function
You can use the range() function as a parameter to the sum () function to sum integers from 1 to n.
Syntax:
range(start,end,step)
Parameters:
- start: starting value. The default is 0.
- end: end value.
- step: Set the difference between each iteration. It can be positive or negative if no default step is 1.
The range() function returns a sequence of numbers, starting at 0 and ending at a specified number, and the step is an optional argument.
Note: The range(i) function prints a starting value of 0, so the ending value will be i-1.
Example:
- Illustrate the integer n equals 100 so that you can easily imagine.
- Use the sum() function with the range() function as the argument to sum the numbers from 1 to n (for example, n equals 100).
# Illustrate n = 100 for you to visualize n = 100 # Use the sum () function with the range() function as a parameter to calculate the sum from 1 to n myTotal = sum(range(1, n + 1)) print('Sum of integers from 1 to n:', myTotal)
Output:
Sum of integers from 1 to n: 5050
Use the for loop
Example:
- Illustrate the integer n equals 100 so that you can easily imagine.
- Use the for loop in conjunction with the range() function to sum integers from 1 to n (eg n equals 100).
# Illustrate n = 100 for you to visualize n = 100 # Use the for loop in conjunction with the range() function to sum integers from 1 to n mySum = 0 for i in range(1, n+1): mySum += i print('Sum of integers from 1 to n:', mySum)
Output:
Sum of integers from 1 to n: 5050
Use the arithmetic progression formula
Given an array of integers from 1 to n. To calculate the sum of a series of numbers, we use the arithmetic progression formula:
n*(n+1)/2
Example:
- Illustrate the integer n equals 100 so that you can easily imagine.
- Declare a function as an arithmetic progression formula.
- Use that function to sum integers from 1 to n (eg n = 100).
# Illustrate n = 100 for you to visualize n = 100 def arithProgsForml(n): return n * (n + 1) // 2 print('Sum of integers from 1 to n:', arithProgsForml(100))
Output:
Sum of integers from 1 to n: 5050
Use the numpy.sum () function
You can use the built-in numpy.sum() function in the numpy package. The function calculates the sum of the elements, the sum of the columns, or the sum of the rows in a particular array.
The numpy.range() function is a parameter to the numpy.sum() function.
Syntax:
numpy.sum (arr, axis = None, dtype = None, out = None, keepdims = <no value>, initial = <no value>)
Example:
- Import the numpy module.
- Illustrate the integer n equals 100 so that you can easily imagine.
- Use the numpy.sum() function with the numpy.range() function as the argument to sum the numbers from 1 to n (for example, n equals 100).
import numpy as np # Illustrate n = 100 for you to visualize n = 100 # Use the function to sum integers from 1 to n result = np.sum(np.arange(1, n + 1)) print('Sum of integers from 1 to n:', result)
Output:
Sum of integers from 1 to n: 5050
Summary
That’s all about how to sum the integers from 1 to n in Python. Thank you for reading my post. Maybe you have found your idea.
Maybe you are interested:
- The equivalent of sum() for multiplication in Python
- Round a float to 2 decimals in python
- Round a number up to the nearest integer in python
My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.
Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java