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.
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.
python3In Windows (assuming that Python is in the PATH), the command
pythonis used instead. 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+1in. 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.
New Window. In this window type the following:
First save the program. Go to
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 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
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
without any arguments (
python in Windows). 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 (If in Windows, use
python program_name.py in the cmd window from the correct directory).