Please tell me how to find average of n numbers in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please tell me how to find average of n numbers in python

18th Aug 2021, 12:20 PM
Srajan Gupta
Srajan Gupta - avatar
3 Answers
+ 2
Create a for loop (0 to N) and within this loop on every iteration input a number and add it to a variable called, for instance, sum. Then print the sum.
18th Aug 2021, 12:32 PM
Artem šŸ‡ŗšŸ‡¦
Artem šŸ‡ŗšŸ‡¦ - avatar
+ 2
There is default sum() function, donā€™t create variables with such names. You can put your numbers in list, or use range if they go after each other and then divide their sum by lenght (there are also default function len() ) of the list: my_list = [1, 3, 8, 2] average = sum(my_list) / len(my_list)
18th Aug 2021, 3:27 PM
Š›ŠµŠ² ŠŸŠ°Š²Š»Š¾Š²
Š›ŠµŠ² ŠŸŠ°Š²Š»Š¾Š² - avatar
+ 1
lst = [put n numbers in here] sum = 0 for x in lst: sum += x average = sum/len(lst)
18th Aug 2021, 3:10 PM
Chloe
Chloe - avatar