code: def median(X, evaluate=True, **kwargs):
      r"""
      Calculuates the median of the probability distribution.
  
      Explanation
      ===========
  
      Mathematically, median of Probability distribution is defined as all those
      values of `m` for which the following condition is satisfied
  
      .. math::
          P(X\leq m) \geq  \frac{1}{2} \text{ and} \text{ } P(X\geq m)\geq \frac{1}{2}
  
      Parameters
      ==========
  
      X: The random expression whose median is to be calculated.
  
      Returns
      =======
  
      The FiniteSet or an Interval which contains the median of the
      random expression.
  
      Examples
      ========
  
      >>> from sympy.stats import Normal, Die, median
      >>> N = Normal('N', 3, 1)
      >>> median(N)
      {3}
      >>> D = Die('D')
      >>> median(D)
      {3, 4}
  
      References
      ==========
  
      .. [1] https://en.wikipedia.org/wiki/Median#Probability_distributions
  
      """
      if not is_random(X):
          return X
  
      from sympy.stats.crv import ContinuousPSpace
      from sympy.stats.drv import DiscretePSpace
      from sympy.stats.frv import FinitePSpace
  
      if isinstance(pspace(X), FinitePSpace):
          cdf = pspace(X).compute_cdf(X)
          result = []
          for key, value in cdf.items():
              if value >= Rational(1, 2) and (1 - value) + pspace(
                  X
              ).probability(Eq(X, key)) >= Rational(1, 2):
                  result.append(key)
          return FiniteSet(*result)
      if isinstance(pspace(X), (ContinuousPSpace, DiscretePSpace)):
          cdf = pspace(X).compute_cdf(X)
          x = Dummy("x")
          result = solveset(
              piecewise_fold(cdf(x) - Rational(1, 2)), x, pspace(X).set
          )
          return result
      raise NotImplementedError(
          "The median of %s is not implemented." % str(pspace(X))
      )
  
imports: from sympy.sets import FiniteSet
  from sympy.core.numbers import Rational
  from sympy.core.relational import Eq
  from sympy.core.symbol import Dummy
  from sympy.functions.combinatorial.factorials import FallingFactorial
  from sympy.functions.elementary.exponential import exp, log
  from sympy.functions.elementary.miscellaneous import sqrt
  from sympy.functions.elementary.piecewise import piecewise_fold
  from sympy.integrals.integrals import Integral
  from sympy.solvers.solveset import solveset
  from .rv import probability, expectation, density, where, given, pspace, cdf, PSpace, characteristic_function, sample, sample_iter, random_symbols, independent, dependent, sampling_density, moment_generating_function, quantile, is_random, sample_stochastic_process
label_desc: Use image node_id 12 for calling a global function with example usage: median(X, evaluate) and returns: X, FiniteSet, result