Subsections

Intro

First things first

So, you've never programmed before. As we go through this tutorial I will attempt to teach you how to program. There really is only one way to learn to program. You must read code and write code. I'm going to show you lots of code. You should type in code that I show you to see what happens. Play around with it and make changes. The worst that can happen is that it won't work. When I type in code it will be formatted like this:


##Python is easy to learn
print("Hello, World!")

That's so it is easy to distinguish from the other text. To make it confusing I will also print what the computer outputs in that same font.

The next step is to get a computer with Python 3 on it.

Installing Python

In order to program in Python you need the Python software. If you are on a Linux or BSD computer, you probably already have Python 3 installed (check your package manager for an installation package if you don't, or you can compile Python 3 from source code. IDLE may be in a separate package). If you are on a Windows or macOS computer you will need to install Python 3. To install Python, download the appropriate file for your computer from http://www.python.org/download. You will need Python 3.3 or newer for this tutorial.

Interactive Mode

From the command line, interactive mode can be gotten to by typing python3 Alternatively, you can go into IDLE (also called the Python GUI). You should see a window that has some text like this:
Python 3.6.6 (default, Jul 19 2018, 14:25:17)
[GCC 8.1.1 20180712 (Red Hat 8.1.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
The >>> is Python's way of telling you that you are in interactive mode. In interactive mode what you type is immediately run. Try typing 1+1 in. Python will respond with 2. Interactive mode allows you to test out and see what Python will do. If you ever feel you need to play with new Python statements go into interactive mode and try them out.


Creating and Running Programs

Go into IDLE if you are not already. Go to File then New Window. In this window type the following:


print("Hello, World!")

First save the program. Go to File then Save. Save it as hello.py. (If you want, you can save it to some other directory than the default.) Now that it is saved it can be run.

Next run the program by going to Run then Run Module. This will output Hello, World! on the *Python Shell* window.

Confused still? Try this tutorial for IDLE at http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html

Using Python from the command line

If you don't want to use Python from the command line, you don't have to, just use IDLE. For those of you who don't want to use IDLE, to get into interactive mode just type python3 without any arguments. To run a program create it with a text editor (Emacs has a good python mode, Notepad++ also has a python mode) and then run it with python3 program_name.py.