Skip to main content
Chemistry LibreTexts

8.9: Modulus Method

  • Page ID
    470275
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    This is the older method and you will often find it in packages, especially code developed for older sensors, and so it is important that you are familiar with it, but we will not require you to write code using this method. You should still skim through this material, and be prepared to reference it when needed.  Before we get into formatting syntax we need to identify the type, as the way a computer stores data depends on its type.

    Types

    Remember the type() function lets you know the type of object you are dealing with, and there are four common data types we deal with in formatting

    • %s - represents strings
    • %i - represents integers
    • %d - represents decimal integers
    • %f - represents floats

    Generic Syntax

    clipboard_ed6a11efc5a8c0cf026b17707e177ec57.pngFigure \(\PageIndex{1}\):Generic syntax for formatting non string numbers. (Belford cc 0.0)

    The following code depicts this for the float 1.00784

    print("The molar mass of hydrogen is: %1.2f"%(1.00784))
    print("The molar mass of hydrogen is: %10.2f"%(1.00784))
    print("The molar mass of hydrogen is: %1.0f"%(1.00784))
    print("The molar mass of hydrogen is: %1.5f"%(1.00784))
    Hello world!

    Now go into the fourth line of the above code and change the %1.5f to a %1.5i and look at how the output is changed. Well, you are formatting a floating number as an integer type, which makes no sense. 

    Now lets format a couple of variables, one being a string and the other a float 

    #here we are assigning two variables in one line, and then printing them in a line
    entity, molar_mass="water", 18.01528
    print("The molar mass of %s is %.3f g/mol." %(entity,molar_mass))
    print("The molar mass of %s is %.5f g/mol." %(entity,molar_mass))
    print("The molar mass of %s is %.3d g/mol." %(entity,molar_mass))
    Hello world!
    Exercise \(\PageIndex{1}\)

    Can you explain the last line of code?  Can you fix it?

    Answer

    You are formating a float as if it was a decimal integer, and so the precision is not what you want.  you should change the %.3d to %.3f in the print statement and rerun the code


    This page titled 8.9: Modulus Method is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?