First commands in Python

Let's see if we installed Python successfully!

First check if your virtual environment is active. (You should see (myenv) in the beginning of your command line.)

If it's there, we can now start Python (specifically, the Python console). To do that, just write python:

(myenv)$ python
Python 3.4.0 (default, Jan 26 2014, 18:15:05)
[GCC 4.8.2 20131212 (Red Hat 4.8.2-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

This command will print some information. in the first line, it shows which version of Python you have (you should have Python 3). The command line starts with three "greater than" signs. This way Python prompts (asks) you for instructions. This is the same as the standard command line, but instead of commands like cd or mkdir, you write Python commands.

The simplest command is a number. Try it:

>>> 1
1
>>> 42
42
>>> -8.3    # (Python uses period as decimal point)
-8.3

Python prints the greater-than signs >>> and the answer by itself! You just write number and press Enter.

Python can also add up numbers, like this:

>>> 8 + 2
10

Notice that commands from the standard command line do not work here, although the window looks the same:

>>> whoami
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'whoami' is not defined

This is an error message which appears every time when you do anything wrong. You will see a lot of them during the course, so look at it carefully so you recognise it later.

If you got this far, congratulations! You have installed Python and it works. You can now quit the Python console and close the command prompt. To quit, just type quit() with empty parentheses.

>>> quit()
(myenv)$

The greater-than signs >>> change back to (myenv) followed by $ or >. Now commands like whoami and cd work again, but Python commands like 1 + 2 don't wotk, until you enter the Python console
with python again.

To quit the virtual environment, type deactivate or source deactivate (for Linux and Mac) -- this time without parentheses.

(myenv)$ deactivate

The command prompt (terminal) can be closed by typing exit.

$ exit

As an exercise, try to run the Python console again -- open the command prompt (the terminal), then activate the virtual environment, and finally run Python.

{
  "data": {
    "sessionMaterial": {
      "id": "session-material:2018/pyladies-en-prague:install:3",
      "title": "First steps",
      "html": "\n          \n    \n\n    <h2>First commands in Python</h2>\n<p>Let&apos;s see if we installed Python successfully!</p>\n<p>First check if your virtual environment is active. (You should see <code>(myenv)</code>\nin the beginning of your command line.)</p>\n<p>If it&apos;s there, we can now start Python (specifically, the Python console).\nTo do that, just write <code>python</code>:</p>\n<div class=\"highlight\"><pre><code>(myenv)$ python\nPython 3.4.0 (default, Jan 26 2014, 18:15:05)\n[GCC 4.8.2 20131212 (Red Hat 4.8.2-7)] on linux\nType &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.\n&gt;&gt;&gt;</code></pre></div><p>This command will print some information.\nin the first line, it shows which version of Python you have \n(you should have Python 3).\nThe command line starts with three &quot;greater than&quot; signs.\nThis way Python prompts (asks) you for instructions.\nThis is the same as the standard command line, but instead of commands\nlike <code>cd</code> or <code>mkdir</code>, you write Python commands.</p>\n<p>The simplest command is a number. Try it:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"mi\">1</span>\n<span class=\"go\">1</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"mi\">42</span>\n<span class=\"go\">42</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"o\">-</span><span class=\"mf\">8.3</span>    <span class=\"c1\"># (Python uses period as decimal point)</span>\n<span class=\"go\">-8.3</span>\n</pre></div><div class=\"admonition note\"><p>Python prints the greater-than signs <code>&gt;&gt;&gt;</code> and the answer by itself!\nYou just write number and press Enter.</p>\n</div><p>Python can also add up numbers, like this:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"mi\">8</span> <span class=\"o\">+</span> <span class=\"mi\">2</span>\n<span class=\"go\">10</span>\n</pre></div><p>Notice that commands from the standard command line do not work here,\nalthough the window looks the same:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">whoami</span>\n<span class=\"gt\">Traceback (most recent call last):</span>\n  File <span class=\"nb\">&quot;&lt;stdin&gt;&quot;</span>, line <span class=\"m\">1</span>, in <span class=\"n\">&lt;module&gt;</span>\n<span class=\"gr\">NameError</span>: <span class=\"n\">name &apos;whoami&apos; is not defined</span>\n</pre></div><p>This is an <em>error message</em> which appears every time when you\ndo anything wrong.\nYou will see a lot of them during the course,\nso look at it carefully so you recognise it later.</p>\n<p>If you got this far, congratulations!\nYou have installed Python and it works.\nYou can now quit the Python console and close the command prompt.\nTo quit, just type <code>quit()</code> with empty parentheses.</p>\n<div class=\"highlight\"><pre><span class=\"gp\">&gt;&gt;&gt;</span> quit()\n<span class=\"gp\">(myenv)$</span>\n</pre></div><p>The greater-than signs <code>&gt;&gt;&gt;</code> change back to <code>(myenv)</code> \nfollowed by <code>$</code> or <code>&gt;</code>.\nNow commands like <code>whoami</code> and <code>cd</code> work again, but Python\ncommands like <code>1 + 2</code> don&apos;t wotk, until you enter the Python console<br>\nwith <code>python</code> again.</p>\n<p>To quit the virtual environment, type <code>deactivate</code> or <code>source deactivate</code> \n(for Linux and Mac) -- this time without parentheses.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"gp\">(myenv)$ </span>deactivate\n</pre></div><p>The command prompt (terminal) can be closed by typing <code>exit</code>.</p>\n<div class=\"highlight\"><pre><span></span><span class=\"gp\">$ </span><span class=\"nb\">exit</span>\n</pre></div><p>As an exercise, try to run the Python console again -- open the command prompt \n(the terminal), then activate the virtual environment, and finally run Python.</p>\n\n\n        "
    }
  }
}