Python

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 »

Python cmath.phase() Method with Examples

cmath.phase() Method in Python:

The phase of a complex number is returned by the cmath.phase() method.

The magnitude and angle of a complex number can be expressed. Phase is the angle formed by a vector (representing a complex number) and the positive x-axis.

Note: It should be noted that the output is always between -π and π.

Syntax:

cmath.phase(x)

Parameters

x: This is Required. The number used to determine the phase of

Return Value:

Returns a float value that represents a complex number’s phase.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

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

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The given complex number's (5+2j) phase value = 
0.3805063771123649

Note: The above input format is for dynamic input.

cmath.phase() 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.phase() method that returns the given complex number’s phase value.
  • Store it in another variable.
  • Print the phase 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.phase() method that
# returns the the given complex number's phase value.
# Store it in another variable.
rslt = cmath.phase(complexnumb)
# Print the phase value of the given complex number.
print("The given complex number's", complexnumb,
      "phase value = ")
print(rslt)

Output:

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

Similarly, try for the other examples

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

Output:

The given complex number's (-2-1j) phase value = 
-2.677945044588987

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.phase() method that returns the given complex number’s phase value.
  • Store it in another variable.
  • Print the phase 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.phase() method that
# returns the the given complex number's phase value.
# Store it in another variable.
rslt = cmath.phase(complexnumb)
# Print the phase value of the given complex number.
print("The given complex number's", complexnumb,
      "phase value = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The given complex number's (5+2j) phase value = 
0.3805063771123649

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

Python cmath.isinf() Method with Examples

cmath.isinf() Method in Python:

The cmath.isinf() method determines whether or not a value is positive or negative infinity.

This method gives the following Boolean value: If the value is infinity, True; otherwise, False.

Syntax:

cmath.isinf(x)

Parameters

x: This is Required. The value used to test for infinity

Return Value:

Returns a bool value that is True if the value is infinite and False otherwise.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The result after applying isinf() method to the given complex number (3+4j)  = 
False

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

Enter real part and complex part of the complex number = 5 2
The result after applying isinf() method to the given complex number (5+2j) = 
False

Note: The above input format is for dynamic input.

cmath.isinf() 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.isinf() method that determines whether a given complex number’s value is positive or negative infinity.
  • Store it in another variable.
  • Print the above result after applying isinf() method to 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.isinf() method
# that determines whether a given complex number's value is positive or
# negative infinity.
# Store it in another variable.
rslt = cmath.isinf(complexnumb)
# Print the above result after applying isinf() method to the given complex number.
print("The result after applying isinf() method to the given complex number", complexnumb,
      " = ")
print(rslt)

Output:

The result after applying isinf() method to the given complex number (3+4j)  = 
False

Similarly, try for the other examples

import cmath
complexnumb = complex(10 + float('inf'))
rslt = cmath.isinf(complexnumb)
print("The result after applying isinf() method to the given complex number", complexnumb,
      " = ")
print(rslt)

Output:

The result after applying isinf() method to the given complex number (inf+0j)  = 
True

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.isinf() method that determines whether a given complex number’s value is positive or negative infinity.
  • Store it in another variable.
  • Print the above result after applying isinf() method to 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.isinf() method
# that determines whether a given complex number's value is positive or
# negative infinity.
# Store it in another variable.
rslt = cmath.isinf(complexnumb)
# Print the above result after applying isinf() method to the given complex number.
print("The result after applying isinf() method to the given complex number", complexnumb,
      " = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The result after applying isinf() method to the given complex number (5+2j) = 
False

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

Python cmath.isnan() Method with Examples

cmath.isnan() Method in Python:

The cmath.isnan() method determines whether a value is a nan (Not a Number).

This method gives the following Boolean value: True if the value is nan, False otherwise.

Syntax:

cmath.isnan(x)

Parameters

x: This is Required. The value used to test for NaN

Return Value:

Returns a bool value that is True if any part of a complex number (real or imaginary) is NaN, otherwise False.

Examples:

Example1:

Input:

Given Complex Number = 3+4j

Output:

The result after applying isnan() method to the given complex number (3+4j)  = 
False

Example2:

Input:

Given realpart = 5
Given imaginary part = 2

Output:

The result after applying isnan() method to the given complex number (5+2j) = 
False

Note: The above input format is for dynamic input.

cmath.isnan() 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.isnan() method that determines whether a given complex number’s value is nan (Not a Number) or not.
  • Store it in another variable.
  • Print the above result after applying isnan() method to 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.isnan() method
# that determines whether a given complex number's value is nan (Not a Number)or not.
# Store it in another variable.
rslt = cmath.isnan(complexnumb)
# Print the above result after applying isnan() method to the given complex number.
print("The result after applying isnan() method to the given complex number", complexnumb,
      " = ")
print(rslt)

Output:

The result after applying isnan() method to the given complex number (3+4j)  = 
False

Similarly, try for the other examples

import cmath
complexnumb = 10 + float('nan')
rslt = cmath.isnan(complexnumb)
print("The result after applying isnan() method to the given complex number = ")
print(rslt)

Output:

The result after applying isnan() method to the given complex number = 
True

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.isnan() method that determines whether a given complex number’s value is nan (Not a Number) or not.
  • Store it in another variable.
  • Print the above result after applying isnan() method to 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.isnan() method
# that determines whether a given complex number's value is nan (Not a Number)
# or not.
# Store it in another variable.
rslt = cmath.isnan(complexnumb)
# Print the above result after applying isnan() method to the given complex number.
print("The result after applying isnan() method to the given complex number", complexnumb,
      " = ")
print(rslt)

Output:

Enter real part and complex part of the complex number = 5 2
The result after applying isnan() method to the given complex number (5+2j) = 
False

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