{"id":33,"date":"2018-06-04T05:09:16","date_gmt":"2018-06-03T23:39:16","guid":{"rendered":"https:\/\/pynative.com\/?p=33"},"modified":"2021-06-16T18:43:27","modified_gmt":"2021-06-16T13:13:27","slug":"python-program-to-calculate-sum-and-average-of-numbers","status":"publish","type":"post","link":"https:\/\/pynative.com\/python-program-to-calculate-sum-and-average-of-numbers\/","title":{"rendered":"Python program to calculate sum and average of first n natural numbers"},"content":{"rendered":"\n
In this lesson, you will learn how to calculate the sum and average of the first n natural numbers<\/a> in Python.<\/p>\n\n\n\n Also, you will get to know how to calculate the addition and average of user-entered numbers, list of numbers. And the use of built-in function This tutorials is part of Python Basics<\/a>.<\/p>\n\n\n\n Sum and average of Use input() function<\/a> to accept integer number from a user.<\/p> <\/li> Next, run a for loop<\/a> till the entered number using the In each iteration, keep adding the current number into the sum variable to calculate the addition. Use a formula At last, after the loop ends, calculate the average using a formula Program<\/strong>:<\/p>\n\n\nsum()<\/code>.<\/p>\n\n\n\n
Table of contents<\/h2>
Sum and average of first n natural numbers<\/h2>\n\n\n\n
n<\/code> numbers in Python<\/p>
range()<\/a><\/code> function. In each iteration, we will get the next number till the loop reaches the last number, i.e.,
n<\/code>.<\/p> <\/li>
sum = sum + current number<\/code>.<\/p> <\/li>
average = sum \/ n<\/code>. Here, The
n<\/code> is a number entered by the user.<\/p> <\/li><\/ol><\/div>\n\n\n\n