×

Python Statistical Module

Python Statistical Module

The Python statistical module provides various functions that we can use in our Python program, to perform mathematical statistics operations on the numerical data given to us. The statistical module comes with the Python installation, so we don’t need to install it externally.

Importing statistical Module

By using the following syntax in our Python program, we can import Python statistic module in a particular python program:

import statistics

After importing the statistical module in with the help of above-mentioned syntax, we are able to use various statistics function and perform statistical operations on the numeric data given to us.

In this part of tutorial, we are going to learn and use the following statistic functions in our Python program:

1. The mean() function

2. The median() function

3. The mode() function

4. The stdev() function

5. The median_low() function

6. The median_high() function

With the help of above listed statistic functions, we will perform operations on numeric data given to us. So, let’s learn about each of them one by one and understand how they work when used in our Python program.

Python Statistical Module Built-in Functions.

Following are the important built-in methods of Python Statistical Module.

1. The mean() function:

The mean() function of statistical module is used to calculate the arithmetic mean of the datasets of given number. The arithmetic mean of some given numbers is also called as average of that number. When we use mean() function in our Python program, it returns the average of dataset that we used in the function.

Syntax:

We will use the following syntax in the given example to calculate the arithmetic mean of numeric data present in example:

statistics.mean(GivenData)

Let's understand mean() function through a Python program used in following example:

Example: Calculating arithmetic mean of a given dataset of numbers using mean() function:

 # importing python statistical module in the program
 import statistics   
 # list of positive integer numbers as a given dataset
 GivenData = [51, 26, 7, 43, 2, 67, 8, 14, 29, 53]
 # calculating mean of given dataset with mean() function
 a = statistics.mean(GivenData)    
 # Printing the mean in the output of program
 print("The Arithmetic Mean of the dataset used in our Python program i.e., GivenData is :", a) 

Output:

The Arithmetic Mean of the dataset used in our Python program i.e., GivenData is : 30

Explanation: In the above program, first we have imported statistical module in our Python program. Then we have defined a dataset of number with named GivenData that contain all the positive integer numbers in it. Then we calculated the arithmetic mean of the dataset we have defined by using the mean() function on the dataset.

After calculating the mean, we have printed the result in the output of program by using print statement. As we can see in the output, the result shown to us is the arithmetic mean or the average of the dataset we have used in the above Python program i.e., GivenData.

2. The median() function:

The median() function is used to calculate statistical median of a given dataset of numbers. The median of a given dataset is the middle most value from the total numbers given to us. We can even use negative numbers while defining dataset of numbers in our Python program.

Syntax: We will use the following syntax in the given example to calculate the median of numeric data present in example:

statistics.median(GivenData)

Let's understand median() function through a Python program used in following example:

Example: Calculating median of a given dataset of numbers using median() function:

 # importing python statistical module in the program
 import statistics   
 # list of positive and negative integer numbers as a given dataset
 GivenData = [51, 26, 7, -43, 2, 67, -8, 14, 29, 53]
 # calculating median of given dataset with median() function
 a = statistics.median(GivenData)    
 # Printing the median in the output of program
 print("The Statistical Median of the dataset used in our Python program i.e., GivenData is :", a) 

Output:

The Statistical Median of the dataset used in our Python program i.e., GivenData is : 20.0

Explanation: In the above Python program, first we have imported statistical module in our Python program.

Then we have defined a dataset of number with named GivenData that have all the positive and negative integer numbers in it.

Then we calculated the median of the dataset we have defined by using the median() function on the dataset. After calculating the median, we have printed the result in the output of program by using print statement. As we can see in the output, the result shown to us is the median of the dataset we have used in the above Python program i.e., GivenData.

3. The mode() function:

The mode() function is used to calculate the statistical mode of a given dataset of numbers. This dataset of number either can be positive or can be negative. The mode of a dataset of numbers means that the most occurring number or value from that given dataset or list of numbers.

Syntax:

We will use the following syntax in the given example to calculate the mode of numeric data present in example:

statistics.mode(GivenData)

Let's understand mode() function through a Python program used in following example:

Example: Calculating mode of a given dataset of numbers using mode() function:

 # importing python statistical module in the program
 import statistics   
 # list of positive and negative integer numbers as a given dataset
 GivenData = [55, 26, 7, -43, 2, 67, -8, 14, 92, 53, 27, 26, 2, 14, 55, 14, 26, 7, 67, 26]
 # calculating mode of given dataset with mode() function
 a = statistics.mode(GivenData)    
 # Printing the mode in the output of program
 print("The Statistical Mode of the dataset used in our Python program i.e., GivenData is :", a) 

Output:

The Statistical Mode of the dataset used in our Python program i.e., GivenData is: 26

Explanation: In the above Python program, first we have imported statistical module in our Python program. Then we have defined a dataset of number with named GivenData that have all the positive and negative integer numbers in it.

Then we calculated the mode of the dataset we have defined by using the mode() function on the dataset. After calculating the mode, we have printed the result in the output of program by using print statement.

As we can see in the output, the result shown to us is the mode or the most occurring number from the dataset we have used in the above Python program i.e., GivenData.

4. The stdev() function:

The stdev() function uses in our Python program when we have to calculate statistical standard deviation of a given sample which is available from the list of number provided to us. This dataset of number either can be positive or can be negative.

Syntax: We will use the following syntax in the given example to calculate the standard deviation of numeric data present in example:

statistics.stdev(GivenData)

Let's understand stdev() function through a Python program used in following example:

Example: Calculating standard deviation of a given dataset of numbers using stdev() function:

 # importing python statistical module in the program
 import statistics   
 # list of positive and negative integer numbers as a given dataset
 GivenData = [55, 26, 7, -43, 2, 67, -8, 14, 92, 53, 27, 26, 2, 14, 55, 14, 26, 7, 67, 26]
 # calculating standard deviation of given dataset with stdev() function
 a = statistics.stdev(GivenData)    
 # Printing the standard deviation in the output of program
 print("The Statistical Standard Deviation of the dataset used in our Python program i.e., GivenData is :", a) 

Output:

The Statistical Standard Deviation of the dataset used in our Python program i.e., GivenData is : 31.07626442361301

Explanation: In the above Python program, first we have imported statistical module in our Python program.

Then we have defined a dataset of number with named GivenData that have all the positive and negative integer numbers in it. Then we calculated the standard deviation of the dataset we have defined by using the stdev() function on the dataset.

 After calculating the standard deviation, we have printed the standard deviation as result in the output of program by using print statement. As we can see in the output, the result shown to us is the standard deviation of the dataset we have used in the above Python program i.e., GivenData.

5. The median_low() function:

The median_low() function we uses in our Python program when we have to calculate statistical low median of a given dataset of numbers. We can even use negative numbers while defining dataset of numbers in our Python program.

Syntax: We will use the following syntax in the given example to calculate the low median of numeric data present in example:

statistics.median_low(GivenData)

Let's understand median_low() function through a Python program used in following example:

Example: Calculating low median of a given dataset of numbers using median_low() function:

 # importing python statistical module in the program
 import statistics
 # list of positive and negative integer numbers as a given dataset
 GivenData = [55, 26, 7, -43, 2, 67, -8, 14, 92, 53, 27, 26, 2, 14, 55, 14, 26, 7, 67, 26]
 # calculating low median of given dataset with median_low() function
 a = statistics.median_low(GivenData)    
 # Printing the low median in the output of program
 print("The Statistical Low Median of the dataset used in our Python program i.e., GivenData is :", a) 

Output:

The Statistical Low Median of the dataset used in our Python program i.e., GivenData is : 26

Explanation: In the above Python program, first we have imported statistical module in our Python program.

 Then we have defined a dataset of number with named GivenData that have all the positive and negative integer numbers in it. Then we calculated the low median of the dataset we have defined by using the median_low() function on the dataset.

After calculating the low median, we have printed the low median as result in the output of program by using print statement. As we can see in the output, the result shown to us is the low median of the dataset we have used in the above Python program i.e., GivenData.

6. The median_high() function:

The median_high() function we uses in our Python program when we have to calculate statistical high median of a given dataset of numbers. We can even use negative numbers while defining dataset of numbers in our Python program.

Syntax: We will use the following syntax in the given example to calculate the high median of numeric data present in example:

statistics.median_high(GivenData)

Let's understand median_high() function through a Python program used in following example:

Example: Calculating high median of a given dataset of numbers using median_high() function:

 # importing python statistical module in the program
 import statistics
 # list of positive and negative integer numbers as a given dataset
 GivenData = [55, 26, 7, -43, 2, 67, -8, 14, 92, 53, 27, 26, 2, 14, 55, 14, 26, 7, 67, 26]
 # calculating high median of given dataset with median_high() function
 a = statistics.median_high(GivenData)    
 # Printing the high median in the output of program
 print("The Statistical High Median of the dataset used in our Python program i.e., GivenData is :", a) 

Output:

The Statistical High Median of the dataset used in our Python program i.e., GivenData is: 26

Explanation: In the above Python program, first we have imported statistical module in our Python program.

Then we have defined a dataset of number with named GivenData that have all the positive and negative integer numbers in it.

Then we calculated the high median of the dataset we have defined by using the median_high() function on the dataset.

After calculating the high median, we have printed the high median as result in the output of program by using print statement.

 As we can see in the output, the result shown to us is the high median of the dataset we have used in the above Python program i.e., GivenData.


Related Topics

Introducing modern python computing in simple packages

Python is the language for newly evolving technologies and has become one of the most popular computer languages in the world. Python is used in everything right from a simple...

3 minutes read.

Python String swapcase() method

Python String swapcase() method The string.swapcase() method in Python returns a copy of the string with uppercase characters converted to lowercase and vice versa. Syntax string.swapcase() Parameter NA Return This method returns a copy of the string...

1 minute read.

Python Interpreter

In this tutorial, we will go through the basic knowledge about what an interpreter is and how we use it in the python programming language. It is one of the...

3 minutes read.

Python Recursion

Recursion is one of the most interesting yet important concepts of any programming language. If you want to be a good programmer or data scientist, then you should better have...

4 minutes read.

*Args and **Kwargs in Python

Let’s know about ** (dual star / asterisk) and * (star / asterisk) are operators in Python. These stars are used in Python for parameters. Args and kwargs are special...

3 minutes read.

Reverse a String in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has...

4 minutes read.

Standard GUI Unit Converter using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

12 minutes read.

Python Program for Tower of Hanoi

In this tutorial, we will examine and learn about the Python coding used to create the video game Tower of Hanoi. Before seeing the game's step-by-step implementation in Python, we...

3 minutes read.

Inheritance in Python

Inheritance in Python Object-Oriented Programming provides reusable patterns to the code for restricting the redundancy in development projects. One of the basic principles of Object-Oriented Programming that helps achieve recyclable code...

8 minutes read.

What is Python online compiler?

The compiler is a program that is used to scan an entire high-level program (source code) and it translates the scanned program into machine code. Compilers convert .py source code into...

5 minutes read.

Python Network Programming

In network programming, python plays a very important role. Python provides full support for encoding and decoding data and network protocol in its standard library. Writing a network program in...

3 minutes read.

Python Set issubset() method

Python Set issubset() method The set.issubset() method in Python returns True if all items in the set exists in the specified set, otherwise it returns False. Syntax set.issubset(set1) Parameter set- This parameter represents the set to check whether...

2 minutes read.

Dynamic Typing in Python

Many key factors for developing a great programming language include how it manages its memory space. This memory space largely depends on empty memory boxes called variables that one creates...

3 minutes read.

How to write a program in python?

How to write a program in python? In python, writing programs is not a big deal. What we need to work on is our logic and some basic rules of this...

4 minutes read.

Python Dictionary fromkeys() method

Python Dictionary fromkeys() method The dictionary.fromkeys() method in Python creates a new dictionary from the given sequence of elements and returns a dictionary with the specified keys and values. Syntax dictionary.fromkeys(sequence[, value]) Parameter sequence- This...

1 minute read.

Writing to a CSV file in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

6 minutes read.

Python frozenset()

Python frozenset() class The frozenset() class in Python returns a new frozenset object, optionally with elements taken from iterable. Syntax class frozenset([iterable]) Parameter iterable: This parameter represents an iterable object, like list, set, tuple etc. Return This class returns an unchangeable...

1 minute read.

Sentence to python vector

Conversion of a Sentence to Vector in Python Before starting the tutorial, let’s just recap about the vector and the respective package that has to be imported in Python. Python Vector: Putting simply,...

3 minutes read.

Add in Dictionary Python

Dictionary in python: Dictionaries are a useful data configuration for storing information in Python because they can mimic real-world data arrangements where a particular value exists for a particular key. The...

4 minutes read.

How to reverse a string in Python

In Python language, we have a few ways to reverse a string. In this article, let us discuss these ways and understand the suitability of these ways in different scenarios...

5 minutes read.