First of all, hello everybody :)

I'm startint to learn Python and i have question about keyboard input.

e.g. I want to enter grades (e.g. 10, 4, 4, 2) and calculate average.

So i guess it should be:

grades = raw_input("Enter grades:")
print "Grades:", grades

And finaly - question.. How do I get those entered numbers for further calculations?

Thanks in advance.

This session of the python interpreter should enlight you

>>> grades = raw_input("Enter grades:")
Enter grades:10, 4, 4, 2
>>> print grades
10, 4, 4, 2
>>> # grades is a string (str). To examine it we print its repr
... 
>>> print repr(grades)
'10, 4, 4, 2'
>>> # the string is separated by commas. Let's split it
... 
>>> gradelist = grades.split(",")
>>> print gradelist
['10', ' 4', ' 4', ' 2']
>>> # gradelist is a list of strings. Some of them have white space
... # around the numbers. Let's remove this white space
... 
>>> gradelist = [s.strip() for s in gradelist]
>>> print gradelist
['10', '4', '4', '2']
>>> # now let's convert these strings to floating point numbers
... 
>>> gradelist = [float(s) for s in gradelist]
>>> print gradelist
[10.0, 4.0, 4.0, 2.0]
>>> # let's sum these numbers
... 
>>> bigsum = sum(gradelist)
>>> print bigsum
20.0
>>> # what is the number of grades ?
... 
>>> print len(gradelist)
4
>>> # let's compute the average
... 
>>> average = bigsum/len(gradelist)
>>> print average
5.0
commented: nice +13

Now I've got it. Thanks a lot

I have another question about counting so i'll use this theme for it.

I have nubers list = [85, 25, 56, 11, 57, 52, 47, 72, 11, 94];

Average is 51 and need to count how many numbers from this list are bigger than average. How can I do this?

I have another question about counting so i'll use this theme for it.

I have nubers list = [85, 25, 56, 11, 57, 52, 47, 72, 11, 94];

Average is 51 and need to count how many numbers from this list are bigger than average. How can I do this?

In python, the boolean True has integer value 1, so you can write

>>> L = [85, 25, 56, 11, 57, 52, 47, 72, 11, 94]
>>> sum((x > 51) for x in L)
6

Damn.. Now i see where sleeping in lectures leads ;/

And another, and I hope the last question..

I need to select numbers bigger than average (51) from the same list and assign to new list e.g. new_list = [numbers >51].

Damn.. Now i see where sleeping in lectures leads ;/

And another, and I hope the last question..

I need to select numbers bigger than average (51) from the same list and assign to new list e.g. new_list = [numbers >51].

Ok, here it is

new_list = [ x for x in L if x > 51 ]

Read the section about list comprehensions in the python documentation.

nl = [85, 25, 56, 11, 57, 52, 47, 72, 11, 94]
avg = float(sum(nl))/len(nl)
print ('Average is %.1f and from list %i are bigger than that.' %
       ((avg, len([x for x in nl if x > avg]))))

Sorry, did not see earlier answers as they where in other page of answers!

I've done almost everything I needed, thanks for you, except one thing. So this will be the last..

So, I have new_list = [52, 56, 57, 72, 85, 94]. I need to get difference between numbers from the list and average [52-51=1, ..., 72-51=21 and so on..], then raise the square of each, sum all them and divide from list length.

nl = [85, 25, 56, 11, 57, 52, 47, 72, 11, 94]
avg = float(sum(nl))/len(nl)
print ('Average is %.1f and from list %i are bigger than that.' %
       ((avg, len([x for x in nl if x > avg]))))

Sorry, did not see earlier answers as they where in other page of answers!

This will be useful for me in future. Thanks

o, I have new_list = [52, 56, 57, 72, 85, 94]. I need to get difference between numbers from the list and average [52-51=1, ..., 72-51=21 and so on..], then raise the square of each, sum all them and divide from list length

Process each number and use a list if you want to store more than one number.

for num in new_list:
    total += num
    square_num =
    square_total += square_num
    average_dif = average_num - num:
etc
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.