Introduction
What is Python?
Python is, in short, a scripting language. It is similar in function to Perl, but to my knowledge is not nearly as popular. To fix that is part of the goal of this tutorial. It is a very high-level language that can perform complex tasks, but is surprisingly easy to learn. Many different add=ons (modules) are available to control everything from MP3s to windowing toolkits (Unix). I find Python to be a just plain fun language to program in, as it is very intuitive and suited to a variety of uses.
Python can be run on almost any platform, from 'nix to Windows. To get Python, first go to Python.org, and then download away!
This tutorial is geared towards people who have little experience with programming but know at least something. I make numerous references to languages like C and Perl, which are good to know, but you won't lose much if you just skip over them.
What's with the funky name, you ask? Some sort of carnivorous reptile? No, dear reader. Python's creators were (are) big Monty Python fans. The name comes from the BBC show "Monty Python's Flying Circus," and in the official docs, it says "Making references to Monty Python skits in documentation is not only allowed, it is encouraged." That said, I'm afraid I haven't seen all too many Monty Python movies so we'll go a little light on the references :)
The Interpreter
Python is a scripted, i.e. interpreted, language. Unlike C, which has a compiler, we have in Python the...(drum roll please)...Python interpreter. Once you have Python correctly installed, in your Unix shell or Dos box type 'python' (Note: anytime I tell you to type something and put it in quotes, don't type the quotes. I'm sure you already figured this out, but...). If you've used perl or some other interpreted language before, you might notice something: there's a prompt (those three little ">>>" things)! Not waiting for stdin or saying "Usage: python <script-file>"! That's right, Python has an interactive mode. In this mode, you can type any Python command and it will work just like you typed it from a script, with a few differences. Most importantly, if you type a variable of some sort or something that returns a value of some sort (except assignments), it will print the result automatically. Neat for trying stuff out (and learning!). Now type '1 + 1' at the prompt you should still be at. What do you see? 2! Now ain't that a powerful language:
>>> 1 + 1
2
You can do this with any kind of variable or math expression (see part 3b) and get some output. Works with strings too: >>> "elp! I'm being oppressed"
"elp! I'm being oppressed"
Note the bad Monty Python reference :). Anyway, that's all for the interpreter. Now we get into the meat of things. If at any point there's a sample script and you don't feel like creating a new file for it, feel free to type (or better yet copy & paste) it into the interpreter, it should work the same. Wait, scratch that :). If I put something in a script, it most likely means you only get the effect from a script and it won't work in interactive mode. Sorry.Your First Program
Hello, World!
Alright, if you've ever read a programming tutorial before you know they all have some sort of program that just prints "Hello, World!" or something on the screen. Unfortunately, this tutorial is no different. Alright, here goes:
#!/usr/bin/python
#You only need the above line for Unix; replace that with your path to python
hello = "Hello, World!"
print hello
That's it! Put that in a text file, name it hello.py, run it, and we're good. On Unix you may have to 'chmod +x hello.py' to run it, or just type 'python hello.py'. Windows, I'm pretty sure .py files are associated with the Python interpreter.Stay Connected....
0 comments:
Post a Comment