Skip to content

chemhelper.ideal_gas

chemhelper.ideal_gas

Ideal gas law, PV = nRT.

R module-attribute

R = 0.0821

Gas constant in L·atm/(mol·K).

ideal_gas

ideal_gas(solve_for, substance=None, P=None, V=None, n=None, T=None, m=None, decimals=2, Tunits='K', Vunits='L', Punits='atm')

Solve PV = nRT for one variable.

Give every variable except solve_for. Instead of n you may give the mass m together with substance.

Parameters:

Name Type Description Default
solve_for (P, V, n, T)

The variable to solve for.

'P'
substance str

Chemical formula of the gas; needed only with m.

None
P float

Pressure, in Punits.

None
V float

Volume, in Vunits.

None
n float

Amount of gas in moles.

None
T float

Temperature, in Tunits.

None
m float

Mass of gas in grams (used to compute n).

None
decimals int

Decimal places in the result.

2
Tunits (K, C)

Units of the given T.

'K'
Vunits (L, mL)

Units of the given V.

'L'
Punits (atm, torr, mmHg, kPa)

Units of the given P.

'atm'

Returns:

Type Description
float

The solved value, in atm (P), L (V), mol (n) or K (T).

Raises:

Type Description
ValueError

For an invalid solve_for or unit, or a missing input.

Examples:

>>> ideal_gas('V', n=1, T=273.15, P=1)
22.43
>>> ideal_gas('P', substance='O2', m=32, V=500, Vunits='mL', T=25, Tunits='C')
48.96
Source code in src/chemhelper/ideal_gas.py
def ideal_gas(solve_for, substance=None, P=None, V=None, n=None, T=None, m=None, decimals=2,
              Tunits="K", Vunits="L", Punits="atm"):
    """Solve PV = nRT for one variable.

    Give every variable except ``solve_for``. Instead of ``n`` you may give the
    mass ``m`` together with ``substance``.

    Parameters
    ----------
    solve_for : {'P', 'V', 'n', 'T'}
        The variable to solve for.
    substance : str, optional
        Chemical formula of the gas; needed only with ``m``.
    P : float, optional
        Pressure, in ``Punits``.
    V : float, optional
        Volume, in ``Vunits``.
    n : float, optional
        Amount of gas in moles.
    T : float, optional
        Temperature, in ``Tunits``.
    m : float, optional
        Mass of gas in grams (used to compute ``n``).
    decimals : int, default 2
        Decimal places in the result.
    Tunits : {'K', 'C'}, default 'K'
        Units of the given ``T``.
    Vunits : {'L', 'mL'}, default 'L'
        Units of the given ``V``.
    Punits : {'atm', 'torr', 'mmHg', 'kPa'}, default 'atm'
        Units of the given ``P``.

    Returns
    -------
    float
        The solved value, in atm (P), L (V), mol (n) or K (T).

    Raises
    ------
    ValueError
        For an invalid ``solve_for`` or unit, or a missing input.

    Examples
    --------
    >>> ideal_gas('V', n=1, T=273.15, P=1)
    22.43
    >>> ideal_gas('P', substance='O2', m=32, V=500, Vunits='mL', T=25, Tunits='C')
    48.96
    """
    P, V, n, T = _prepare(solve_for, substance, P, V, n, T, m, Tunits, Vunits, Punits, show=False)
    return round(_solve(solve_for, P, V, n, T), decimals)

ideal_gas_steps

ideal_gas_steps(solve_for, substance=None, P=None, V=None, n=None, T=None, m=None, decimals=2, Tunits='K', Vunits='L', Punits='atm')

Print a step-by-step ideal gas law solution.

Takes the same arguments as :func:ideal_gas.

Examples:

>>> ideal_gas_steps('V', n=1, T=273.15, P=1)
PV = nRT
V = nRT/P
V = 1 moles x 0.0821 L*atm/(mol*K) x 273.15 K / 1 atm = 22.43 L
Source code in src/chemhelper/ideal_gas.py
def ideal_gas_steps(solve_for, substance=None, P=None, V=None, n=None, T=None, m=None, decimals=2,
                    Tunits="K", Vunits="L", Punits="atm"):
    """Print a step-by-step ideal gas law solution.

    Takes the same arguments as :func:`ideal_gas`.

    Examples
    --------
    >>> ideal_gas_steps('V', n=1, T=273.15, P=1)
    PV = nRT
    V = nRT/P
    V = 1 moles x 0.0821 L*atm/(mol*K) x 273.15 K / 1 atm = 22.43 L
    """
    P, V, n, T = _prepare(solve_for, substance, P, V, n, T, m, Tunits, Vunits, Punits, show=True)
    result = round(_solve(solve_for, P, V, n, T), decimals)
    g = lambda x: "{:.6g}".format(x)  # noqa: E731
    print("PV = nRT")
    if solve_for == "P":
        print("P = nRT/V")
        print("P = {} moles x 0.0821 L*atm/(mol*K) x {} K / {} L = {} atm".format(g(n), g(T), g(V), result))
    elif solve_for == "V":
        print("V = nRT/P")
        print("V = {} moles x 0.0821 L*atm/(mol*K) x {} K / {} atm = {} L".format(g(n), g(T), g(P), result))
    elif solve_for == "n":
        print("n = PV/RT")
        print("n = ({} atm x {} L) / (0.0821 L*atm/(mol*K) x {} K) = {} moles".format(g(P), g(V), g(T), result))
    else:
        print("T = PV/nR")
        print("T = ({} atm x {} L) / ({} moles x 0.0821 L*atm/(mol*K)) = {} K".format(g(P), g(V), g(n), result))