Square

Now we'll go back to elementary school and try to write a program that calculates the perimeter and the area of a square.

Maths

I hope that this won't scare anyone off but the word "computer" is derived from the word computing. So there's no need to be scared, knowledge from elementary school will be enough for basic programming.

The perimeter of a square with a side length of a can be computed by the P = 4a formula, and the area formula is S = a². So let's say that our square has a side length of a = 356 cm.

Print the result with print(). Save the program into the file ~/pyladies/02/sqare.py and run it; this is what it should print:

The perimeter of a square with a side of 356 cm is 1424 cm.
The area of a square with a side of 356 cm is 126736 cm2

The result should be computed by Python so don't write the numbers 1424 and 126736 into your code.
If you don't know what to do, look into your program printing.py from the lesson about print, where one of the lines does a similar thing.

Řešení

Smaller square

If everything works, try to change the program so it computes the perimeter and the area of a square with a side of 123 cm.

Řešení

Variables

Could you make it even for a side of 3945 cm, 832 cm, 956 cm? Do you enjoy rewriting numbers? If the program were longer (few pages) how would you make sure that you didn't forget to rewrite one of the numbers?

There is a way how to write a program without rewriting all the numbers every time: You name the side of the square and then you just use that name. In Python, variables are used to name values. They are being used this way:

side = 123
print("The perimeter of a square with a side of", side,"cm is ", side * 4,"cm.")
print("The area of a square with a side of", side,"cm is", side * side, "cm2.")

So you write the name, then = and after that the expression whose value will be assigned to that variable. When ever you write the name of the variable, Python will use just its value.

The convention here is to put a space before and after the equals sign.

Which leads us to one of the core principles of programming: Don't repeat yourself, DRY. When there is a value, an expression or the same piece of code repeatedly, a good programmer will name that part, and then they use the name several times. It often happens that the program needs to be changed - either there is a mistake or the task has changed. And then, it is easier to make that change only in one place.

On top of that, clear names makes reading the program much easier: 4 * side (maybe squareSide would be clearer) doesn't need any comment, but with 4 * 183, it's not clear what the numbers mean.

Circles

This is an extra task! You can skip it.

Change of the task! Try to expand the program so that it computes the perimeter and the area of a circle where the radius will be the same value as the side length from your code. The perimeter of a circle with radius r is o = 2πr, the area is S = πr² and π is approx. 3.1415926.

Name all variables appropriately.

Comments

Now we will make our code clearer with comments. In Python, the comment begins with a Hash (Pound) sign #, after which you can write anything until the end of the line. Everything is ignored.

Comments are important! Programs are not read only by computers, but also by other humans. In your comments, you can include statements like: what the whole program does, explain how a more complicated part works and clarify something that is not clear enough.

Whenever you write a program, try to get into the role of someone who will read it, and all that may be unclear should be specified in the comments. Help yourself. After a few months you will not remember what the code is about.

# This program computes the perimeter and the area of a square

side = 123
print("The perimeter of a square with a side of", side,"cm is ", side * 4,"cm.")
print("The area of a square with a side of", side,"cm is", side * side, "cm2.")

The convention is that when you write a comment on the same line as the code, there are two spaces or more before #, then after # there is one more space.

Input

Finally, we learn how to improve the program so that we don't have to write the number in the program - and users can (in)put their own number.

Just like you used print, we will now use a different function to capture user input: We will explain the details later, for now, just remember these:

  • If you want to retrieve a text(string), use:

    variable = input('Enter some text: ')
    
  • If you want to retrieve a whole number, use:

    variable = int(input('Enter some whole number: '))
    
  • If you want to retrieve a decimal, use:

    variable = float(input('Enter some decimal: '))
    

    The text inside the parentheses can be tailored according to your needs. It serves as the prompt for the user, so use it to ask for any needed info.

The final code might look like this:

# This program computes the perimeter and the area of a sqare

side = float(input('Enter the side of a square in centimeters: '))
print("The perimeter of a square with a side of", side,"cm is ", side * 4,"cm.")
print("The area of a square with a side of", side,"cm is", side * side, "cm2.")
{
  "data": {
    "sessionMaterial": {
      "id": "session-material:2018/pyladies-en-prague:hello:2",
      "title": "Variables",
      "html": "\n          \n    \n\n    <h1>Square</h1>\n<p>Now we&apos;ll go back to elementary school and try to write a program\nthat calculates the perimeter and the area of a square.</p>\n<div class=\"admonition note\"><p class=\"admonition-title\">Maths</p>\n<p>I hope that this won&apos;t scare anyone off\nbut the word &quot;computer&quot; is derived from the\nword <em>computing</em>. So there&apos;s no need\nto be scared, knowledge from elementary\nschool will be enough for basic programming.</p>\n</div><p>The perimeter of a square with a side length of <var>a</var>\ncan be computed by the <var>P</var> = 4<var>a</var>\nformula, and the area formula is <var>S</var> = <var>a</var>&#xB2;.\nSo let&apos;s say that our square has a side length of <var>a</var> = 356 cm.</p>\n<p>Print the result with <code>print()</code>.\nSave the program into the file <code>~/pyladies/02/sqare.py</code>\nand run it; this is what it should print:</p>\n<div class=\"highlight\"><pre><code>The perimeter of a square with a side of 356 cm is 1424 cm.\nThe area of a square with a side of 356 cm is 126736 cm2</code></pre></div><p>The result should be computed by Python so don&apos;t write the\nnumbers 1424 and 126736 into your code. <br>\nIf you don&apos;t know what to do, look into your program <code>printing.py</code>\nfrom&#xA0;the lesson about <a href=\"/2018/pyladies-en-prague/beginners/print/\"><code>print</code></a>,\nwhere one of the lines does a similar thing.</p>\n<div class=\"solution\" id=\"solution-0\">\n    <h3>&#x158;e&#x161;en&#xED;</h3>\n    <div class=\"solution-cover\">\n        <a href=\"/2018/pyladies-en-prague/beginners-en/variables/index/solutions/0/\"><span class=\"link-text\">Uk&#xE1;zat &#x159;e&#x161;en&#xED;</span></a>\n    </div>\n    <div class=\"solution-body\" aria-hidden=\"true\">\n        <p>A program which prints the right result could look like this:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">&apos;The perimeter of a square with a side of 356 cm is&apos;</span><span class=\"p\">,</span> <span class=\"mi\">4</span> <span class=\"o\">*</span> <span class=\"mi\">356</span><span class=\"p\">,</span> <span class=\"s1\">&apos;cm&apos;</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">&apos;The area of a square with a side of 356 cm is&apos;</span><span class=\"p\">,</span> <span class=\"mi\">356</span> <span class=\"o\">*</span> <span class=\"mi\">356</span><span class=\"p\">,</span> <span class=\"s1\">&apos;cm2&apos;</span><span class=\"p\">)</span>\n</pre></div>\n    </div>\n</div><h2>Smaller square</h2>\n<p>If everything works, try to change the program\nso it computes the perimeter and the area\nof a square with a side of 123 cm.</p>\n<div class=\"solution\" id=\"solution-1\">\n    <h3>&#x158;e&#x161;en&#xED;</h3>\n    <div class=\"solution-cover\">\n        <a href=\"/2018/pyladies-en-prague/beginners-en/variables/index/solutions/1/\"><span class=\"link-text\">Uk&#xE1;zat &#x159;e&#x161;en&#xED;</span></a>\n    </div>\n    <div class=\"solution-body\" aria-hidden=\"true\">\n        <div class=\"highlight\"><pre><span></span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">&apos;The perimeter of a square with a side of 123 cm is&apos;</span><span class=\"p\">,</span> <span class=\"mi\">4</span> <span class=\"o\">*</span> <span class=\"mi\">123</span><span class=\"p\">,</span> <span class=\"s1\">&apos;cm&apos;</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">&apos;The area of a square with a side of 123 cm is&apos;</span><span class=\"p\">,</span> <span class=\"mi\">123</span> <span class=\"o\">*</span> <span class=\"mi\">123</span><span class=\"p\">,</span> <span class=\"s1\">&apos;cm2&apos;</span><span class=\"p\">)</span>\n</pre></div>\n    </div>\n</div><h2>Variables</h2>\n<p>Could you make it even for a side of 3945 cm, 832 cm, 956 cm?\nDo you enjoy rewriting numbers?\nIf the program were longer (few pages)\nhow would you make sure that you didn&apos;t forget\nto rewrite one of the numbers?</p>\n<p>There is a way how to write a program without\nrewriting all the numbers every time:\nYou name the side of the square and then you just\nuse that name. In Python, <em>variables</em> are used to name values.\nThey are being used this way:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">side</span> <span class=\"o\">=</span> <span class=\"mi\">123</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s2\">&quot;The perimeter of a square with a side of&quot;</span><span class=\"p\">,</span> <span class=\"n\">side</span><span class=\"p\">,</span><span class=\"s2\">&quot;cm is &quot;</span><span class=\"p\">,</span> <span class=\"n\">side</span> <span class=\"o\">*</span> <span class=\"mi\">4</span><span class=\"p\">,</span><span class=\"s2\">&quot;cm.&quot;</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s2\">&quot;The area of a square with a side of&quot;</span><span class=\"p\">,</span> <span class=\"n\">side</span><span class=\"p\">,</span><span class=\"s2\">&quot;cm is&quot;</span><span class=\"p\">,</span> <span class=\"n\">side</span> <span class=\"o\">*</span> <span class=\"n\">side</span><span class=\"p\">,</span> <span class=\"s2\">&quot;cm2.&quot;</span><span class=\"p\">)</span>\n</pre></div><p>So you write the name, then <code>=</code> and after that\nthe expression whose value will be <em>assigned</em>\nto that variable.\nWhen ever you write the name of the variable,\nPython will use just its value.</p>\n<div class=\"admonition style-note\"><p>The convention here is to put a space before and after the equals sign.</p>\n</div><p>Which leads us to one of the core principles of programming:\n<em>Don&apos;t repeat yourself</em>, <abbr class=\"initialism\">DRY</abbr>.\nWhen there is a value, an expression or the same\npiece of code repeatedly, a good programmer will\nname that part, and then they use the name several times.\nIt often happens that the program needs to be changed - either\nthere is a mistake or the task has changed.\nAnd then, it is easier to make that change only in one place.</p>\n<p>On top of that, clear names makes reading the\nprogram much easier: <code>4 * side</code> (maybe <code>squareSide</code> would be clearer)\ndoesn&apos;t need any comment, but with <code>4 * 183</code>, it&apos;s not clear what\nthe numbers mean.</p>\n<div class=\"admonition extra-activity\"><h2>Circles</h2>\n<p><em>This is an extra task! You can skip it.</em></p>\n<p>Change of the task!\nTry to expand the program so that it computes the perimeter and the area of\na circle where the radius will be the same value as the side length from your code.\nThe perimeter of a circle with radius <var>r</var>\nis <var>o</var> = 2&#x3C0;<var>r</var>, the area is <var>S</var> = &#x3C0;<var>r</var>&#xB2;\nand &#x3C0; is approx. 3.1415926.</p>\n<p>Name all variables appropriately.</p>\n</div><h2>Comments</h2>\n<p>Now we will make our code clearer with <em>comments</em>.\nIn Python, the comment begins with a Hash (Pound) sign #,\nafter which you can write anything until the end of the line. Everything is ignored.</p>\n<p>Comments are important! Programs are not read only by computers, but also by other humans.\nIn your comments, you can include statements like: what the whole program does,\nexplain how a more complicated part works and clarify something\nthat is not clear enough.</p>\n<p>Whenever you write a program, try to get into the role of someone who will read it,\nand all that may be unclear should be specified in the comments.\nHelp yourself. After a few months you will not remember what the code is about.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"c1\"># This program computes the perimeter and the area of a square</span>\n\n<span class=\"n\">side</span> <span class=\"o\">=</span> <span class=\"mi\">123</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s2\">&quot;The perimeter of a square with a side of&quot;</span><span class=\"p\">,</span> <span class=\"n\">side</span><span class=\"p\">,</span><span class=\"s2\">&quot;cm is &quot;</span><span class=\"p\">,</span> <span class=\"n\">side</span> <span class=\"o\">*</span> <span class=\"mi\">4</span><span class=\"p\">,</span><span class=\"s2\">&quot;cm.&quot;</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s2\">&quot;The area of a square with a side of&quot;</span><span class=\"p\">,</span> <span class=\"n\">side</span><span class=\"p\">,</span><span class=\"s2\">&quot;cm is&quot;</span><span class=\"p\">,</span> <span class=\"n\">side</span> <span class=\"o\">*</span> <span class=\"n\">side</span><span class=\"p\">,</span> <span class=\"s2\">&quot;cm2.&quot;</span><span class=\"p\">)</span>\n</pre></div><div class=\"admonition style-note\"><p>The convention is that when you write a comment on the same line\nas the code, there are two spaces or more before <code>#</code>,\nthen after <code>#</code> there is one more space.</p>\n</div><h2>Input</h2>\n<p>Finally, we learn how to improve the program so that we don&apos;t have to write the number \nin the program - and users can (in)put their own number.</p>\n<p>Just like you used <code>print</code>, we will now use a different <em>function</em>\nto capture user input:\nWe will explain the details later, for now, just remember these:</p>\n<ul>\n<li><p>If you want to retrieve <strong>a text(string)</strong>, use:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">variable</span> <span class=\"o\">=</span> <span class=\"nb\">input</span><span class=\"p\">(</span><span class=\"s1\">&apos;Enter some text: &apos;</span><span class=\"p\">)</span>\n</pre></div></li>\n<li><p>If you want to retrieve <strong>a whole number</strong>, use:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">variable</span> <span class=\"o\">=</span> <span class=\"nb\">int</span><span class=\"p\">(</span><span class=\"nb\">input</span><span class=\"p\">(</span><span class=\"s1\">&apos;Enter some whole number: &apos;</span><span class=\"p\">))</span>\n</pre></div></li>\n<li><p>If you want to retrieve <strong>a decimal</strong>, use:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"n\">variable</span> <span class=\"o\">=</span> <span class=\"nb\">float</span><span class=\"p\">(</span><span class=\"nb\">input</span><span class=\"p\">(</span><span class=\"s1\">&apos;Enter some decimal: &apos;</span><span class=\"p\">))</span>\n</pre></div><p>The text inside the parentheses can be tailored according to your needs.\nIt serves as the prompt for the user, so use it to ask for any needed info.</p>\n</li>\n</ul>\n<p>The final code might look like this:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"c1\"># This program computes the perimeter and the area of a sqare</span>\n\n<span class=\"n\">side</span> <span class=\"o\">=</span> <span class=\"nb\">float</span><span class=\"p\">(</span><span class=\"nb\">input</span><span class=\"p\">(</span><span class=\"s1\">&apos;Enter the side of a square in centimeters: &apos;</span><span class=\"p\">))</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s2\">&quot;The perimeter of a square with a side of&quot;</span><span class=\"p\">,</span> <span class=\"n\">side</span><span class=\"p\">,</span><span class=\"s2\">&quot;cm is &quot;</span><span class=\"p\">,</span> <span class=\"n\">side</span> <span class=\"o\">*</span> <span class=\"mi\">4</span><span class=\"p\">,</span><span class=\"s2\">&quot;cm.&quot;</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s2\">&quot;The area of a square with a side of&quot;</span><span class=\"p\">,</span> <span class=\"n\">side</span><span class=\"p\">,</span><span class=\"s2\">&quot;cm is&quot;</span><span class=\"p\">,</span> <span class=\"n\">side</span> <span class=\"o\">*</span> <span class=\"n\">side</span><span class=\"p\">,</span> <span class=\"s2\">&quot;cm2.&quot;</span><span class=\"p\">)</span>\n</pre></div>\n\n\n        "
    }
  }
}