>>> 3 + 4
7
You can issue commands in the Python interactive console. But it has a disadvantage: The program that you type is lost when the session ends. It is good for trying simple commands. But you need a way to save more complex programs somewhere.
Open your editor (You should have an editor installed. If not, follow the instructions in previous lesson.)
Create a new file in your editor and type the following code:
print("Hello world!")
Then save the file as hello.py
.
02
in the pyladies
(or whatever folder name you've created last lecture) directory.hello.py
in it.If you can choose the encoding in your editor, you should use UTF-8
.
If you can choose the file type, use .py
or All Formats
.
Some operating systems or editors hide extensions or add their own extensions.
You can check the real name using the command line.
Open your command line and change your current directory using the cd
command to
~/pyladies/02.
List what is in the directory using the command ls
(Mac or Linux) or dir
(Windows)
and check that the filename is really hello.py
and not, for example, hello.py.txt
.
Open the command line and activate the virtual environment.
Change to the ~/pyladies/02
directory and issue the following command:
$ python hello.py
You have learned about the command line in
a previous lesson which shows how to change the current directory
using the cd
command.
You know how to activate the virtual environment since the lesson about
Python installation.
Do you see the greeting? If you do, you just wrote your first Python program!
If it does not work, make sure that:
(venv)
in your command line prompt. If you do not see it,
use the activate
or source activate
command that you have used in a previous lession.)~/pyladies/02
(you need to replace ~/pyladies
with the directory that you have created previously).hello.py
file contains the correct command, including quotes and parentheses.$
or >
character in the command line – it is there to indicate that this is a command line.
It is printed by the operating system after any program has finished.
You type only: python hello.py
.If it still does not work, ask your coach.
Note about code style
It does not usually matter where you use space in Python inside a command.
The command print("Hello world!")
has the same effect as:
print ( "Hello world!" )
It is however a good practice to follow some typical guidelines.
In English, we do not write a space after an opening parenthesis.
In Python, we do not write a space even between print
and (
.
The recommended style is:
print("Hello world!")
The spaces between the quotes have a meaning: If you type
" Hello world!"
, then the extra spaces are printed.
{ "data": { "sessionMaterial": { "id": "session-material:2018/pyladies-en-prague:hello:0", "title": "First program", "html": "\n \n \n\n <h1>My First Program</h1>\n<div class=\"highlight\"><pre><span></span><span class=\"gp\">>>> </span><span class=\"mi\">3</span> <span class=\"o\">+</span> <span class=\"mi\">4</span>\n<span class=\"go\">7</span>\n</pre></div><p>You can issue commands in the <em>Python interactive console</em>. \nBut it has a disadvantage:\nThe program that you type is lost when the session ends.\nIt is good for trying simple commands. \nBut you need a way to save more complex programs somewhere.</p>\n<p>Open your editor\n(You should have an editor installed. If not, follow the instructions in \n<a href=\"/2018/pyladies-en-prague/beginners-en/install-editor/\">previous lesson</a>.)</p>\n<p>Create a new file in your editor and type the following code:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s2\">"Hello world!"</span><span class=\"p\">)</span>\n</pre></div><p>Then save the file as <code>hello.py</code>.</p>\n<ul>\n<li>You have to create the subdirectory named <code>02</code> in the <code>pyladies</code>\n(or whatever folder name you've created last lecture) directory.</li>\n<li>Then you can store the file as <code>hello.py</code> in it.</li>\n</ul>\n<p>If you can choose the <em>encoding</em> in your editor, you should use <code>UTF-8</code>.\nIf you can choose the file type, use <code>.py</code> or <code>All Formats</code>.</p>\n<p>Some operating systems or editors hide extensions or add their own extensions.\nYou can check the real name using the command line.\nOpen your command line and change your current directory using the <code>cd</code> command to\n~/pyladies/02.\nList what is in the directory using the command <code>ls</code> (Mac or Linux) or <code>dir</code> (Windows) \nand check that the filename is really <code>hello.py</code> and not, for example, <code>hello.py.txt</code>.</p>\n<h2>Executing Your Program</h2>\n<p>Open the command line and activate the virtual environment.\nChange to the <code>~/pyladies/02</code> directory and issue the following command:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"gp\">$ </span>python hello.py\n</pre></div><div class=\"admonition note\"><p>You have learned about the command line in \na <a href=\"/2018/pyladies-en-prague/beginners-en/cmdline/\">previous lesson</a> which shows how to change the current directory \nusing the <code>cd</code> command.\nYou know how to activate the virtual environment since the lesson about\n<a href=\"/2018/pyladies-en-prague/beginners-en/install/\">Python installation</a>.</p>\n</div><p>Do you see the greeting? If you do, you just wrote your first Python program!</p>\n<p>If it does not work, make sure that:</p>\n<ul>\n<li>You have activated the virtual environment.\n(You should see the <code>(venv)</code> in your command line prompt. If you do not see it, \nuse the <code>activate</code> or <code>source activate</code> command that you have used in a <a href=\"/2018/pyladies-en-prague/beginners-en/install/\">previous lession</a>.)</li>\n<li>You are in the correct directory: <code>~/pyladies/02</code>\n(you need to replace <code>~/pyladies</code> with the directory that you have created previously).</li>\n<li>The <code>hello.py</code> file contains the correct command, including quotes and parentheses.</li>\n<li>Do not type <code>$</code> or <code>></code> character in the command line – it is there to indicate that this is a command line.\nIt is printed by the operating system after any program has finished.\nYou type only: <code>python hello.py</code>.</li>\n</ul>\n<p>If it still does not work, ask \nyour coach.</p>\n<div class=\"admonition style-note\"><p class=\"admonition-title\">Note about code style</p>\n<p>It does not usually matter where you use space in Python inside a command. \nThe command <code>print("Hello world!")</code> has the same effect as:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">print</span> <span class=\"p\">(</span> <span class=\"s2\">"Hello world!"</span> <span class=\"p\">)</span>\n</pre></div><p>It is however a good practice to follow some typical guidelines.\nIn English, we do not write a space after an opening parenthesis.\nIn Python, we do not write a space even between <code>print</code> and <code>(</code>.\nThe recommended style is:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"s2\">"Hello world!"</span><span class=\"p\">)</span>\n</pre></div><p>The spaces between the quotes have a meaning: If you type\n<code>" Hello world!"</code>, then the extra spaces are printed.</p>\n</div>\n\n\n " } } }