Skip to main content
Chemistry LibreTexts

4: if-elif-else Statement

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

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    An if-elif-else statement has two parts.  The if-elif and the else. For each if or elif condition a block of code is executed if it is true, and the next condition is then checked if it is false. If none of the condition are true, the else block of code is executed.  So if and elif execute when a condition is true, while else executes if it is false.  Note the conditions are tested sequentially, and sometimes this can effect the logic.

    clipboard_e33b9d5e178a1f6e39f8f48d6467f7d4b.pngFigure \(\PageIndex{1}\): Copy and Paste Caption here. (Copyright; author via source)

    The following code uses the List content type (section 2.7) and the membership operator (section 2.18). These lists are lists of strings (section 2.6) and you are seeing which list the element is in.

    Caution

    The following code only works if you capitalize the first letter of each element, which is a formatting issue (Boron works, boron does not).  Note that element is a string, and we are using the membership to see if it is in a list of strings.  Can you find a string method that can fix this problem so that boron will work?  There are actually several methods to do this. So, change the code and run it.  Here is a hint, you can reassign a variable name to the same variable name with a method operating on it (var1=var1.method), where var1 is the variable name and method is the method you are using.

    element = input("Enter the element you want to know the family of: ")
    
    if element == "Hydrogen":
        print("Hydrogen belongs to the nonmetal family.")
    elif element in ["Lithium", "Sodium", "Potassium", "Rubidium", "Cesium", "Francium"]:
        print(f"{element} belongs to the alkali metal family.")
    elif element in ["Beryllium", "Magnesium", "Calcium", "Strontium", "Barium", "Radium"]:
        print(f"{element} belongs to the alkaline earth metal family.")
    elif element in ["Boron", "Aluminum", "Gallium", "Indium", "Thallium"]:
        print(f"{element} belongs to the metalloid family.")
    elif element in ["Carbon", "Silicon", "Germanium", "Tin", "Lead"]:
        print(f"{element} belongs to the nonmetal family.")
    elif element in ["Nitrogen", "Phosphorus", "Arsenic", "Antimony", "Bismuth"]:
        print(f"{element} belongs to the pnictogen family.")
    elif element in ["Oxygen", "Sulfur", "Selenium", "Tellurium", "Polonium"]:
        print(f"{element} belongs to the chalcogen family.")
    elif element in ["Fluorine", "Chlorine", "Bromine", "Iodine", "Astatine"]:
        print(f"{element} belongs to the halogen family.")
    elif element in ["Helium", "Neon", "Argon", "Krypton", "Xenon", "Radon"]:
        print(f"{element} belongs to the noble gas family.")
    elif elment in ["Cerium", "Praseodymium", "Neodymium", "Promethium", "Samarium", 
                   "Europium", "Gadolinium", "Terbium", "Dysprosium", "Holmium", 
                   "Erbium", "Thulium", "Ytterbium", "Lutetium"]:
        print(f'{element} is a lanthanide')
    elif element in ["Thorium", "Protactinium", "Uranium", "Neptunium", "Plutonium", 
                 "Americium", "Curium", "Berkelium", "Californium", "Einsteinium", 
                 "Fermium", "Mendelevium", "Nobelium", "Lawrencium", "Rutherfordium", 
                 "Dubnium", "Seaborgium", "Bohrium", "Hassium", "Meitnerium", "Darmstadtium", 
                 "Roentgenium", "Copernicium", "Nihonium", "Flerovium", "Moscovium", "Livermorium", "Tennessine", "Oganesson"]:
        print(f'{element} is an actinide')
    else:
        print(f'element is a transition metal')
    Hello world!

     

     


    This page titled 4: if-elif-else Statement is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?