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.
Now, on to more important things. In order to program in Python you need the Python software. If you don't already have the Python software go to http://www.python.org/download/ and get the proper version for your platform. Download it, read the instructions and get it installed.
python
or python3
depending on the version of Python that you have. Alternatively, you can go into IDLE (also called the Python GUI). You should see a window that has some text like this:
Python 2.6.6 (r266:84292, Dec 27 2010, 21:57:32) [GCC 4.4.5 20100902 (prerelease)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>The
>>>
is Python 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.
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
If you don't want to use Python from the command line, you don't have
too, just use IDLE. For those of you who don't want to use IDLE, to
get into interactive mode just type python
or python3
with out 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 python program_name.py
or
python3 program_name.py
.