Python comes with its IDLE, and you can use it in this tutorial. If you don't like using it for some reason, you need PowerShell, command prompt or terminal for trying out things. You also need an editor for writing code that will be stored in files.
If you use IDLE as your editor, it comes with everything set up for you, and you don't need to worry about setting up anything. If you don't, you probably need to change some settings to make your editor suitable for Python use.
Do not use word processors like Microsoft Word and LibreOffice Writer for programming. They create their own files, but you need plain text files for programming.
Start by creating an empty file called hello.py
and opening it with
your editor. Or just open your editor and save a file as hello.py
.
This is important. Never use tabs in Python. Nobody else is using tabs, and the official style guide tells you to never use tabs.
However, you don't need to press the spacebar four times every time you want to indent. Your editor should give you four spaces when you hit the tab key. Some editors also remove four spaces when you hit backspace and there are four spaces before the cursor.
- Go to Edit at the top and select Preferences.
- Go to Editor at left.
- Go to Indenting at top.
- Select Spaces instead of Tabs.
- Go to Edit at the top and select Preferences.
- Go to Editor at top.
- Change the indent width to 4 and select Add spaces instead of tabs.
Emacs uses spaces with Python files by default.
Open your ~/.nanorc
.
$ nano ~/.nanorc
Add these lines to it:
set tabsize 4
set tabstospaces
- Go to Document at the top, then Tab Size.
- Select 4.
- Also select Insert Spaces.
If you type a keyword, like if
, it should show up with a different
color than the rest of your text. "Strings"
, # comments
and
everything else should also have their own colors. This makes it much
easier to write code.
Most of the editors below have syntax highlighting turned on by default, but you can also change the colors.
Install more color schemes, then go to View, Change Color Scheme.
Click Fonts & Colors in the preferences and select another color theme.
Type M-x, type load-theme
, press Tab twice to see a list of theme
names, then enter a theme name and press Enter. If you want to
automatically set the theme when Emacs starts, add
(load-theme 'your-theme-name)
to your ~/.emacs
.
Click View, go to Color Scheme and select whatever you want.
Some editors allow you to run your programs with a single keystroke, usually by pressing F5. This tutorial is written for Python 3 or newer, so your editor also needs to run the programs in Python 3 or newer.
If you are unsure which Python your editor runs, create a test file with the following contents:
import sys
print(sys.version)
If the version starts with 2, it's too old.
-
Go to Build, then Set Build Commands.
-
Replace
python
orpython2
withpython3
everywhere. Or if you are using Windows, runpython
on a terminal and enter these commands:>>> import sys >>> print(sys.executable)
You'll get a path to your python.exe. Replace
python
in the build commands with this path. Most importantly, your Execute command should be"C:\your\path" "%f"
.
These editors don't support running programs with F5.
Usually I write something in Emacs, then I press Ctrl+Z to suspend
Emacs, run the program myself and then I run fg
to get back to Emacs.
If you know how to run Python programs in Emacs and you'd like to write
about it here, tell me.
You may use this tutorial freely at your own risk. See LICENSE.