Python Forum
create new variable that sums the array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
create new variable that sums the array
#1
Hi Folks,

Could you please help me to understand how to create new variable that sums the array

create another variable that sums the array arg_numbers. Finish the line that starts with numbs_sum = by calculating the sum of all of numbers in the argument.

1. numbers = [13, 5102, 45, 2301.40, 203, 1502, 3]

numbers_sum = sum(numbers)
if len(numbers) >= 1:
    print(numbers_sum/len(numbers))
else:
    print(0)
Reply
#2
If this is your data:

numbers = [13, 5102, 45, 2301.40, 203, 1502, 3]
You call the function sum, which returns the sum and assign it to a name.
total = sum(numbers)
total is now the sum of numbers.
I can be a float, but in your case it's an int because all input values are integers.

Then you can print the value:
print("Sum of number is:", total)
https://docs.python.org/3/library/functions.html#sum
For statistics, you could use: https://docs.python.org/3/library/statistics.html
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
[quote='DeaD_EyE' pid='126814' dateline='1601033064']
If this is your data:

numbers = [13, 5102, 45, 2301.40, 203, 1502, 3]
You call the function sum, which returns the sum and assign it to a name.
total = sum(numbers)
total is now the sum of numbers.
I can be a float, but in your case it's an int because all input values are integers.

Then you can print the value:
print("Sum of number is:", total)
Hi Da Bishop,

Thanks for your prompt response the real query as below.

numbers = [13, 5102, 45, 2301.40, 203, 1502, 3]
numbers_sum = sum(numbers)
if len(numbers) >= 1:
    print(numbers_sum/len(numbers))
else:
    print(0)
# Complete the mean function 
[python]def mean(_____):
    numbs_sum = ______
    if ________:
        return _______
    else:
        return _______ 
# Test code - Do not change anything below this line
print(mean(numbers))
 
step 1:-
Fill in the blank in the mean function by providing the argument, which we will call - arg_numbers.
def mean(arg_numbers):
step2 :-
Now let's create another variable that sums the array arg_numbers. Finish the line that starts with numbs_sum = by calculating the sum of all of numbers in the argument.
numbs_sum = ______
so I struck in this step 2 level.
Reply
#4
Line 2 does that
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie here. Create an array from file data? Rayj00 2 1,249 Jan-13-2023, 01:35 PM
Last Post: perfringo
  Python Coding Help: Calculating Sums bperez 9 3,538 Jan-18-2021, 07:25 AM
Last Post: perfringo
  Beginner: Exporting variable/array using OSC rkk16 4 2,428 May-27-2020, 01:08 PM
Last Post: rkk16
  Loop of sums MathFreak 2 2,625 Mar-20-2019, 10:07 PM
Last Post: MathFreak

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020