Control Constructs, Python, and Mean

Why do we need control constructs, such as while, or for loop in Python? Certainly we need more control over our code.

But there are more.

That’s the first reason. The second reason is we want to compute a huge number of values in a short period of time.

And by that way, we can find the Mean of a large set of numbers. As a result control constructs are an efficient tool in Data Science.

We need control constructs or loop, because computational thinking gives us enough power to write sequence of steps or recipe for doing any repetitive job.

Suppose we need to find out average of a finite set of different numbers. Again that represents Mean in Data Science. Isn’t it?

We might imagine doing this for 5, 6 or 10. However, when the list grows and reaches 100000, it becomes impossible.

How control constructs can find the Mean

However, in Data science finding the value of Mean is important. We have seen that before. Not only finding the Mean of any set pf numbers, but also in other Data science operations, control constructs play crucial role.

In short, control constructs, such as “while or for loop”, becomes a part Data Science Algorithm.

We need to find out some solution to do that. Suppose our application is programmed to take 100000 inputs from a file where the numbers are stored. Can we enter them manually and see what would be the output?

Consider a program like the following one:

# we will compute average of six numbers by manual addition

    print("Enter first number: ")
    first = int(input())
    print("Enter second number: ")
    second = int(input())
    print("Enter third number: ")
    third = int(input())
    print("Enter fourth number: ")
    fourth = int(input())
    print("Enter fifth number: ")
    fifth = int(input())
    print("Enter sixth number: ")
    sixth = int(input())
    result = 0.00
    result = (first + second + third + fourth + fifth + sixth) / 6
    print("The average of six numbers is : " + str(result))

For 6 numbers it is OK. The output gives us the proper value. The Mean or average of a set of small numbers.

# output

    Enter first number: 
    1
    Enter second number: 
    2
    Enter third number: 
    3
    Enter fourth number: 
    4
    Enter fifth number: 
    5
    Enter sixth number: 
    6
    The average of six numbers is : 3.5

However, we can better handle this type of operations using the ‘while’ statement.

What we have seen in the above code makes us believe that we need an action that should be repeatedly executed.

We add two numbers and get a total. Next, we add the third number with the running total.

It will go on as long as the number of values processed is less than the finite set of numbers that we want to add and then divide by that number to find the average.

Control to find Mean or average in Data Science

In data science, we call this type of average Mean.

Now we need to map that problem to our program domain with the help of a control construct like ‘while’ statement.

Because the ‘while’ statement deals execution of any repetitive action better than any other statement, we can write it using ‘natural language’ this way:

while(the number of values processed is less than the number of finite set of numbers)

    We take input
    
    The running total is adding more input numbers as long as the loop continues
    
    After each cycle the number of values processed is increased by 1
    
    The loop ends as the  number of values processed is equal to the number of finite set of numbers
    
    Now we have the grand total of all the numbers belonging to the finite set of numbers
    
    To get the average we divide the total by the the number of finite set of numbers

As a result, we can write a simple python code as follows.

# we will compute average of six numbers by iteration using while loop

    totalNumberToCompute = 6

    # since number of iteration yet to be taken
    numberOfIteration = 0
    # we have not got the total addition of all numbers
    total = 0.00
    print("Please enter " + str(totalNumberToCompute) + " numbers : ")
    print()

    while(numberOfIteration < totalNumberToCompute):
        value = 0.00
        value = float(input())
        total += value
        numberOfIteration += 1

    averageOfSixNumbers = 0.00
    averageOfSixNumbers = total / numberOfIteration
    print("The average of six numbers is : " + str(averageOfSixNumbers))

And here goes the same output that we have seen in the previous code.

# output

    Please enter 6 numbers : 

    1
    2
    3
    4
    5
    6
    The average of six numbers is : 3.5

In this section, we have learned many important concepts.

You have probably noticed that we are handling with discrete numbers.

We are also talking about a finite set of numbers. It is an integral part of discrete mathematical operations.

Control Constructs and discrete mathematics

In discrete mathematics, we almost always quantify.

Therefore, we always check the ‘existential’ logical expression like ‘if there is’ or ‘if there exists’, etc. Moreover, we also check for ‘global’ values that is meant ‘for all’.

For all the numbers inside the finite set of numbers, we are adding them one after another.

In short, that leads us to a grand total.

In addition, we also count how many numbers are there and divide the grand total by the total numbers present inside the finite set.

As we progress, we will see how these concepts come handy for the functions.

How set theory is pertinent for collections or data structure, etc.

Moreover, this concept will come often in Data Science.

And by the way, we have also learned what algorithm means actually! To make the long story short, it is a sequence of instructions to solve a problem.

In the next section, we will find more about Python, Mathematics and, of course Data science.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

Courses at Educative

GitHub repository

Flutter, Dart and Algorithm

Twitter

Comments

Leave a Reply