Create a file ~/pyladies/02/printing.py
(in the editor)
and write the following commands:
print(1)
print(1, 2, 3)
print(1 + 1)
print(3 * 8)
print(10 - 2.2)
print(3 + (4 + 6) * 8 / 2 - 1)
print('*' * 80)
print("Hello" + " " + "PyLadies!")
print("Sum of numbers 3 and 8 is", 3 + 8)
print('Twinkle, twinkle, little star')
print(How I wonder what you are.)
Now run the program. Does it work?
You will often find out that the code you wrote isn't working on its first run. It's because the computer is not that smart and you have to write the commands in the exact way, according to the Python rules. Don't worry, it happens even to experienced programmers. The important thing is to know how to find out what is wrong. The error messages will help you with that. For example, after we run our program, it will print this:
File "~/pyladies/02/printing.py", line 11 print(How I wonder what you are.) ^ SyntaxError: invalid syntax
First, Python prints the name of the file and line number, where the error is. Then it prints the whole line with the mistake. And finally the error type (in our case it's "syntax error") and eventually some more info.
How is this error different from the one that happens when you
try to add up a number and text? Or when you try to divide by zero?
Error messages can be hard to understand in the beginning, but you will get used to them with practice. For now, the important thing for you will be the line number. When you know that the mistake is on line 11, you can look at that line and try to find it.
When you can't find it there, it can also be a few lines above or below. Python sometimes doesn't share human views where the mistake actually is and it shows you where it noticed the mistake.
In our case the mistake is that we don't have quotation marks around the string. So add them there and run the program again. If it works, congratulations! If not, try to correct the program and repeat until it works :)
Now that our program works we can take a closer look at what is happening when it's running. It's quite simple: Commands are performed one after the other from top to bottom. The program is like a cooking recipe: a list of instructions that tells you what to do.
Soon your programs will look more like a sorcerer's potion (wait for the full moon and if Mars is in conjunction with Jupiter then stir three times) but the principle is still the same: the computer reads the commands from top to bottom and performs them one after the other.
And which instructions make up our "recipe"?
That print
which we use is a function. We will talk
about functions later, now all you need to know is that
if you type print
and after that, in parentheses, some
expressions separated by comma, the values of those
expressions will be printed.
And what are those expressions?
You have some examples in our code:
An expression can be a number, a string, or some (e.g. mathematical)
operations composed of multiple expressions.
For example the expression 3 + 8
will add up 3
and 8
.
We will focus on expressions and their values more in the lesson about variables.
Typography
Notice that there are no spaces around the parentheses:
print("Hello!")
We can write a space after a comma, but not before:
print(1, 2, 3)
We can also write spaces around mathematical operators:
print(2 + 8)
print("One and a half is ", 1 + 1/2)
{ "data": { "sessionMaterial": { "id": "session-material:2018/pyladies-en-prague:hello:1", "title": "Print and errors", "html": "\n \n \n\n <h1>Print and errors</h1>\n<p>Create a file <code>~/pyladies/02/printing.py</code> (in the editor)\nand write the following commands:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"mi\">3</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"mi\">1</span> <span class=\"o\">+</span> <span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"mi\">3</span> <span class=\"o\">*</span> <span class=\"mi\">8</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"mi\">10</span> <span class=\"o\">-</span> <span class=\"mf\">2.2</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"mi\">3</span> <span class=\"o\">+</span> <span class=\"p\">(</span><span class=\"mi\">4</span> <span class=\"o\">+</span> <span class=\"mi\">6</span><span class=\"p\">)</span> <span class=\"o\">*</span> <span class=\"mi\">8</span> <span class=\"o\">/</span> <span class=\"mi\">2</span> <span class=\"o\">-</span> <span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'*'</span> <span class=\"o\">*</span> <span class=\"mi\">80</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s2\">"Hello"</span> <span class=\"o\">+</span> <span class=\"s2\">" "</span> <span class=\"o\">+</span> <span class=\"s2\">"PyLadies!"</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s2\">"Sum of numbers 3 and 8 is"</span><span class=\"p\">,</span> <span class=\"mi\">3</span> <span class=\"o\">+</span> <span class=\"mi\">8</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s1\">'Twinkle, twinkle, little star'</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">How</span> <span class=\"n\">I</span> <span class=\"n\">wonder</span> <span class=\"n\">what</span> <span class=\"n\">you</span> <span class=\"n\">are</span><span class=\"o\">.</span><span class=\"p\">)</span>\n</pre></div><p>Now run the program. Does it work?</p>\n<h2>How to read error messages</h2>\n<p>You will often find out that the code you wrote isn't working on its first run.\nIt's because the computer is not that smart and you have to write the commands in the exact way,\naccording to the Python rules. Don't worry, it happens even to experienced programmers.\nThe important thing is to know how to find out what is wrong. The error messages will help you with\nthat. For example, after we run our program, it will print this:</p>\n<pre> File "<span class=\"plhome\">~/pyladies</span>/02/printing.py", line <span class=\"err-lineno\">11</span>\n print(How I wonder what you are.)\n ^\n<span class=\"err-exctype\">SyntaxError</span>: invalid syntax\n</pre><p>First, Python prints the name of the file and <span class=\"err-lineno\">line number</span>,\nwhere the error is.\nThen it prints the whole line with the mistake.\nAnd finally the <span class=\"err-exctype\">error type</span>\n(in our case it's "syntax error") and eventually some more info.</p>\n<div class=\"admonition note\"><p class=\"admonition-title\">How is this error different from the one that happens when you</p>\n<p>try to add up a number and text? Or when you try to divide by zero?</p>\n</div><p>Error messages can be hard to understand in the beginning, but\nyou will get used to them with practice.\nFor now, the important thing for you will be the line number.\nWhen you know that the mistake is on line <span class=\"err-lineno\">11</span>,\nyou can look at that line and try to find it.</p>\n<p>When you can't find it there, it can also be a few lines above or below.\nPython sometimes doesn't share human views where the mistake actually <em>is</em>\nand it shows you where it <em>noticed</em> the mistake.</p>\n<p>In our case the mistake is that we don't have quotation marks around\nthe string. So add them there and run the program again.\nIf it works, congratulations!\nIf not, try to correct the program and repeat until it works :)</p>\n<h2>How the program works</h2>\n<p>Now that our program works we can take a closer look at what is happening\nwhen it's running.\nIt's quite simple: Commands are performed one after the other from top to bottom.\nThe program is like a cooking recipe: a list of instructions that tells you what to do.</p>\n<p>Soon your programs will look more like a sorcerer's potion\n(<em>wait for the full moon and if Mars is in conjunction with\nJupiter then stir three times</em>) but the principle is still\nthe same: the computer reads the commands from top to bottom\nand performs them one after the other.</p>\n<h2>Print and expressions</h2>\n<p>And which instructions make up our "recipe"?</p>\n<p>That <code>print</code> which we use is a <em>function</em>. We will talk\nabout functions later, now all you need to know is that\nif you type <code>print</code> and after that, in parentheses, some\n<em>expressions</em> separated by comma, the values of those\nexpressions will be printed.</p>\n<p>And what are those expressions?\nYou have some examples in our code:\nAn expression can be a number, a string, or some (e.g. mathematical)\noperations composed of multiple expressions.\nFor example the expression <code>3 + 8</code> will add up <code>3</code> and <code>8</code>.</p>\n<p>We will focus on expressions and their values more in\nthe lesson about <a href=\"/2018/pyladies-en-prague/beginners/variables/\">variables</a>.</p>\n<div class=\"admonition style-note\"><p class=\"admonition-title\">Typography</p>\n<p>Notice that there are no spaces around\nthe parentheses:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s2\">"Hello!"</span><span class=\"p\">)</span>\n</pre></div><p>We can write a space after a comma, but not before:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"mi\">1</span><span class=\"p\">,</span> <span class=\"mi\">2</span><span class=\"p\">,</span> <span class=\"mi\">3</span><span class=\"p\">)</span>\n</pre></div><p>We can also write spaces around mathematical operators:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"mi\">2</span> <span class=\"o\">+</span> <span class=\"mi\">8</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s2\">"One and a half is "</span><span class=\"p\">,</span> <span class=\"mi\">1</span> <span class=\"o\">+</span> <span class=\"mi\">1</span><span class=\"o\">/</span><span class=\"mi\">2</span><span class=\"p\">)</span>\n</pre></div></div>\n\n\n " } } }