Nauč se Python > Materiály > Snake Workshop for PyLadies > Preparation > Editor installation > Setup

Editor installation #

If you are using an editor for which we do not have instructions, you will have to set it up yourself. Here are a few tips on what to watch out for.

Setup #

Line numbering #

Make sure that your editor numbers the lines. If not, check the settings and find out how to turn it on.

Syntax highlighting #

Save a file with the extension .py, for example test.py, and copy the following program into it:

def foo():
    return "abc" * 2

If the text is automatically colored (even with colors different from here), your editor is set up correctly. Otherwise, check the settings and find out how to turn it on.

Indentation #

By pressing the Tab key at the beginning of a line, 4 spaces are inserted. For writing and sharing code in Python, it is important that there are four spaces and that they are truly spaces.

If they are spaces, you can find out by selecting the indentation at the beginning with the mouse. If you can select individual spaces, everything is fine.

If it is not possible to select individual spaces or if pressing Tab inserts a different number than 4, check the settings for options such as 'indentation size' or 'replace tabs with spaces'.

Checking the style of source code #

Editors often support the installation of plugins that can make coding easier and help with its control. One of the most useful is a plugin for checking the correct style of source code. Python has typographic rules. For example, a space is written after a comma, but not before it. They are optional, the program will work even if they are not followed, but they help write clear code, so it is good to follow them from the beginning. These rules are described in the document PEP8.

Try to find and install such a plugin for your editor.

Indentation practice #

As already mentioned, in Python it is important how many spaces a line starts with. Therefore, it will be useful for us to know how to quickly indent blocks of text. Let's see how to do it.

Copy this text into the editor:

OPHELIA:
Good my lord,
How does your honour for this many a day?
HAMLET:
I humbly thank you; well, well, well.
OPHELIA:
My lord, I have remembrances of yours
That I have longed long to re-deliver.
I pray you, now receive them.
HAMLET:
No, not I.
I never gave you aught.
OPHELIA:
My honour’d lord, you know right well you did,
And with them words of so sweet breath compos’d
As made the things more rich; their perfume lost,
Take these again; for to the noble mind
Rich gifts wax poor when givers prove unkind.
There, my lord.

(An excerpt from Hamlet by W. Shakespeare)

This text is not very clear, so we will try to space it out to make it look like this:

OPHELIA:
    Good my lord,
    How does your honour for this many a day?
HAMLET:
    I humbly thank you; well, well, well.
OPHELIA:
    My lord, I have remembrances of yours
    That I have longed long to re-deliver.
    I pray you, now receive them.
HAMLET:
    No, not I.
    I never gave you aught.
OPHELIA:
    My honour’d lord, you know right well you did,
    And with them words of so sweet breath compos’d
    As made the things more rich; their perfume lost,
    Take these again; for to the noble mind
    Rich gifts wax poor when givers prove unkind.
    There, my lord.

To indent one line, set the cursor at the beginning of the line and press the Tab key. Each time you press it, the line will be indented by 4 spaces.

If you indent too much, you can reduce the indentation using Shift+Tab.

If you want to indent multiple lines at once, select them all and press Tab. You can also "unindent" the whole selection using Shift+Tab.

And that's it! Now you not only have the editor set up, but you also know how to use it.


Toto je stránka lekce z kurzu, který probíhá nebo proběhl naživo s instruktorem.