the rootmeansquare average and most probable speeds of gas molecules are derived from the maxwellboltzmann distribution the maxwellboltzmann distribution is a probability distribution that describes the distribution of speeds of particles in an ideal gas the distribution is given by the following equation fv m2rt32 4v2 emv22rt where fv is the fraction of molecules with a speed v m is the molar mass of the gas in kgmol r is the gas constant t is the absolute temperature more information about the maxwellboltzmann distribution can be found here https en wikipedia orgwikimaxwelle28093boltzmanndistribution the average speed can be calculated by integrating the maxwellboltzmann distribution from 0 to infinity and dividing by the total number of molecules the result is vavg 8rtm the most probable speed is the speed at which the maxwellboltzmann distribution is at its maximum this can be found by differentiating the maxwellboltzmann distribution with respect to v and setting the result equal to zero the result is vmp 2rtm the rootmeansquare speed is another measure of the average speed of the molecules in a gas it is calculated by taking the square root of the average of the squares of the speeds of the molecules the result is vrms 3rtm here we have defined functions to calculate the average and most probable speeds of molecules in a gas given the temperature and molar mass of the gas import the constants r and pi from the scipy constants library takes the temperature in k and molar mass in kgmol of a gas and returns the average speed of a molecule in the gas in ms examples avgspeedofmolecule273 0 028 nitrogen at 273 k 454 3488755020387 avgspeedofmolecule300 0 032 oxygen at 300 k 445 52572733919885 avgspeedofmolecule273 0 028 invalid temperature traceback most recent call last exception absolute temperature cannot be less than 0 k avgspeedofmolecule273 0 invalid molar mass traceback most recent call last exception molar mass should be greater than 0 kgmol takes the temperature in k and molar mass in kgmol of a gas and returns the most probable speed of a molecule in the gas in ms examples mpsspeedofmolecule273 0 028 nitrogen at 273 k 402 65620701908966 mpsspeedofmolecule300 0 032 oxygen at 300 k 394 836895549922 mpsspeedofmolecule273 0 028 invalid temperature traceback most recent call last exception absolute temperature cannot be less than 0 k mpsspeedofmolecule273 0 invalid molar mass traceback most recent call last exception molar mass should be greater than 0 kgmol import the constants r and pi from the scipy constants library takes the temperature in k and molar mass in kg mol of a gas and returns the average speed of a molecule in the gas in m s examples avg_speed_of_molecule 273 0 028 nitrogen at 273 k 454 3488755020387 avg_speed_of_molecule 300 0 032 oxygen at 300 k 445 52572733919885 avg_speed_of_molecule 273 0 028 invalid temperature traceback most recent call last exception absolute temperature cannot be less than 0 k avg_speed_of_molecule 273 0 invalid molar mass traceback most recent call last exception molar mass should be greater than 0 kg mol takes the temperature in k and molar mass in kg mol of a gas and returns the most probable speed of a molecule in the gas in m s examples mps_speed_of_molecule 273 0 028 nitrogen at 273 k 402 65620701908966 mps_speed_of_molecule 300 0 032 oxygen at 300 k 394 836895549922 mps_speed_of_molecule 273 0 028 invalid temperature traceback most recent call last exception absolute temperature cannot be less than 0 k mps_speed_of_molecule 273 0 invalid molar mass traceback most recent call last exception molar mass should be greater than 0 kg mol
from scipy.constants import R, pi


def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float:
    

    if temperature < 0:
        raise Exception("Absolute temperature cannot be less than 0 K")
    if molar_mass <= 0:
        raise Exception("Molar mass should be greater than 0 kg/mol")
    return (8 * R * temperature / (pi * molar_mass)) ** 0.5


def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float:
    

    if temperature < 0:
        raise Exception("Absolute temperature cannot be less than 0 K")
    if molar_mass <= 0:
        raise Exception("Molar mass should be greater than 0 kg/mol")
    return (2 * R * temperature / molar_mass) ** 0.5


if __name__ == "__main__":
    import doctest

    doctest.testmod()