A few years ago, people who wished to study programming as a hobby had an important decision to make: Which programming language would they study for their first?
Many people chose to start with BASIC or Pascal, because they were easier. Indeed, those languages are fairly well-suited to hobbyist programming, for people with simpler needs, who basically just want a programming language that won't overwhelm them. But many professional programmers denounce BASIC and Pascal as bad for learning with, because they teach programmers "bad habits" which will be hard to break later on.
Some people just started with a professional-level language like C or Fortran. In fact, there really is nothing wrong with learning C as your first language, it's just harder to get into it. Still other people opted for "instructional" languages which are specifically categorized for people studying programming concepts, like Scheme or ALGOL. Although these languages might make nice learning tools, they have one problem: They're considered useless for real-world programming.
And so the budding programmers were left with a dilemma: Wasn't there a resonably easy-to-learn programming language which wouldn't enforce bad programming technique and which was useful for general-purpose programming? Classically, the answer was no.
But the computer industry moves on, as all industries will, and we've come a long way from the days when most hobbyists were cranking out BASIC code on Commodore 64s or Apple ][ systems. There's a new programming language which aims to be easy-to-learn, elegant, powerful, and useful. It's called Python.
Python is interpreted, not compiled (much like Perl). The Python interpreter is free and can be downloaded from www.python.org.
So now that you know what Python is, where's the tutorial? Right here:
OK, the first thing you might want to know about Python is, how do you print to the screen with it? Well, Python uses good old standard print. So Python's "Hello, World" program looks just like BASIC's:
print "Hello, World."
Printing variables is similarly easy. Guess what the following program does:
a = 2 print `a`
(Note the use of grave accents around the variable name.) If you're using the Python shell, in which commands are entered manually, one at a time, like at a command prompt, you can even forgo the "print" and just type the variable name alone, or the output that you want to display. This may lead you to believe that Python automatically outputs the result of an operation without you needing to use the "print" command. However, this is true only when using the Python shell, not when using program scripts. The shell will behave this way, but actual saved Python program won't. Previously, this page incorrectly stated that a "command" containing the variable name alone would work correctly. I apologize for any inconvenience my misinformation may have caused; I guess that's what I get for having my only actual Python experience be with the shell. My sincere thanks also go to Grail for pointing this mistake out to me and saving me from further embarrassment. =)
Is there anything else you should know about Python? Probably, but I don't know what it is. Anyway, here's a program showing you how to make a socket in Python:
from socket import * HOST = 'www.whatever.com' # The remote host PORT = 80 # The same port as used by the server s = socket(AF_INET, SOCK_STREAM) s.connect(HOST, PORT) s.send('GET / HTTP/1.0\n\n') data = s.recv(1024) s.close() print 'Received', `data`
There you have it. If I figure something else out, I'll stick it on here later.