Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 12th, 2013, 06:36 PM
Vanta Vanta is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 Vanta User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 24 sec
Reputation Power: 0
A small program to compute averages

I am currently taking a python programming class and we are working on a program that requires finding averages of some values from an imported file, but I am having some issues creating a while or for loop that can compute averages.

Lets say I have a list newlist = [1, 2, 3, 4, 5]

I want to create a loop that will compute the mean of these values, which of course is 3. I know I will need a few variables but I do not know how to utilize them. Of course, I will need to convert the values in newlist to integers as well.

Does anyone know how to do something like this with basic syntax? Of course we have covered conditionals, while loops, for loops, string methods, booleans, etc, but I just cannot seem to make a program that will find the average and print it out.

Some help would be appreciated!

Thanks!

Reply With Quote
  #2  
Old February 12th, 2013, 10:05 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,360 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 55 m 30 sec
Reputation Power: 383
If you knew both the sum of the items in the list and its length you could solve the problem. Something like

average = sum(LIST) / len(LIST)

print(average)


sum and len are python functions, you don't need to write them.

>>> LIST = [1,2,3,4,5]
>>> average = sum(LIST) / len(LIST)
>>> print(average)
3


Wow! It works. Let's try again.
Code:
>>> A = [3, 4]
>>> print(sum(A) / len(A))  # trouble
3
>>> import fractions
>>> print(fractions.Fraction(sum(A), len(A)))
7/2
>>> print(sum(A) / float(len(A)))    # resolution
3.5
>>> 
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old February 13th, 2013, 12:06 PM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 482 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 22 h 23 m 21 sec
Reputation Power: 63
FYI ...

In Python2 x / y means integer division if numbers x and y are integers

You can change that to floating point division by making at least one of the numbers a float

In Python3 x / y means floating point division and x // y means integer division. It doesn't matter if x or y are integers or floats.
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

Last edited by Dietrich : February 13th, 2013 at 12:12 PM.

Reply With Quote
  #4  
Old February 13th, 2013, 12:14 PM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 482 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 22 h 23 m 21 sec
Reputation Power: 63
@hovh
Start a new thread! Tagging on to an existing unrelated thread is rather rude!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > A small program to compute averages

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap