Skip to content
Programs · Program 7-6

Q.Program 7-6: Write a program using a user defined function myMean() to calculate the mean of floating values stored in a list.

Punjab PsebTextbookSubjective· 3mImportance★★★★★est
18% · 7/40 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Program 7-6 passes an entire list to a user-defined function; myMean() walks the list once, accumulating the running total and the element count, then divides to get the mean.

The idea. A list is a single object, so it can be passed to a function as one argument. The function does not need to know the list's length in advance — a for loop visits every element, and a counter tracks how many there were.

# Program 7-6: mean of floating values stored in a list

def myMean(myList):
    total = 0
    count = 0
    for i in myList:
        total = total + i     # running sum
        count = count + 1     # how many elements
    mean = total / count
    print("The mean of the list is", mean)

marks = [2.5, 3.5, 4.0, 6.0]
myMean(marks)

Dry run for [2.5, 3.5, 4.0, 6.0]:

itotalcount
2.52.51
3.56.02
4.010.03
6.016.04

mean = 16.0 / 4 = 4.0

Expected output

The mean of the list is 4.0
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.