Skip to main content
Chemistry LibreTexts

5: Compound if/elif Statements

  • Page ID
    432758
  • \( \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}}} \)

    Compound if/elif statements combine two if statements with a logical operator (and, or, not).

    From general chemistry we can determine at which temperature a reaction is spontaneous or not spontaneous based on the enthalpy and entropy values (UALR Gen Chem sec. 18.5.3). That is, for a spontaneous reaction \( \Delta \) G < 0, and \(\Delta\) G = \(\Delta\)H - T\(\Delta\)S.  So a negative reaction enthalpy and a positive reaction entropy contribute to spontaneity, as outlined in the following table.  

     

    Table\(\PageIndex{1}\), Temperature dependence of spontaneity as a function of the enthalpy and entropy changes of a reaction.
    Type \(\Delta H\) \(\Delta S\) \(\Delta G\)
    1. [+] [-] Always [+], never spontaneous
    2. [-] [-] [-] at low T, enthalpy driven process only spontaneous at low T
    3. [+] [+] [-] at high T, entropy driven process only spontaneous at high T
    4. [-] [+] Always [-], spontaneous at all T,
    both enthalpy and entropy driven

     

    and conjunction:

    The following code uses a compound if/elif statement to provide the results of the first two condigtions of the above table, can you complete this code to cover the third and fourth conditions

    #Temperature and reaction spontaneity
    enthalpy=float(input("Enter a reaction enthalpy and include the sign: "))
    entropy=float(input("Enter a reaction entropy and include the sign: "))
    
    if enthalpy > 0 and entropy < 0:
        print(f"A reaction with {enthalpy} and {entropy} is never spontaneous")
    elif enthalpy > 0 and entropy > 0:
        print(f"A reaction with {enthalpy} and {entropy} is entropy driven and spontaneous at high T")
    else:
        print("You need to complete the code to handle this condition")
    Hello world!

     

    or conjunction:

    The following is an example of using an "or" operator in an "if" statement to determine if an element is an alkaline earth metal:

    #is an element an alkaline earth metal?
    
    atom=input("Enter name of element: ")
    #atom=atom.capitalize()
    if atom == "Beryllium" or atom == "Magnesium" or atom == "Calcium" or \
       atom == "Barium" or atom == "Strontium" or atom == "Radium":
        print(f"{atom} is an alkaline earth metal")
    else:
        print(f"{atom} is not an alkaline earth metal")
            hello world
          
    Exercise \(\PageIndex{1}\)

    In the above code type barium in lower case.  Then uncomment the second line and try barium in lower case again.  Explain the line of code you uncommented

    Answer

    If you go to the section on string class, you will see the str.capitalize() method. You could also have used the str.title() method.

     

     

    Exercise \(\PageIndex{2}\)

    The above code could get very extensive if you were going to extend it with elif statements to identify what family an element belongs to.  Can you think of another approach that could be used

    Answer

    One additional approach would be to make a list for each family , and then use the membership operator to see if the element is in the list.

     

     


    This page titled 5: Compound if/elif Statements is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?