Skip to main content
Engineering LibreTexts

21.5 Combining branching with loops

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

    And now for the really important application of branching in data science programs: combining it with loops.

    Just as we can have an if statement (or if/else, or if/elif/else) inside an if body, so we can have an if statement (and friends) inside a loop body. This is where we’re going to get a lot of mileage.

    Let’s return to Springfield. Our simpsons DataFrame from p. 174 looked like this:

    Screen Shot 2022-09-12 at 6.25.17 PM.png

    Now an ordinary loop could print (say) the name and favorite things of all the characters:

    Code \(\PageIndex{1}\) (Python):

    for row in simpsons.itertuples():

    print("{} likes {}.".format(row.Index, row.fave))

    Output:

    Screen Shot 2022-09-12 at 6.49.01 PM.png

    But combining this with branching techniques gives us more power. We could, for example, print only the females:

    Code \(\PageIndex{2}\) (Python):

    for row in simpsons.itertuples():

    if row.gender == "F":

    print("{} likes {}.".format(row.Index, row.fave))

    Output:

    Screen Shot 2022-09-12 at 6.49.39 PM.png

    or give different messages for different age ranges:

    Code \(\PageIndex{3}\) (Python):

    for row in simpsons.itertuples():

    if row.age >= 18:

    print("{} earns ${} outside the home.".format(row.Index, row.salary))

    else:

    print("Aw, {}'s just a kid.".format(row.Index))

    Output:

    Screen Shot 2022-09-12 at 6.50.18 PM.png

    or combine these things in innumerable ways:

    Code \(\PageIndex{4}\) (Python):

    for row in simpsons.itertuples():

    if row.species == "human":

    if row.IQ > 115:

    print("We'd like to nominate {} for a Nobel prize.".format(row.Index))

    elif row.IQ >= 90:

    print("You can trust {} with a {}, or even a knife.".format(row.Index, row.fave))

    else:

    print("Hmm. No comment.")

    if row.salary > 0:

    print("The {}-year-old {} is gainfully employed.".format(row.age, row.Index))

    else:

    print("Hey...{} is some kind of animal!".format(row.Index))

    Output:

    Screen Shot 2022-09-12 at 6.50.53 PM.png

    You get the idea. Using a loop, we can successively consider each element of an array/Series or the rows of a DataFrame. Using if and friends, we can treat each one differently depending on its characteristics. The possibilities are endless!


    This page titled 21.5 Combining branching with loops is shared under a not declared license and was authored, remixed, and/or curated by Stephen Davies (allthemath.org) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?