Non-Programmer's Tutorial for Python 3/Intro
- 1 First things first
- 2 Installing Python
- 3 Interactive Mode
- 4 Creating and Running Programs
- 5 Using Python from the command line
- 6 Where to get help
First things firstEdit
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 (as computer programs are often called). 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. If you're reading this on the Web, you'll notice the code is in color -- that's just to make it stand out, and to make the different parts of the code stand out from each other. The code you enter will probably not be colored, or the colors may be different, but it won't affect the code as long as you enter it the same way as it's printed here.
If the computer prints something out it will be formatted like this:
(Note that printed text goes to your screen, and does not involve paper. Before computers had screens, the output of computer programs would be printed on paper.)
Note that this is a Python 3 tutorial, which means that most of the examples will not work in Python 2.7 and before. As well, all but a small number of the extra Python 2.7 libraries (third-party libraries) have been converted to Python 3. Most likely you will want to learn Python 3, but if you are learning Python for a specific package or set of modules that are only written in Python 2.7, you may want to consider learning from the Non-Programmer's Tutorial for Python 2.6. However, the differences between Python 2 and 3 are not particularly large, so if you learn one, you should be able to read programs written for the other without much difficulty.
There will often be a mixture of the text you type (which is shown in bold) and the text the program prints to the screen, which would look like this:
Halt! Who Goes there? Josh You may pass, Josh
(Some of the tutorial has not been converted to this format. Since this is a wiki, you can convert it when you find it.)
I will also introduce you to the terminology of programming - for example, that programming is often referred to as coding or hacking. This will not only help you understand what programmers are talking about, but also help the learning process.
Now, on to more important things. In order to program in Python you need the Python 3 software. If you don't already have the Python software go to www.python.org/download and get the proper version for your platform. Download it, read the instructions and get it installed.
For Python programming you need a working Python installation and a text editor. Python comes with its own editor, IDLE, which is quite nice and totally sufficient for the beginning. As you get more into programming, you will probably switch to some other editor like nano, emacs, vi or another.
The Python download page is http://www.python.org/download. The most recent version is Python 3.7.3 (as of July 2019); Python 2.7 and older versions will not work with this tutorial. There are various different installation files for different computer platforms available on the download site. Here are some specific instructions for the most common operating systems:
Linux, BSD, and Unix usersEdit
You are probably lucky and Python is already installed on your machine. To test it type
python3 on a command line. If you see something like what is shown in the following section, you are set.
IDLE may need to be installed separately, from its own package such as idle3 or as part of python-tools.
If you have to install Python, first try to use the operating system's package manager or go to the repository where your packages are available and get Python 3. Python 3.0 was released in December 2008; all distributions should have Python 3 available, so you may not need to compile it from scratch. Ubuntu and Fedora do have Python 3 binary packages available, but they are not yet the default, so they need to be installed specially.
Roughly, here are the steps to compile Python from source code in Unix (If these totally don't make sense, you may want to read another introduction to *nix, such as Introduction to Linux):
- Download the .tgz file (use your Web browser to get the gzipped tar file from https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz)
- Uncompress the tar file (put in the correct path to where you downloaded it):
$ tar -xvzf ~/Download/Python-3.7.4.tgz ... list of files as they are uncompressed
- Change to the directory and tell the computer to compile and install the program
$ cd Python-3.7/ $ ./configure --prefix=$HOME/python3_install ... lots of output. Watch for error messages here ... $ make ... even more output. Hopefully no error messages ... $ make install
- Add Python 3 to your path. You can test it first by specifying the full path. You should add $HOME/python3_install/bin to your PATH bash variable.
$ ~/python3_install/bin/python3 Python 3.7.4 (... size and date information ...) [GCC 9.1.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
The above commands will install Python 3 to your home directory, which is probably what you want, but if you skip the --prefix=$HOME/python3_install, it will install it to /usr/local. If you want to use the IDLE graphical code editor, you need to make sure that the tk and tcl libraries, together with their development files, are installed on the system. You will get a warning during the make phase if these are not available.
Starting from Mac OS X Tiger (10.4), versions of Python 2 shipped with the operating system by default, but you will need to also install Python 3 unless Mac OS starts including Python 3 (check the version by starting
python3 in a command line terminal). Also IDLE (the Python editor) might be missing in the standard installation. If you want to (re-)install Python, get the Mac OS installer from the Python download site.
Download the appropriate Windows installer (the x86 MSI installer, if you do not have a 64-bit AMD or Intel chip). Start the installer by double-clicking it and follow the prompts.
See https://docs.python.org/3/using/windows.html#installing-python for more information.
Configuring your PATH environment variableEdit
The PATH environment variable is a list of folders, separated by semicolons, in which Windows will look for a program whenever you try to execute one by typing its name at a Command Prompt. You can see the current value of your PATH by typing this command at a Command Prompt:
The easiest way to permanently change environment variables is to bring up the built-in environment variable editor in Windows. How you get to this editor is slightly different on different versions of Windows.
On Windows 8 or Windows 10: Press the Windows key and type Control Panel to locate the Windows Control Panel. Once you've opened the Control Panel, select View by: Large Icons, then click on System. In the window that pops up, click the Advanced System Settings link, then click the Environment Variables... button.
On Windows 7 or Vista: Click the Start button in the lower-left corner of the screen, move your mouse over Computer, right-click, and select Properties from the pop-up menu. Click the Advanced System Settings link, then click the Environment Variables... button.
Once you've brought up the environment variable editor, you'll do the same thing regardless of which version of Windows you're running. Under System Variables in the bottom half of the editor, find a variable called PATH. If there is is one, select it and click Edit.... Assuming your Python root is C:\Python34, add these two folders to your path (and make sure you get the semicolons right; there should be a semicolon between each folder in the list):
Note: If you want to double-click and start your Python programs from a Windows folder and not have the console window disappear, you can add the following code to the bottom of each script:
#stops console from exiting end_prog = "" while end_prog != "q": end_prog = input("type q to quit")
Go into IDLE (also called the Python GUI). You should see a window that has some text like this:
Python 3.0 (r30:67503, Dec 29 2008, 21:31:07) [GCC 4.3.2 20081105 (Red Hat 4.3.2-7)] on linux2 Type "copyright", "credits" or "license()" for more information. **************************************************************** Personal firewall software may warn about the connection IDLE makes to its subprocess using this computer's internal loopback interface. This connection is not visible on any external interface and no data is sent to or received from the Internet. **************************************************************** IDLE 3.0 >>>
>>> 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
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 ProgramsEdit
Go into IDLE if you are not already. In the menu at the top, select File then New File. In the new window that appears, type the following:
Now save the program: select File from the menu, then Save. Save it as "
hello.py" (you can save it in any folder you want). Now that it is saved it can be run.
Next run the program by going to Run then Run Module (or if you have an older version of IDLE use Edit then Run script). This will output
Hello, World! on the *Python Shell* window.
For a more in-depth introduction to IDLE, a longer tutorial with screenshots can be found at http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html.
Program file namesEdit
It is very useful to stick to some rules regarding the file names of Python programs. Otherwise some things might go wrong unexpectedly. These don't matter as much for programs, but you can have weird problems if you don't follow them for module names (modules will be discussed later).
- Always save the program with the extension .py. Do not put another dot anywhere else in the file name.
- Only use standard characters for file names: letters, numbers, dash (-) and underscore (_).
- White space (" ") should not be used at all (use underscores instead).
- Do not use anything other than a letter (particularly no numbers!) at the beginning of a file name.
- Do not use "non-English" characters (such as å, ɓ, ç, ð, é, õ, ü) in your file names—or, even better, do not use them at all when programming.
- Do not use module names for file names (such as print.py, math.py, list.py)
Using Python from the command lineEdit
If you don't want to use Python from the command line, you don't have to, just 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) and then run it with
Additionally, to use Python within Vim, you may want to visit the Python wiki page about VIM.
Running Python Programs in *nixEdit
If you are using Unix (such as Linux, Mac OS, or BSD), if you make the program executable with chmod, and have as the first line:
you can run the python program with
./hello.py like any other command.
Where to get helpEdit
At some point in your Python career you will probably get stuck and have no clue about how to solve the problem you are supposed to work on. This tutorial only covers the basics of Python programming, but there is a lot of further information available.
First of all, Python is very well documented. There might even be copies of these documents on your computer that came with your Python installation:
- The official Python 3 Tutorial by Guido van Rossum is often a good starting point for general questions.
- For questions about standard modules (you will learn what these are later), the Python 3 Library Reference is the place to look.
- If you really want to get to know something about the details of the language, the Python 3 Reference Manual is comprehensive but quite complex for beginners.
Python user communityEdit
There are a lot of other Python users out there, and usually they are nice and willing to help you. This very active user community is organised mostly through mailing lists and a newsgroup:
- The tutor mailing list is for folks who want to ask questions regarding how to learn computer programming with the Python language.
- The python-help mailing list is python.org's help desk. You can ask a group of knowledgeable volunteers questions about all your Python problems.
- The Python newsgroup comp.lang.python (Google groups archive) is the place for general Python discussions, questions and the central meeting point of the community.
- Python wiki has a list of local user groups, you can join the group mailing list and ask questions. You can also participate in the user group meetings.
- LearnPython subreddit is another location where beginner level questions can be asked.
In order not to reinvent the wheel and discuss the same questions again and again, people will appreciate very much if you do a web search for a solution to your problem before contacting these lists!