Table of Contents
Introduction. 2
How to execute a Python file. 2
Windows. 2
macOS. 2
The program.. 3
How to code the program.. 3
The list 4
Sorting. 4
Minimum and Maximum.. 5
Average (mean) 6
Median. 6
Mode. 7
STD (Standard Deviation) 7
Rounding. 7
 

 

 

Introduction

We Will Write a Custom Essay Specifically
For You For Only $13.90/page!


order now

Python programming language is widely used for many different tasks, it is a high-level language, it supports many programing paradigms, for example OOP(object oriented programming), functional, procedural.

Python is an interpreted language which means that the syntax is read and then executed, while when its compiled which is not commonly used in python like Cpython, the syntax is transformed into an executable form in the memory before the running.

In this program we will use python for data analysis, python is commonly used for data analysis due to the easiness of the syntax and the libraries that makes the work much easier, but in this program no (math) or (statistics) library are used.

How to execute a Python file

Python files normally have the extension .py, and to execute them you need to have python environmental variables, can be downloaded from the original Website.

Windows

After installing python for windows, python files can be executed by opening CMD(Command Prompt) from the start menu, then you need to call the commands:

python  file-path

then you need to add the file, either by adding the path or by dragging the file to the terminal, for example:

python “D:AhmedUNIMMscratch filesmathmatical.py”  

And the file will be executed.

 

macOS

after installing Python 3.6 for the macOS, open terminal, the easiest way by pressing command + spacebar and enter terminal then press enter.

Then you need to call the python interpreter with the version 3 and the path of the file, for example:

1.  python3 file – path  

then you need to add the file, either by adding the path or by dragging the file to the terminal, for example:

python3 Desktopmathmatical.py  

And the file will be executed.

The program

This program can find some statistical operations from a list of numbers, like the minimum value and the maximum value of the list, the average, the most common number, Standard deviation, median, and to return a sorted list without using any built-in library for any of the operations mentioned above. The list is entered either manually or randomly generated numbers, the list consist of 10 integer numbers with a value between minimum of 0 and maximum of a 100.

How to code the program

To generate random numbers, python has a built-in library to generate random number:

1.  import random  

then we can make a welcome text:

1.  print(“TEXT”)  

the program need to ask if we want to enter the numbers manually or by generating numbers, this can be achieved by making a shortcut, for example when the user inputs M then the program will ask the user to input a list of 10 numbers separated by comma ( , ), or R to randomly generate numbers:

1.  selection = input(“for random numbers please use R”                           

2.                    “, for manual entry please use M: “)  

Now we need to make the program check the input from the Selection, if its R then it will generate the numbers, and if it is M it will ask to enter the numbers manually, and if the input is not M neither it will print a message tells the user its wrong:

1.  if selection == “R” or selection == “r”: “):  

2.    

3.  elif selection == “M” or selection == “m”:  

4.    

5.    

6.  else:  

7.      print(“Not a selection, please enter a correct letter.”)  

8.      exit()  

 

 

 

The list

after it checks the selection, we need to make the program generate numbers if the selection is R and print the generated list before the sorting:

1.  if selection == “R” or selection == “r”:  

2.      b =   

3.      for i in range(10):  

4.          b.append(random.randint(0,100))  

5.      print(”
“,”The generated list: ” ,b)  

and if the selection is M which means manual entry, it should also check the length of the list which is 10 :

1.  elif selection == “M” or selection == “m”:  

2.      a = input(“enter a list of 10 numbers of range 0-100: “)  

3.      b = int(b) for b in a.split(“,”)  

4.      if len(b) > 10:  

5.          print(“list is too long”)  

6.          exit()  

7.      elif len(b) < 10:   8.          print("list is too short")   9.          exit()list: " ,b)   then the program should check the value of each number in the list and each number should not exceed the value of 100, If a number is higher than 100 it will exit: 1.  for v in b:   2.      if v > 100:  

3.          print(“please enter a list with numbers between 0-100”)  

4.          exit()  

5.      else:  

6.          continue 

Sorting

There are many different ways for sorting in python, normally used the built-in function (sorted) which is the fastest and the easiest way to do the sorting, but in big programs that have many different lists, a different soring function is better, the most popular one is bubblesort, I made this function which is based on the principle of bubblesort by comparing each number to the others

1.  def mysort(list):  

2.      for index in range(1,len(list)):  

3.          value = listindex  

4.          i = index-1  

5.          while i>=0:  

6.              if value < listi:   7.                  listi+1 = listi   8.                  listi = value   9.                  i -= 1   10.             else:  break   Minimum and Maximum There are two ways to find the minimum and the maximum, logically and pythonic, what is the difference? The difference in this program for example that you can find it by comparing each number to the rest and find the highest one between them and the lowest value: 1.  def minimum(x):   2.      mini = x0   3.      for i in x0::   4.          if i < mini:   5.              mini = i   6.      return mini   7.     8.  def maximum(x):   9.      mymax = x0   10.     for number in x:   11.         if mymax < number:   12.             mymax = number   13.     return mymax   as seen that both are identically the same except the smaller and bigger signs. But there is an easier way in one line of code, in this program we sorted the list by using a function, and the sorting is basically putting the numbers of the list in order from the smallest value to the biggest one, so after we sort the list we can find the minimum by just printing the first number from the list, and the maximum by printing the last number in that list: 1.  maximum = b-1   2.  minimum = b0   we used 0 as the first index because in programming the first thing is 0 not 1, and in python and many other languages like swift, rather than making a loop or calling a number to find the index of the last number, we can use -1 which going to the last index in the list because normally there is no such an index before the 0, so the -1 goes to the last index and -2 to second last index and so on.         Average (mean) Finding the average of a list of numbers is easy as normal life, dividing the sum of all the number by the count of numbers the result is the average: 1.  def mean(x):   2.      listlength = len(x)   3.      listsum = sum(x)   4.      listaverage = listsum / listlength   5.      return listaverage   also, there is a built-in function called (mean) can be used by importing the module statistics: 1.  import statistics   2.  a = list   3.  MEAN = mean(a)      Median Finding the median depends on the list, there are two ways, but first we need to sort the numbers, if the count of numbers in the list is even, then we need to find the middle two and we sum them, then we divide them by 2 we get the median, but if the list is odd, it is easier by just finding the middle number, an example showing them both: Even 3, 5, 7, 12, 13, 14, 21, 23, 23, 23, 23, 29, 40, 56 In the middle there are 21 and 23 21 + 23 = 44 then 44 ÷ 2 = 22 Then the median is 22 Odd 5, 7, 12, 13, 14, 21, 23, 23, 23, 23, 29, 39, 40 The middle number is 23 Then the median is 23     Mode To find the most common number in the list there are 2 ways, the first one to use the counter for the module Counter: 1.  from collections import Counter   2.  a = list   3.  mode = Counter(a).most_common(1)   or by making a function that can read not only one list but a whole file full of lists: 1.  def mode(array):   2.      most = max(map(array.count, array)) if array else None   3.      mset = set(filter(lambda x: array.count(x) == most, array))   4.      return mset if set(array) - mset else "list does not have a mode"   which counts how many each number is available and compare the result to the others and prints the highest, and if there are two numbers with the same frequency it will return them both. STD (Standard Deviation) It is the square root of the variance, since the variance is at least 0, and the STD cannot be a negative value. 1.  def SD(x):   2.      variance = 0   3.      mn = mean(x)   4.      for e in x:   5.          variance += (e - mn) ** 2   6.      variance /= len(x)   7.     8.      return float(variance**(.5))   we found the square root by adding exponential value to the variance, python has an exponentiation operator using the double star (**)  Rounding  When we round a number in any programming language, the number should be a flout or Double in some languages like Swift, 1.  a = 13,43353245432   2.  b = round(a)   we can round a number to a specific decimal place by adding a key to the function like:(number, the number of decimal places) 1.  a = 13.43353245432   2.  b = round(a, 5)  # b = 13.43353    

Written by
admin
x

Hi!
I'm Colleen!

Would you like to get a custom essay? How about receiving a customized one?

Check it out