Ways to compute the squares of the integers between 1 and 5

On this page click on the code samples to execute them.

  1. The for loop combined with the range function is the most direct approach:

    Animate squares-1

    for i in range(1, 6):
        print(i**2)
  2. It is also possible to combine a while loop with the incrementation of a variable at each iteration of the loop:

    Animate squares-2

    i = 1
    while i <= 5:
        print(i**2)
        i = i + 1
  3. The order of the values can be reversed by using a decrementation:

    Animate squares-3

    i = 5
    while i > 0:
        print(i**2)
        i = i - 1
  4. Summing the odd integers with a while loop reaches the same result without using the ** operator:

    Single-step squares-4

    i = 1
    n = 1
    while i < 10:
        print(n)
        i = i + 2
        n = n + i
  5. A recursive function is another approach which has a certain similarity with the while loop:

    Single-step console

    >>> def f(i, n):
    ...     if i < 10:
    ...         print(n)
    ...         f(i + 2, n + i + 2)
    ... 
    >>> f(1, 1)
        1
        4
  6. Using the functional programming approach a solution in one line is possible:

    Execute squares-6

    list(map(lambda i: print((i+1)**2), range(5)))
  7. The previous approaches have the merit of a certain generality which the following solution lacks:

    Bundle squares-7

    print(1)
    print(4)
    print(9)
    print(16)
    print(25)
  8. The results can be put in a CSV file and then displayed in a spreadsheet and a bar chart:

    Execute squares-8

    # Create a CSV file containing the squares.

    with open('squares.csv', 'w') as f:
        f.write('N,N**2\n')
        for i in range(1, 6):
            f.write(str(i) + ',' + str(i**2) + '\n')

    # Display it as a spreadsheet.

    show_file('squares.csv')

    # Display it as a bar chart.

    chart(read_file('squares.csv'), mark='bar', x_type='ordinal')
  9. The results could also be drawn using the turtle metaphor (here executing as a standalone web app):

    squares-9

    # This program uses the turtle to draw the
    # numbers using 7 segments.

    from turtle import hop, lt, rt, fd, pensize

    dist = 20  # length of each segment

    segments = {  # the 7 segments of each symbol
        ' ':0,
        '0':63, '1':12,  '2':118, '3':94,  '4':77,
        '5':91, '6':123, '7':14,  '8':127, '9':95
    }

    def print_char(c):  # draw a symbol
        lt(90)
        hop(); fd(dist)
        bits = segments[c]  # each bit = 1 segment
        for i in range(7):
            if (bits & 1) == 0: hop()
            fd(dist)
            bits >>= 1
            if i != 2: rt(90)
        hop(); fd(dist)
        lt(90)
        hop(); fd(dist/2)

    def print(n):  # print a number
        for c in str(n):
            print_char(c)
        print_char(' ')

    hop(); fd(-175); pensize(5)

    for i in range(1, 6):  # compute the squares
        print(i**2)

HTML code for the above page

(see https://github.com/codeboot-org/presentations/tree/main/html/indirect/en)
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Computing the squares of the integers between 1 and 5</title>

  <!-- The following CSS is completely optional but gives a nicer look -->

  <style>

div[data-codeboot-type] {
    background-color: #f6f6f6;
    border-radius: 6px;
    position: relative;
}

div[data-codeboot-type] > div {
    padding: 2px;
}

div[data-codeboot-type] > div > code {
    background: transparent;
    border: 0;
}

div[data-codeboot-type] span[data-cb-link] {
    position: absolute;
    right: 0;
}

div[data-codeboot-type]:hover span[data-cb-link] > a > span[data-cb-image] > svg {
    background: #e0e0e0;
    border-radius: 6px;
}

div[data-codeboot-type]:hover span[data-cb-link] > a:hover > span[data-cb-image] > svg {
    background: #c0c0c0;
}

div[data-codeboot-type]:hover span[data-cb-link] > a > span[data-cb-image] {
    display: inline !important;
}

div[data-codeboot-type] span[data-cb-link] > a > span[data-cb-text] {
    display: none;
}

span[data-cb-image] svg {
    padding: 0 3px 4px 3px;
    width: 1.5em;
}

  </style>

</head>

<body>


<h3>Ways to compute the squares of the integers between 1 and 5</h3>


<p>On this page <b>click on the code samples to execute them.</b></p>

<ol>


<li>
<p>The <samp>for</samp> loop combined with the <samp>range</samp> function
is the most direct approach:</p>

<!-- Automatic animation created using a right-click of the single-step
button and selecting "Visit animated execution link or copy to clipboard:
With code as HTML" and then pasting the HTML here -->
<div data-codeboot-type="file" data-codeboot-file-name="squares-1.py"><span data-cb-link><a href="https://app.codeboot.org/4.0.0/?init=.fc3F1YXJlcy0xLnB5~XQAAgAAlAAAAAAAAAAAzG8qmJ5VKcRIqc4D53n0GAajKdINri6XFtz6xpwkpsUNe9RxaVfxZX_7nGwA=.~lang=py-novice.a" style="font-size:18px;"><span data-cb-text>Animate squares-1<br><br></span><span data-cb-image style="display:none"><svg class="cb-svg-clone" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><g><path d="M 55 60 L 55 206 L 201 206 L 201 60 Z M 65 85 L 191 85 L 191 196 L 65 196 Z"/></g></svg></span></a></span><div style="color:rgb(1, 1, 1);font-size:18px;font-family:&quot;Consolas&quot;,&quot;Monaco&quot;,&quot;Courier New&quot;,monospace;white-space:pre;font-weight:400;"><code><span style="color:rgb(119, 0, 136);">for</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(119, 0, 136);">in</span><span>&nbsp;</span><span style="color:rgb(51, 0, 170);">range</span><span>(</span><span style="color:rgb(17, 102, 68);">1</span><span>,</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">6</span><span>)</span><span>:</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(51, 0, 170);">print</span><span>(</span><span style="color:rgb(1, 1, 1);">i</span><span style="color:rgb(1, 1, 1);">*</span><span style="color:rgb(1, 1, 1);">*</span><span style="color:rgb(17, 102, 68);">2</span><span>)</span></code></div></div>


<li>
<p>It is also possible to combine a <samp>while</samp> loop with the
incrementation of a variable at each iteration of the loop:</p>

<!-- Automatic animation created using a right-click of the single-step
button and selecting "Animate Fast" then another right-click of the same
button and selecting "Visit animated execution link or copy to clipboard:
With code as HTML" and then pasting the HTML here -->
<div data-codeboot-type="file" data-codeboot-file-name="squares-2.py"><span data-cb-link><a href="https://app.codeboot.org/4.0.0/?init=.fc3F1YXJlcy0yLnB5~XQAAgAAxAAAAAAAAAAA0iAOiEVbb_rX75jYHdW7yCfNF8at8ZfNjOkblaajR-xluLk699mU1qM1Qsc5f__mG8AA=.~lang=py-novice.~animationSpeed=fast.a" style="font-size:18px;"><span data-cb-text>Animate squares-2<br><br></span><span data-cb-image style="display:none"><svg class="cb-svg-clone" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><g><path d="M 55 60 L 55 206 L 201 206 L 201 60 Z M 65 85 L 191 85 L 191 196 L 65 196 Z"/></g></svg></span></a></span><div style="color:rgb(1, 1, 1);font-size:18px;font-family:&quot;Consolas&quot;,&quot;Monaco&quot;,&quot;Courier New&quot;,monospace;white-space:pre;font-weight:400;"><code><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">=</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">1</span><br><span style="color:rgb(119, 0, 136);">while</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">&#60;=</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">5</span><span>:</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(51, 0, 170);">print</span><span>(</span><span style="color:rgb(1, 1, 1);">i</span><span style="color:rgb(1, 1, 1);">*</span><span style="color:rgb(1, 1, 1);">*</span><span style="color:rgb(17, 102, 68);">2</span><span>)</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">=</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">+</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">1</span></code></div></div>


<li>
<p>The order of the values can be reversed by using a decrementation:</p>

<!-- Automatic animation created by selecting "Large font" in the Settings
menu and then using a right-click of the single-step
button and selecting "Visit animated execution link or copy to clipboard:
With code as HTML" and then pasting the HTML here -->
<div data-codeboot-type="file" data-codeboot-file-name="squares-3.py"><span data-cb-link><a href="https://app.codeboot.org/4.0.0/?init=.fc3F1YXJlcy0zLnB5~XQAAgAAwAAAAAAAAAAA0iAOiEX9yPrX75jYHdW7zTsO01Z0TxR_9TuYR3xt0paTkJt4ijXeVTm_he9__18gAAA==.~lang=py-novice.~largeFont=true.a" style="font-size:18px;"><span data-cb-text>Animate squares-3<br><br></span><span data-cb-image style="display:none"><svg class="cb-svg-clone" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><g><path d="M 55 60 L 55 206 L 201 206 L 201 60 Z M 65 85 L 191 85 L 191 196 L 65 196 Z"/></g></svg></span></a></span><div style="color:rgb(1, 1, 1);font-size:18px;font-family:&quot;Consolas&quot;,&quot;Monaco&quot;,&quot;Courier New&quot;,monospace;white-space:pre;font-weight:400;"><code><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">=</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">5</span><br><span style="color:rgb(119, 0, 136);">while</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">&#62;</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">0</span><span>:</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(51, 0, 170);">print</span><span>(</span><span style="color:rgb(1, 1, 1);">i</span><span style="color:rgb(1, 1, 1);">*</span><span style="color:rgb(1, 1, 1);">*</span><span style="color:rgb(17, 102, 68);">2</span><span>)</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">=</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">-</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">1</span></code></div></div>


<li>
<p>Summing the odd integers with a <samp>while</samp> loop reaches the
same result without using the <samp>**</samp> operator:

<!-- Automatic execution with a pause after 45 steps created by single-stepping
the program to step 45 then using a right-click of the single-step
button and selecting "Visit single stepping execution link or copy to clipboard:
With code as HTML" and then pasting the HTML here -->
<div data-codeboot-type="file" data-codeboot-file-name="squares-4.py"><span data-cb-link><a href="https://app.codeboot.org/4.0.0/?init=.fc3F1YXJlcy00LnB5~XQAAgABCAAAAAAAAAAA0iAOiEVbb45eV5_jlSvXLFu9uqJ033SmAPt9zCycxT8IPUhA184nO9XzCZolqJ_SIA2Y8Qv4sz0A=.~lang=py-novice.e45" style="font-size:18px;"><span data-cb-text>Single-step squares-4<br><br></span><span data-cb-image style="display:none"><svg class="cb-svg-clone" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><g><path d="M 55 60 L 55 206 L 201 206 L 201 60 Z M 65 85 L 191 85 L 191 196 L 65 196 Z"/></g></svg></span></a></span><div style="color:rgb(1, 1, 1);font-size:18px;font-family:&quot;Consolas&quot;,&quot;Monaco&quot;,&quot;Courier New&quot;,monospace;white-space:pre;font-weight:400;"><code><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">=</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">1</span><br><span style="color:rgb(1, 1, 1);">n</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">=</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">1</span><br><span style="color:rgb(119, 0, 136);">while</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">&#60;</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">10</span><span>:</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(51, 0, 170);">print</span><span>(</span><span style="color:rgb(1, 1, 1);">n</span><span>)</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">=</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">+</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">2</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">n</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">=</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">n</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">+</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span></code></div></div>


<li>
<p>A recursive function is another approach which has
a certain similarity with the <samp>while</samp> loop: </p>

<!-- Automatic execution with a pause after 33 steps created by entering
the code at the console and single-stepping the call f(1, 1) until step 33
is reached then using a right-click of the single-step
button and selecting "Visit single stepping execution link or copy to clipboard:
With code as HTML" and then pasting the HTML here -->
<div data-codeboot-type="input" data-codeboot-input="f(1, 1)"><span data-cb-link><a href="https://app.codeboot.org/4.0.0/?init=.~lang=py-novice.iZGVmIGYoaSwgbik6CiAgICBpZiBpIDwgMTA6CiAgICAgICAgcHJpbnQobikKICAgICAgICBmKGkgKyAyLCBuICsgaSArIDIpCgpmKDEsIDEp.e33" style="font-size:18px;"><span data-cb-text>Single-step console<br><br></span><span data-cb-image style="display:none"><svg class="cb-svg-clone" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><g><path d="M 55 60 L 55 206 L 201 206 L 201 60 Z M 65 85 L 191 85 L 191 196 L 65 196 Z"/></g></svg></span></a></span><div style="color:rgb(1, 1, 1);font-size:18px;font-family:&quot;Consolas&quot;,&quot;Monaco&quot;,&quot;Courier New&quot;,monospace;white-space:pre;font-weight:400;"><code>&#62;&#62;&#62;&nbsp;<span style="color:rgb(119, 0, 136);">def</span><span>&nbsp;</span><span style="color:rgb(0, 0, 255);">f</span><span>(</span><span style="color:rgb(1, 1, 1);">i</span><span>,</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">n</span><span>)</span><span>:</span><br>...&nbsp;<span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(119, 0, 136);">if</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">&#60;</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">10</span><span>:</span><br>...&nbsp;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(51, 0, 170);">print</span><span>(</span><span style="color:rgb(1, 1, 1);">n</span><span>)</span><br>...&nbsp;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">f</span><span>(</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">+</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">2</span><span>,</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">n</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">+</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">+</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">2</span><span>)</span><br>...&nbsp;<br>&#62;&#62;&#62;&nbsp;<span style="color:rgb(1, 1, 1);">f</span><span>(</span><span style="color:rgb(17, 102, 68);">1</span><span>,</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">1</span><span>)</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:rgb(17, 102, 68);">1</span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:rgb(17, 102, 68);">4</span></code></div></div>


<li>
<p>Using the functional programming approach a solution in one line
is possible:</p>

<!-- Automatic complete execution created by using a right-click of the Execute
button and selecting "Visit execution link or copy to clipboard:
With code as HTML" and then pasting the HTML here -->
<div data-codeboot-type="file" data-codeboot-file-name="squares-6.py"><span data-cb-link><a href="https://app.codeboot.org/4.0.0/?init=.fc3F1YXJlcy02LnB5~XQAAgAAuAAAAAAAAAAA2GkrOHUoZatHs46TlxgWtaoIMTUvCla5pgpsEn7cwx-L0S7TDpa-mUWRiq3e50GMJ__-m24AA.~lang=py-novice.e" style="font-size:18px;"><span data-cb-text>Execute squares-6<br><br></span><span data-cb-image style="display:none"><svg class="cb-svg-clone" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><g><path d="M 55 60 L 55 206 L 201 206 L 201 60 Z M 65 85 L 191 85 L 191 196 L 65 196 Z"/></g></svg></span></a></span><div style="color:rgb(1, 1, 1);font-size:18px;font-family:&quot;Consolas&quot;,&quot;Monaco&quot;,&quot;Courier New&quot;,monospace;white-space:pre;font-weight:400;"><code><span style="color:rgb(51, 0, 170);">list</span><span>(</span><span style="color:rgb(51, 0, 170);">map</span><span>(</span><span style="color:rgb(119, 0, 136);">lambda</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>:</span><span>&nbsp;</span><span style="color:rgb(51, 0, 170);">print</span><span>(</span><span>(</span><span style="color:rgb(1, 1, 1);">i</span><span style="color:rgb(1, 1, 1);">+</span><span style="color:rgb(17, 102, 68);">1</span><span>)</span><span style="color:rgb(1, 1, 1);">*</span><span style="color:rgb(1, 1, 1);">*</span><span style="color:rgb(17, 102, 68);">2</span><span>)</span><span>,</span><span>&nbsp;</span><span style="color:rgb(51, 0, 170);">range</span><span>(</span><span style="color:rgb(17, 102, 68);">5</span><span>)</span><span>)</span><span>)</span></code></div></div>


<li>
<p>The previous approaches have the merit of a certain generality which
the following solution lacks:</p>

<!-- Manual execution example created by using a right-click of the Stop
button and selecting "Visit stopped execution link or copy to clipboard:
With code as HTML" and then pasting the HTML here -->
<div data-codeboot-type="file" data-codeboot-file-name="squares-7.py"><span data-cb-link><a href="https://app.codeboot.org/4.0.0/?init=.fc3F1YXJlcy03LnB5~XQAAgAAuAAAAAAAAAAA4HIlWjAuZk4pKKgorO_ZOQIbe2Ru-UYltBWP_zZ0AAA==.~lang=py-novice" style="font-size:18px;"><span data-cb-text>Bundle squares-7<br><br></span><span data-cb-image style="display:none"><svg class="cb-svg-clone" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><g><path d="M 55 60 L 55 206 L 201 206 L 201 60 Z M 65 85 L 191 85 L 191 196 L 65 196 Z"/></g></svg></span></a></span><div style="color:rgb(1, 1, 1);font-size:18px;font-family:&quot;Consolas&quot;,&quot;Monaco&quot;,&quot;Courier New&quot;,monospace;white-space:pre;font-weight:400;"><code><span style="color:rgb(51, 0, 170);">print</span><span>(</span><span style="color:rgb(17, 102, 68);">1</span><span>)</span><br><span style="color:rgb(51, 0, 170);">print</span><span>(</span><span style="color:rgb(17, 102, 68);">4</span><span>)</span><br><span style="color:rgb(51, 0, 170);">print</span><span>(</span><span style="color:rgb(17, 102, 68);">9</span><span>)</span><br><span style="color:rgb(51, 0, 170);">print</span><span>(</span><span style="color:rgb(17, 102, 68);">16</span><span>)</span><br><span style="color:rgb(51, 0, 170);">print</span><span>(</span><span style="color:rgb(17, 102, 68);">25</span><span>)</span></code></div></div>


<li>
<p>The results can be put in a CSV file and then displayed
in a spreadsheet and a bar chart:</p>

<!-- Automatic complete execution created by using a right-click of the Execute
button and selecting "Visit execution link or copy to clipboard:
With code as HTML" and then pasting the HTML here -->
<div data-codeboot-type="file" data-codeboot-file-name="squares-8.py"><span data-cb-link><a href="https://app.codeboot.org/4.0.0/?init=.fc3F1YXJlcy04LnB5~XQAAgABKAQAAAAAAAAARiATpCXoP0CMmlBBXJdjbXuO9Mlkf5U178efAFOqDvznIgEZfuZgNYD1qH3db1BPknJrCa8kRZyVaTKSLheZlRFg-_f4a62ukkNaYU7Hi2tvxTjPUxOAfnlW75Z9Vu54g0R6IgWC4L2naNjVWGBWXEHv_XEgcatBnLwSYyNuNsEly_QeCQYOF55_cqBecOIf0grN4QW_bpKQ2kVPnb4NNbbNZQTrp67jes5fKFkNq3Cl1moIEsRtdMlTKX0hGISnc_TFVDlKbVtJNrI8GOGug0lE9hTTEf8j6Yy4-H_yRu6Y=.~lang=py-novice.e" style="font-size:18px;"><span data-cb-text>Execute squares-8<br><br></span><span data-cb-image style="display:none"><svg class="cb-svg-clone" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><g><path d="M 55 60 L 55 206 L 201 206 L 201 60 Z M 65 85 L 191 85 L 191 196 L 65 196 Z"/></g></svg></span></a></span><div style="color:rgb(1, 1, 1);font-size:18px;font-family:&quot;Consolas&quot;,&quot;Monaco&quot;,&quot;Courier New&quot;,monospace;white-space:pre;font-weight:400;"><code><span style="color:rgb(170, 85, 0);">#&nbsp;Create&nbsp;a&nbsp;CSV&nbsp;file&nbsp;containing&nbsp;the&nbsp;squares.</span><br><br><span style="color:rgb(119, 0, 136);">with</span><span>&nbsp;</span><span style="color:rgb(51, 0, 170);">open</span><span>(</span><span style="color:rgb(170, 17, 17);">&#39;squares.csv&#39;</span><span>,</span><span>&nbsp;</span><span style="color:rgb(170, 17, 17);">&#39;w&#39;</span><span>)</span><span>&nbsp;</span><span style="color:rgb(119, 0, 136);">as</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">f</span><span>:</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">f</span><span>.</span><span style="color:rgb(1, 1, 1);">write</span><span>(</span><span style="color:rgb(170, 17, 17);">&#39;N,N**2\n&#39;</span><span>)</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(119, 0, 136);">for</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(119, 0, 136);">in</span><span>&nbsp;</span><span style="color:rgb(51, 0, 170);">range</span><span>(</span><span style="color:rgb(17, 102, 68);">1</span><span>,</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">6</span><span>)</span><span>:</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">f</span><span>.</span><span style="color:rgb(1, 1, 1);">write</span><span>(</span><span style="color:rgb(51, 0, 170);">str</span><span>(</span><span style="color:rgb(1, 1, 1);">i</span><span>)</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">+</span><span>&nbsp;</span><span style="color:rgb(170, 17, 17);">&#39;,&#39;</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">+</span><span>&nbsp;</span><span style="color:rgb(51, 0, 170);">str</span><span>(</span><span style="color:rgb(1, 1, 1);">i</span><span style="color:rgb(1, 1, 1);">*</span><span style="color:rgb(1, 1, 1);">*</span><span style="color:rgb(17, 102, 68);">2</span><span>)</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">+</span><span>&nbsp;</span><span style="color:rgb(170, 17, 17);">&#39;\n&#39;</span><span>)</span><br><br><span style="color:rgb(170, 85, 0);">#&nbsp;Display&nbsp;it&nbsp;as&nbsp;a&nbsp;spreadsheet.</span><br><br><span style="color:rgb(1, 1, 1);">show_file</span><span>(</span><span style="color:rgb(170, 17, 17);">&#39;squares.csv&#39;</span><span>)</span><br><br><span style="color:rgb(170, 85, 0);">#&nbsp;Display&nbsp;it&nbsp;as&nbsp;a&nbsp;bar&nbsp;chart.</span><br><br><span style="color:rgb(1, 1, 1);">chart</span><span>(</span><span style="color:rgb(1, 1, 1);">read_file</span><span>(</span><span style="color:rgb(170, 17, 17);">&#39;squares.csv&#39;</span><span>)</span><span>,</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">mark</span><span style="color:rgb(1, 1, 1);">=</span><span style="color:rgb(170, 17, 17);">&#39;bar&#39;</span><span>,</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">x_type</span><span style="color:rgb(1, 1, 1);">=</span><span style="color:rgb(170, 17, 17);">&#39;ordinal&#39;</span><span>)</span></code></div></div>


<li>
<p>The results could also be drawn using the turtle metaphor (here
executing as a standalone web app):</p>

<!-- A web app is created by using a right-click of the Execute
button and selecting "Visit web app link or copy to clipboard:
With code as HTML" and then pasting the HTML here -->
<div data-codeboot-type="file" data-codeboot-file-name="squares-9.py"><span data-cb-link><a href="https://app.codeboot.org/4.0.0/?init=.fc3F1YXJlcy05LnB5~XQAAgAALAwAAAAAAAAARiAcH4cNcktp9fgj0caxBKC3zgfC39x6OxDF0Nojr1mk7l64DmzVe9W5B7gHfgCukq-gKHo4h8DY_wX1Uo2Zw2qv1DwyeJSPk1bZGix2MpZtrFMQxYScEGUqbkUs75agfCEGSrsd-5TS5MNlU8Bn0zapgEXwz1X6MnZ-uLwKUr3IZH0dHY1YJX0Yld0Doh5mJGipyJhApOsex6FBNa-s3LVsz0kbj5xNXhzr10EqpDBFP78s80JarFp4f2zuZ9IFhC3DsHTeIKLp9hmb_FQ03rdSHs0lNrPHe9LMf1j8F8oL5gKOCXosSbJTAhxufcrTysd8lzmZbx-zGehovuwv6r4pZqPbfLg2KaQOUpAz4qoLFrERlCxAE2B0b7zKw_itoK5KkYgjOT0jBDvaYNF8vZ6xRFNq6pAc51EwHp7fgYNGYXkvdVOGjEajjwJoY5fxQd2loBM4cSW3GKL3pDjLyI0GCUoM84M0QPDvY0bHXgqFKECvM-_rfIzJcZPo4Z-GqqBbBp8-8_cEjHLFGNqQfJHkV9BvTR33t_Gaa0w==.~lang=py-novice.~hidden=true.e" style="font-size:18px;"><span data-cb-text>squares-9<br><br></span><span data-cb-image style="display:none"><svg class="cb-svg-clone" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><g><path d="M 55 60 L 55 206 L 201 206 L 201 60 Z M 65 85 L 191 85 L 191 196 L 65 196 Z"/></g></svg></span></a></span><div style="color:rgb(1, 1, 1);font-size:18px;font-family:&quot;Consolas&quot;,&quot;Monaco&quot;,&quot;Courier New&quot;,monospace;white-space:pre;font-weight:400;"><code><span style="color:rgb(170, 85, 0);">#&nbsp;This&nbsp;program&nbsp;uses&nbsp;the&nbsp;turtle&nbsp;to&nbsp;draw&nbsp;the</span><br><span style="color:rgb(170, 85, 0);">#&nbsp;numbers&nbsp;using&nbsp;7&nbsp;segments.</span><br><br><span style="color:rgb(119, 0, 136);">from</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">turtle</span><span>&nbsp;</span><span style="color:rgb(119, 0, 136);">import</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">hop</span><span>,</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">lt</span><span>,</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">rt</span><span>,</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">fd</span><span>,</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">pensize</span><br><br><span style="color:rgb(1, 1, 1);">dist</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">=</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">20</span><span>&nbsp;&nbsp;</span><span style="color:rgb(170, 85, 0);">#&nbsp;length&nbsp;of&nbsp;each&nbsp;segment</span><br><br><span style="color:rgb(1, 1, 1);">segments</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">=</span><span>&nbsp;</span><span>{</span><span>&nbsp;&nbsp;</span><span style="color:rgb(170, 85, 0);">#&nbsp;the&nbsp;7&nbsp;segments&nbsp;of&nbsp;each&nbsp;symbol</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(170, 17, 17);">&#39;&nbsp;&#39;</span><span>:</span><span style="color:rgb(17, 102, 68);">0</span><span>,</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(170, 17, 17);">&#39;0&#39;</span><span>:</span><span style="color:rgb(17, 102, 68);">63</span><span>,</span><span>&nbsp;</span><span style="color:rgb(170, 17, 17);">&#39;1&#39;</span><span>:</span><span style="color:rgb(17, 102, 68);">12</span><span>,</span><span>&nbsp;&nbsp;</span><span style="color:rgb(170, 17, 17);">&#39;2&#39;</span><span>:</span><span style="color:rgb(17, 102, 68);">118</span><span>,</span><span>&nbsp;</span><span style="color:rgb(170, 17, 17);">&#39;3&#39;</span><span>:</span><span style="color:rgb(17, 102, 68);">94</span><span>,</span><span>&nbsp;&nbsp;</span><span style="color:rgb(170, 17, 17);">&#39;4&#39;</span><span>:</span><span style="color:rgb(17, 102, 68);">77</span><span>,</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(170, 17, 17);">&#39;5&#39;</span><span>:</span><span style="color:rgb(17, 102, 68);">91</span><span>,</span><span>&nbsp;</span><span style="color:rgb(170, 17, 17);">&#39;6&#39;</span><span>:</span><span style="color:rgb(17, 102, 68);">123</span><span>,</span><span>&nbsp;</span><span style="color:rgb(170, 17, 17);">&#39;7&#39;</span><span>:</span><span style="color:rgb(17, 102, 68);">14</span><span>,</span><span>&nbsp;&nbsp;</span><span style="color:rgb(170, 17, 17);">&#39;8&#39;</span><span>:</span><span style="color:rgb(17, 102, 68);">127</span><span>,</span><span>&nbsp;</span><span style="color:rgb(170, 17, 17);">&#39;9&#39;</span><span>:</span><span style="color:rgb(17, 102, 68);">95</span><br><span>}</span><br><br><span style="color:rgb(119, 0, 136);">def</span><span>&nbsp;</span><span style="color:rgb(0, 0, 255);">print_char</span><span>(</span><span style="color:rgb(1, 1, 1);">c</span><span>)</span><span>:</span><span>&nbsp;&nbsp;</span><span style="color:rgb(170, 85, 0);">#&nbsp;draw&nbsp;a&nbsp;symbol</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">lt</span><span>(</span><span style="color:rgb(17, 102, 68);">90</span><span>)</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">hop</span><span>(</span><span>)</span><span>;</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">fd</span><span>(</span><span style="color:rgb(1, 1, 1);">dist</span><span>)</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">bits</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">=</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">segments</span><span>[</span><span style="color:rgb(1, 1, 1);">c</span><span>]</span><span>&nbsp;&nbsp;</span><span style="color:rgb(170, 85, 0);">#&nbsp;each&nbsp;bit&nbsp;=&nbsp;1&nbsp;segment</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(119, 0, 136);">for</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(119, 0, 136);">in</span><span>&nbsp;</span><span style="color:rgb(51, 0, 170);">range</span><span>(</span><span style="color:rgb(17, 102, 68);">7</span><span>)</span><span>:</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(119, 0, 136);">if</span><span>&nbsp;</span><span>(</span><span style="color:rgb(1, 1, 1);">bits</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">&#38;</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">1</span><span>)</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">==</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">0</span><span>:</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">hop</span><span>(</span><span>)</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">fd</span><span>(</span><span style="color:rgb(1, 1, 1);">dist</span><span>)</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">bits</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">&#62;&#62;=</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">1</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(119, 0, 136);">if</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">!=</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">2</span><span>:</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">rt</span><span>(</span><span style="color:rgb(17, 102, 68);">90</span><span>)</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">hop</span><span>(</span><span>)</span><span>;</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">fd</span><span>(</span><span style="color:rgb(1, 1, 1);">dist</span><span>)</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">lt</span><span>(</span><span style="color:rgb(17, 102, 68);">90</span><span>)</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">hop</span><span>(</span><span>)</span><span>;</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">fd</span><span>(</span><span style="color:rgb(1, 1, 1);">dist</span><span style="color:rgb(1, 1, 1);">/</span><span style="color:rgb(17, 102, 68);">2</span><span>)</span><br><br><span style="color:rgb(119, 0, 136);">def</span><span>&nbsp;</span><span style="color:rgb(51, 0, 170);">print</span><span>(</span><span style="color:rgb(1, 1, 1);">n</span><span>)</span><span>:</span><span>&nbsp;&nbsp;</span><span style="color:rgb(170, 85, 0);">#&nbsp;print&nbsp;a&nbsp;number</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(119, 0, 136);">for</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">c</span><span>&nbsp;</span><span style="color:rgb(119, 0, 136);">in</span><span>&nbsp;</span><span style="color:rgb(51, 0, 170);">str</span><span>(</span><span style="color:rgb(1, 1, 1);">n</span><span>)</span><span>:</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">print_char</span><span>(</span><span style="color:rgb(1, 1, 1);">c</span><span>)</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(1, 1, 1);">print_char</span><span>(</span><span style="color:rgb(170, 17, 17);">&#39;&nbsp;&#39;</span><span>)</span><br><br><span style="color:rgb(1, 1, 1);">hop</span><span>(</span><span>)</span><span>;</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">fd</span><span>(</span><span style="color:rgb(1, 1, 1);">-</span><span style="color:rgb(17, 102, 68);">175</span><span>)</span><span>;</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">pensize</span><span>(</span><span style="color:rgb(17, 102, 68);">5</span><span>)</span><br><br><span style="color:rgb(119, 0, 136);">for</span><span>&nbsp;</span><span style="color:rgb(1, 1, 1);">i</span><span>&nbsp;</span><span style="color:rgb(119, 0, 136);">in</span><span>&nbsp;</span><span style="color:rgb(51, 0, 170);">range</span><span>(</span><span style="color:rgb(17, 102, 68);">1</span><span>,</span><span>&nbsp;</span><span style="color:rgb(17, 102, 68);">6</span><span>)</span><span>:</span><span>&nbsp;&nbsp;</span><span style="color:rgb(170, 85, 0);">#&nbsp;compute&nbsp;the&nbsp;squares</span><br><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:rgb(51, 0, 170);">print</span><span>(</span><span style="color:rgb(1, 1, 1);">i</span><span style="color:rgb(1, 1, 1);">*</span><span style="color:rgb(1, 1, 1);">*</span><span style="color:rgb(17, 102, 68);">2</span><span>)</span></code></div></div>

</ol>


</body>
</html>