Author name: Vikram Chiluka

Python statistics.pstdev() Method with Examples

statistics.pstdev() Method in Python:

The statistics.pstdev() method computes the standard deviation from a population as a whole.

The standard deviation measures how evenly distributed the numbers are.

A high standard deviation indicates that the data is dispersed, whereas a low standard deviation indicates that the data is tightly clustered around the mean.

Tip: Unlike variance, the standard deviation is expressed in the same units as the data.

Tip: The standard deviation is equal to the square root of the sample variance.

Look at the statistics.stdev() method to calculate the standard deviation from a sample of data.

Syntax:

statistics.pstdev(data, xbar)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

xbar: This is Optional. The arithmetic mean of the given data (can also be a second moment around a point that is not the mean). If omitted (or set to None), the mean is calculated automatically.

Note: It is important to note that if the data is empty, it returns a StatisticsError.

Return Value:

Returns a float value representing the given data’s population standard deviation.

Examples:

Example1:

Input:

Given list = [10, 20, 40, 15, 30]

Output:

The standard deviation of the given list items [10, 20, 40, 15, 30] =  10.770329614269007

Example2:

Input:

Given list = [3, 4, 5, 1, 3, 2]

Output:

The standard deviation of the given list items [3, 4, 5, 1, 3, 2] = 1.2909944487358056

statistics.pstdev() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.pstdev() method that computes the standard deviation from a population as a whole.
  • Store it in another variable.
  • Print the standard deviation of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [10, 20, 40, 15, 30]
# Pass the given list as an argument to the statistics.pstdev() method that
# computes the standard deviation from a population as a whole.
# Store it in another variable.
rslt = statistics.pstdev(gvn_lst)
# Print the standard deviation of the given list items.
print("The standard deviation of the given list items", gvn_lst, "= ", rslt)

Output:

The standard deviation of the given list items [10, 20, 40, 15, 30] =  10.770329614269007

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the statistics.pstdev() method that computes the standard deviation from a population as a whole.
  • Store it in another variable.
  • Print the standard deviation of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the statistics.pstdev() method that
# computes the standard deviation from a population as a whole.
# Store it in another variable.
rslt = statistics.pstdev(gvn_lst)
# Print the standard deviation of the given list items.
print("The standard deviation of the given list items", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = 3 4 5 1 3 2
The standard deviation of the given list items [3, 4, 5, 1, 3, 2] = 1.2909944487358056

Python statistics.pstdev() Method with Examples Read More »

Python statistics.mode() Method with Examples

statistics.mode() Method in Python:

Statistics.mode() computes the mode (central tendency) of a numeric or nominal data set.

The mode of a set of data values is the most frequently occurring value. It is the most likely value at which the data will be sampled. A mode of a continuous probability distribution is frequently defined as any value x at which its probability density function has a local maximum value, implying that any peak is a mode.

Syntax:

statistics.mode(data)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

Note: It is to be noted that if the data is empty, it returns a StatisticsError.

Return Value:

Returns a float or nominal value that represents the given data’s mode.

Examples:

Example1:

Input:

Given list = ['hello', 'this', 'is', 'btechgeeks', 'hello']

Output:

The mode of the given list items ['hello', 'this', 'is', 'btechgeeks', 'hello'] =  hello

Explanation:

Here 'hello' occurs most frequently. Hence the mode is 'hello'.

Example2:

Input:

Given list = [3, 4, 2, 5, 6, 3, 3, 1, 3]

Output:

The mode of the given list items [3, 4, 2, 5, 6, 3, 3, 1, 3] = 3

statistics.mode() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.mode() method that computes the mode (which occurs most frequently) of the given list items.
  • Store it in another variable.
  • Print the mode of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = ['hello', 'this', 'is', 'btechgeeks', 'hello']
# Pass the given list as an argument to the statistics.mode() method that
# computes the mode (which occurs most frequently) of the given list items.
# Store it in another variable.
rslt = statistics.mode(gvn_lst)
# Print the mode of the given list items.
print("The mode of the given list items", gvn_lst, "= ", rslt)

Output:

The mode of the given list items ['hello', 'this', 'is', 'btechgeeks', 'hello'] =  hello

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the statistics.mode() method that computes the mode (which occurs most frequently) of the given list items.
  • Store it in another variable.
  • Print the mode of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the statistics.mode() method that
# computes the mode (which occurs most frequently) of the given list items.
# Store it in another variable.
rslt = statistics.mode(gvn_lst)
# Print the mode of the given list items.
print("The mode of the given list items", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = 3 4 2 5 6 3 3 1 3
The mode of the given list items [3, 4, 2, 5, 6, 3, 3, 1, 3] = 3

Python statistics.mode() Method with Examples Read More »

Python statistics.median_high() Method with Examples

statistics.median_high() Method in Python:

The statistics.median_high() method computes the data set’s high median. Before calculating the high median, the data is also sorted in ascending order using this method.

Note: It should be noted that if the number of data values is odd, it will return the exact middle value. If there are an even number of data values, it returns the larger of the two middle values.

Syntax:

statistics.median_high(data)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

Note: It is to be noted that if the data is empty, it returns a StatisticsError.

Return Value:

Returns a float value representing the data’s high median (middle value).

Examples:

Example1:

Input:

Given list = [10, 20, 40, 15, 30, 13, 17]

Output:

The high median of the given list items [10, 20, 40, 15, 30, 13, 17] =  17

Example2:

Input:

Given list = [2, 1, 3, 5, 7, 8]

Output:

The high median of the given list items [2, 1, 3, 5, 7, 8] = 5

statistics.median_high() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.median_high() method that computes the high median of the given list items.
  • Store it in another variable.
  • Print the high median of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [10, 20, 40, 15, 30, 13, 17]
# Pass the given list as an argument to the statistics.median_high() method that
# computes the high median of the given list items.
# Store it in another variable.
rslt = statistics.median_high(gvn_lst)
# Print the high median of the given list items.
print("The high median of the given list items", gvn_lst, "= ", rslt)

Output:

The high median of the given list items [10, 20, 40, 15, 30, 13, 17] =  17

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the statistics.median_high() method that computes the high median of the given list items.
  • Store it in another variable.
  • Print the high median of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the statistics.median_high() method that
# computes the high median of the given list items.
# Store it in another variable.
rslt = statistics.median_high(gvn_lst)
# Print the high median of the given list items.
print("The high median of the given list items", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = 2 1 3 5 7 8
The high median of the given list items [2, 1, 3, 5, 7, 8] = 5

Python statistics.median_high() Method with Examples Read More »

Python statistics.median_low() Method with Examples

statistics.median_low() Method in Python:

The statistics.median_low() method computes the data set’s low median. Before calculating the low median, the data is also sorted in ascending order using this method.

Note: It should be noted that if the number of data values is odd, it will return the exact middle value. If there are an even number of data values, it returns the smaller of the two middle values.

Syntax:

statistics.median_low(data)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

Note: It is to be noted that if the data is empty, it returns a StatisticsError.

Return Value:

Returns a float value representing the data’s low median (middle value).

Examples:

Example1:

Input:

Given list = [10, 20, 40, 15, 30, 13, 17]

Output:

The low median of the given list items [10, 20, 40, 15, 30, 13, 17] =  17

Example2:

Input:

Given list = [2, 1, 3, 5, 7, 8]

Output:

The low median of the given list items [2, 1, 3, 5, 7, 8] = 3

statistics.median_low() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.median_low() method that computes the low median of the given list items.
  • Store it in another variable.
  • Print the low median of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [10, 20, 40, 15, 30, 13, 17]
# Pass the given list as an argument to the statistics.median_low() method that
# computes the low median of the given list items.
# Store it in another variable.
rslt = statistics.median_low(gvn_lst)
# Print the low median of the given list items.
print("The low median of the given list items", gvn_lst, "= ", rslt)

Output:

The low median of the given list items [10, 20, 40, 15, 30, 13, 17] =  17

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the statistics.median_low() method that computes the low median of the given list items.
  • Store it in another variable.
  • Print the low median of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the statistics.median_low() method that
# computes the low median of the given list items.
# Store it in another variable.
rslt = statistics.median_low(gvn_lst)
# Print the low median of the given list items.
print("The low median of the given list items", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = 2 1 3 5 7 8
The low median of the given list items [2, 1, 3, 5, 7, 8] = 3

Python statistics.median_low() Method with Examples Read More »

Python statistics.median_grouped() Method with Examples

statistics.median_grouped() Method in Python:

The statistics.median grouped() method computes the median of grouped continuous data as the 50th percentile.

This method treats the data points as continuous data and computes the 50% percentile median by first determining the median range using the specified interval width (default is 1), and then interpolating within that range using the position of the values from the data set that fall within that range.

GMedian = L + interval * (N / 2 – CF) / F

is the mathematical formula for Grouped Median.

  • L = the lower limit of the median interval
  • interval = the width of the interval
  • N represents the total number of data points.
  • CF = The number of data points that are less than the median interval.
  • The number of data points in the median interval is denoted by F.

Syntax:

statistics.median_grouped(data, interval)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

interval: This is Optional. The interval between classes. The default value is 1.

Note: It is important to note that if the data is empty, it returns a StatisticsError.

Return Value:

Returns a float value that represents the median of grouped continuous data and is calculated as the 50% percentile.

Examples:

Example1:

Input:

Given list = [10, 20, 40, 15, 30]

Output:

The median of grouped continuous data [10, 20, 40, 15, 30] =  20.0

Example2:

Input:

Given list = [9, 8, 5, 1]
Given interval = 3

Output:

The median of grouped continuous data [9, 8, 5, 1] =  6.5

statistics.median_grouped() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.median_grouped() method that computes the median of grouped continuous data(given list) as the 50th percentile.
  • Store it in another variable.
  • Print the median of grouped continuous data(given list) as the 50th percentile.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [10, 20, 40, 15, 30]
# Pass the given list as an argument to the statistics.median_grouped() method
# that computes the median of grouped continuous data(given list) as the 50th
# percentile.
# Store it in another variable.
rslt = statistics.median_grouped(gvn_lst)
# Print the median of grouped continuous data(given list) as the 50th percentile.
print("The median of grouped continuous data", gvn_lst, "= ", rslt)

Output:

The median of grouped continuous data [10, 20, 40, 15, 30] =  20.0
With giving interval

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the interval as static input and store it in another variable.
  • Pass the given list, interval as the arguments to the statistics.median_grouped() method that computes the median of grouped continuous data(given list) as the 50th percentile for the given interval width.
  • Store it in another variable.
  • Print the median of grouped continuous data(given list) as the 50th percentile.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [9, 8, 5, 1]
# Give the interval as static input and store it in another variable.
gvn_intrvl = 3
# Pass the given list, interval as the arguments to the statistics.median_grouped()
# method that computes the median of grouped continuous data(given list) as
# the 50th percentile for the given interval width.
# Store it in another variable.
rslt = statistics.median_grouped(gvn_lst, gvn_intrvl)
# Print the median of grouped continuous data(given list) as the 50th percentile.
print("The median of grouped continuous data", gvn_lst, "= ", rslt)

Output:

The median of grouped continuous data [9, 8, 5, 1] =  6.5

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Pass the given list as an argument to the statistics.median_grouped() method that computes the median of grouped continuous data(given list) as the 50th percentile.
  • Store it in another variable.
  • Print the median of grouped continuous data(given list) as the 50th percentile.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the statistics.median_grouped() method
# that computes the median of grouped continuous data(given list) as the 50th
# percentile.
# Store it in another variable.
rslt = statistics.median_grouped(gvn_lst)
# Print the median of grouped continuous data(given list) as the 50th percentile.
print("The median of grouped continuous data", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = 5 8 9 4 1
The median of grouped continuous data [5, 8, 9, 4, 1] = 5.0
With giving interval

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Give the interval as user input using the int(input()) function and store it in another variable.
  • Pass the given list, interval as the arguments to the statistics.median_grouped() method that computes the median of grouped continuous data(given list) as the 50th percentile for the given interval width.
  • Store it in another variable.
  • Print the median of grouped continuous data(given list) as the 50th percentile.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
    
# Give the interval as user input using the int(input()) function and store it in another variable.
gvn_intrvl = int(input("Enter some random number = "))
# Pass the given list, interval as the arguments to the statistics.median_grouped()
# method that computes the median of grouped continuous data(given list) as
# the 50th percentile for the given interval width.
# Store it in another variable.
rslt = statistics.median_grouped(gvn_lst, gvn_intrvl)
# Print the median of grouped continuous data(given list) as the 50th percentile.
print("The median of grouped continuous data", gvn_lst, "= ", rslt)

Output:

Enter some random List Elements separated by spaces = 4 6 7 2
Enter some random number = 5
The median of grouped continuous data [4, 6, 7, 2] = 3.5

Python statistics.median_grouped() Method with Examples Read More »

Python statistics.median() Method with Examples

statistics.median() Method in Python:

The statistics.median() method computes the given data set’s median (middle value). Before calculating the median, this method sorts the data in ascending order.

Median is calculated as:

Median = (n + 1) / 2 

where n is the number of values in a set of data.

The data must first be sorted in ascending order before the median can be calculated. The median is the number that falls in the middle.

Note: It should be noted that if the number of data values is odd, it will return the exact middle value. If the number of data values is even, the average of the two middle values is returned.

Syntax:

statistics.median(data)

Parameters

data: This is Required. It is the data values that will be used (it can be any sequence, list, or iterator).

Note: It is to be noted that if the data is empty, it returns a StatisticsError.

Return Value:

Returns a float value representing the data’s median (middle value).

Examples:

Example1:

Input:

Given list = [10, 20, 40, 15, 30]

Output:

The median of the given list items [10, 20, 40, 15, 30]  =  20

Note:

The given list is first sorted in ascending order before the calculating 
the median.

Example2:

Input:

Given list = [2, 3, 1, 6, 5, 4]

Output:

The median of the given list items [2, 3, 1, 6, 5, 4] = 3.5

Explanation:

Here median = (6+1)/2 = 3.5

statistics.median() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the statistics.median() method that computes the median (middle value) of the given list items.
  • Store it in another variable.
  • Print the median (middle value) of the given list items.
  • The Exit of the Program.

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as static input and store it in a variable.
gvn_lst = [10, 20, 40, 15, 30]
# Pass the given list as an argument to the statistics.median() method that
# computes the median (middle value) of the given list items.
# Store it in another variable.
rslt = statistics.median(gvn_lst)
# Print the median (middle value) of the given list items.
print("The median of the given list items", gvn_lst, " = ", rslt)

Output:

The median of the given list items [10, 20, 40, 15, 30]  =  20

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import statistics module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Pass the given list as an argument to the statistics.median() method that computes the median (middle value) of the given list items.
  • Store it in another variable.
  • Print the median (middle value) of the given list items.
  • The Exit of the Program

Below is the implementation:

# Import statistics module using the import keyword.
import statistics
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))

# Pass the given list as an argument to the statistics.median() method that
# computes the median (middle value) of the given list items.
# Store it in another variable.
rslt = statistics.median(gvn_lst)
# Print the median (middle value) of the given list items.
print("The median of the given list items", gvn_lst, " = ", rslt)

Output:

Enter some random List Elements separated by spaces = 2 3 1 6 5 4
The median of the given list items [2, 3, 1, 6, 5, 4] = 3.5

Python statistics.median() Method with Examples Read More »

Python cmath.tan() Method with Examples

cmath.tan() Method in Python:

The cmath.tan() method returns the complex number’s tangent.

Syntax:

cmath.tan(x)

Parameters

x: This is Required. A number from which to calculate the tangent.

  • If the value is not a number, a TypeError is returned.

Return Value:

Returns a complex value that represents the complex number’s tangent.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The given complex number's (3+4j) tangent value = 
(-0.0001873462046294784+0.999355987381473j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The given complex number's (5+2j) tangent value = 
(-0.020553016568255655+1.0310080051524912j)

Note: The above input format is for dynamic input.

cmath.tan() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.tan() method that returns the given complex number’s tangent value.
  • Store it in another variable.
  • Print the tangent value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.tan() method that
# returns the the given complex number's tangent value.
# Store it in another variable.
rslt = cmath.tan(complexnumb)
# Print the tangent value of the given complex number.
print("The given complex number's", complexnumb,
      "tangent value = ")
print(rslt)

Output:

The given complex number's (3+4j) tangent value = 
(-0.0001873462046294784+0.999355987381473j)

Similarly, try for the other examples

import cmath
complexnumb = -2-1j
rslt = cmath.tan(complexnumb)
print("The given complex number's", complexnumb,
      "tangent value = ")
print(rslt)

Output:

The given complex number's (-2-1j) tangent value = 
(0.24345820118572534-1.16673625724092j)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given complex number as an argument to the cmath.tan() method that returns the given complex number’s tangent value.
  • Store it in another variable.
  • Print the tangent value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.tan() method that
# returns the the given complex number's tangent value.
# Store it in another variable.
rslt = cmath.tan(complexnumb)
# Print the tangent value of the given complex number.
print("The given complex number's", complexnumb,
      "tangent value = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) tangent value = 
(-0.020553016568255655+1.0310080051524912j)

Python cmath.tan() Method with Examples Read More »

Python cmath.sqrt() Method with Examples

cmath.sqrt() Method in Python:

The cmath.sqrt() method returns the given complex number’s square root.

Note: It should be noted that the number must be greater than or equal to 0.

Syntax:

cmath.sqrt(x)

Parameters

x: This is Required. A number whose square root is to be found.

  • If the number is less than zero, a ValueError is returned.
  • If the value is not a number, a TypeError is returned.

Return Value:

Returns a complex value that represents the complex number’s square root.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The given complex number's (3+4j) square root value = 
(2+1j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The given complex number's (5+2j) square root value = 
(2.27872385417085+0.4388421169022545j)

Note: The above input format is for dynamic input.

cmath.sqrt() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.sqrt() method that returns the given complex number’s square root value.
  • Store it in another variable.
  • Print the square root value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.sqrt() method that
# returns the the given complex number's square root value.
# Store it in another variable.
rslt = cmath.sqrt(complexnumb)
# Print the square root value of the given complex number.
print("The given complex number's", complexnumb,
      "square root value = ")
print(rslt)

Output:

The given complex number's (3+4j) square root value = 
(2+1j)

Similarly, try for the other examples

import cmath
complexnumb = 12
rslt = cmath.sqrt(complexnumb)
print("The given complex number's", complexnumb,
      "square root value = ")
print(rslt)

Output:

The given complex number's 12 square root value = 
(3.4641016151377544+0j)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given complex number as an argument to the cmath.sqrt() method that returns the given complex number’s square root value.
  • Store it in another variable.
  • Print the square root value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.sqrt() method that
# returns the the given complex number's square root value.
# Store it in another variable.
rslt = cmath.sqrt(complexnumb)
# Print the square root value of the given complex number.
print("The given complex number's", complexnumb,
      "square root value = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) square root value = 
(2.27872385417085+0.4388421169022545j)

Python cmath.sqrt() Method with Examples Read More »

Python cmath.log10() Method with Examples

cmath.log10() Method in Python:

The cmath.log10() method returns the complex number’s base-10 logarithm.

There is one continuous branch cut from 0 to – ∞ along the negative real axis.

Syntax:

cmath.log10(x)

Parameters

x: This is Required. Specifies the value for which the base-10 logarithm will be computed.

  • If the value is 0 or a negative number, a ValueError is returned.
  • If the value is not a number, a TypeError is returned.

Return Value:

Returns a complex value that represents a number’s base-10 logarithm.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The given complex number's (3+4j) base-10 logarithm value = 
(0.6989700043360187+0.4027191962733731j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The given complex number's (5+2j) base-10 logarithm value = 
(0.7311989989494779+0.16525181990889787j)

Note: The above input format is for dynamic input.

cmath.log10() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.log10() method that returns the given complex number’s base-10 logarithm value.
  • Store it in another variable.
  • Print the base-10 logarithm value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.log10() method that
# returns the the given complex number's base-10 logarithm value.
# Store it in another variable.
rslt = cmath.log10(complexnumb)
# Print the base-10 logarithm value of the given complex number.
print("The given complex number's", complexnumb,
      "base-10 logarithm value = ")
print(rslt)

Output:

The given complex number's (3+4j) base-10 logarithm value = 
(0.6989700043360187+0.4027191962733731j)

Similarly, try for the other examples

import cmath
complexnumb = -6-2j
rslt = cmath.log10(complexnumb)
print("The given complex number's", complexnumb,
      "base-10 logarithm value = ")
print(rslt)

Output:

The given complex number's (-6-2j) base-10 logarithm value = 
(0.8010299956639811-1.2246418635180674j)

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given complex number as an argument to the cmath.log10() method that returns the given complex number’s base-10 logarithm value.
  • Store it in another variable.
  • Print the base-10 logarithm value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.log10() method that
# returns the the given complex number's base-10 logarithm value.
# Store it in another variable.
rslt = cmath.log10(complexnumb)
# Print the base-10 logarithm value of the given complex number.
print("The given complex number's", complexnumb,
      "base-10 logarithm value = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) base-10 logarithm value = 
(0.7311989989494779+0.16525181990889787j)

Python cmath.log10() Method with Examples Read More »

Python cmath.log() Method with Examples

cmath.log() Method in Python:

The logarithm of a complex value is returned by the cmath.log() method.

This method takes a single argument and returns the natural logarithm of that argument with base e.

This method takes two arguments and returns the logarithm of the first argument (x) multiplied by the base of the second argument (base).

Syntax:

cmath.log(x, base)

Parameters

x: This is Required. Specifies the value for which the logarithm should be calculated.

  • If the value is 0 or a negative number, a ValueError is returned.
  • If the value is not a number, a TypeError is returned.

base: This is Optional. The logarithmic base that should be used. ‘e’ is the default.

Return Value:

Returns a complex value that represents the natural logarithm of a number or the base logarithm of a number.

Changelog: The base parameter has been added.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The given complex number's (3+4j) logarithm value = 
(1.6094379124341003+0.9272952180016122j)

Example2:

Input:

Given realpart = 5
Given imaginary part = 2
Given base value = 4

Output:

The given complex number's (5+2j) logarithm value for the given base{ 4.0 } = 
(1.214495248781893+0.27447733164331733j)

Note: The above input format is for dynamic input.

cmath.log() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

1)With a single argument

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Pass the given complex number as an argument to the cmath.log() method that returns the given complex number’s logarithm value.
  • Store it in another variable.
  • Print the logarithm value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 3+4j
# Pass the given complex number as an argument to the cmath.log() method that
# returns the the given complex number's logarithm value.
# Store it in another variable.
rslt = cmath.log(complexnumb)
# Print the logarithm value of the given complex number.
print("The given complex number's", complexnumb,
      "logarithm value = ")
print(rslt)

Output:

The given complex number's (3+4j) logarithm value = 
(1.6094379124341003+0.9272952180016122j)
2)With two arguments

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the complex number as static input and store it in a variable.
  • Give the base value as static input and store it in another variable.
  • Pass the given complex number, base value as the arguments to the cmath.log() method that returns the given complex number’s logarithm value for the given base.
  • Store it in another variable.
  • Print the logarithm value of the given complex number for the given base.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the complex number as static input and store it in a variable.
complexnumb = 4
# Give the base value as static input and store it in another variable.
gvn_basevalu = 8
# Pass the given complex number, base value as the arguments to the cmath.log()
# method that returns the given complex number's logarithm value for
# the given base.
# Store it in another variable.
rslt = cmath.log(complexnumb, gvn_basevalu)
# Print the logarithm value of the given complex number for the given base.
print("The given complex number's", complexnumb,
      "logarithm value for the given base{", gvn_basevalu, "} = ")
print(rslt)

Output:

The given complex number's 4 logarithm value for the given base{ 8 } = 
(0.6666666666666667+0j)

Method #2: Using Built-in Functions (User Input)

1)With a single argument

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Pass the given complex number as an argument to the cmath.log() method that returns the given complex number’s logarithm value.
  • Store it in another variable.
  • Print the logarithm value of the given complex number.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Pass the given complex number as an argument to the cmath.log() method that
# returns the the given complex number's logarithm value.
# Store it in another variable.
rslt = cmath.log(complexnumb)
# Print the logarithm value of the given complex number.
print("The given complex number's", complexnumb,
      "logarithm value = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) logarithm value = 
(1.6836479149932368+0.3805063771123649j)
2)With two arguments

Approach:

  • Import cmath module(for complex number operations) using the import keyword.
  • Give the real part and imaginary part of the complex number as user input using map(), int(), split().
  • Store it in two variables.
  • Using a complex() function convert those two variables into a complex number and store them in a variable.
  • Give the base value as user input using the float(input()) function and store it in another variable.
  • Pass the given complex number, base value as the arguments to the cmath.log() method that returns the given complex number’s logarithm value for the given base.
  • Store it in another variable.
  • Print the logarithm value of the given complex number for the given base.
  • The Exit of the Program.

Below is the implementation:

# Import cmath module(for complex number operations) using the import keyword.
import cmath
# Give the real part and imaginary part of the complex number as user input
# using map(), int(), split().
# Store it in two variables.
realnumb, imaginarynumb = map(int, input(
    'Enter real part and complex part of the complex number = ').split())
# Using a complex() function convert those two variables into a complex number.
complexnumb = complex(realnumb, imaginarynumb)

# Give the base value as user input using the float(input()) function and
# store it in another variable.
gvn_basevalu = float(input("Enter some random number = "))
# Pass the given complex number, base value as the arguments to the cmath.log()
# method that returns the given complex number's logarithm value for
# the given base.
# Store it in another variable.
rslt = cmath.log(complexnumb, gvn_basevalu)
# Print the logarithm value of the given complex number for the given base.
print("The given complex number's", complexnumb,
      "logarithm value for the given base{", gvn_basevalu, "} = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
Enter some random number = 4
The given complex number's (5+2j) logarithm value for the given base{ 4.0 } = 
(1.214495248781893+0.27447733164331733j)

 

Python cmath.log() Method with Examples Read More »