GIDNetwork > Beginning Python Tutorial (Part 1)
« RSS feed implemented at CMRPG GIDNetwork™ and the Blogs »

Beginning Python Tutorial (Part 1)

by: crystalattice - Jul 27, 2005

Since I'm starting to learn Python programming for my Colonial Marines RPG, I thought I might as well make a tutorial for others while I'm at it. Since I'm a beginner Python programmer, I figured I'd be in a good place to show others how to use the language, my thoughts on it, and what mistakes I've made and learned from.

I have programmed before (C, C++, Java) for college but nothing really serious. I didn't really like the low-level work involved with C/C++ and Java, as my first programming class in school, didn't make any sense. Hopefully Python won't be as bad.

Python is regarded as being a great hobbyist language yet extremely powerful. It has bindings for C/C++ and Java so it can be used to tie large projects together or for rapid prototyping. It has a built-in GUI binding via Tkinter but other GUI builders are available. It can be used in a real-time interpreter for code testing then built into a normal "executable".

Even though there are several great tutorials at the Python web site in addition to many books, my emphasis will be on the practical features of the language, i.e. I won't go into the history of the language or the esoteric ways it can be used. I will assume you either have some experience programming or can at least follow the code flow. You can download Python from the web site or it may already be installed on your system if you're using a Mac, Linux, or *BSD. I'll also assume you can figure out how to get the interactive interpreter running; if you need help, read the help pages on the web site.

Launching and Creating Python programs

Python can be programmed via the interactive command line (aka the interpreter or IDE) but anything you code won't be saved. Once you close the session it all goes away. To save your program, it's easiest to just type it in a text file and save it in the format.

To use the interpreter, type python at the command prompt (*nix and Mac) or launch the Python IDE (Windows and Mac). Here are two versions of what you should see (the first is an example from the bash shell, the second example is from the MacPython IDE):

Generic Code Example:

cpe-70-95-108-53:~ codyjackson$ python
Python 2.3 (#1, Sep 13 2003, 00:49:11) 
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

Generic Code Example:

Python 2.3 (#1, Sep 13 2003, 00:49:11) 
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)]
Type "copyright", "credits" or "license" for more information.
MacPython IDE 1.0.1

The >>> is the Python command prompt; your code is typed here and the result is printed on the following line, without a prompt. For example:

Generic Code Example:

>>>print "Hello World!"
Hello World!
>>>spam = "yum"

By the way, Python was named after Monty Python, not the snake. Hence, much of the code you'll find on the 'net, tutorials, and books will have references to Monty Python sketches.

If you want to run a Python program, simply type python at the shell command prompt (not the IDE) followed by the program name.

Generic Code Example:


Files saved with the .py extension are called modules and can be called individually at the command line or within a program like header files. More information on working with modules can be found in the Python documentation.

That's it for Part 1. I know it's not much but in the next installment I'll start talking about the types and operators available in Python.

Briefly, Python has the same implementations that C does, except that the largest types are used (such as double floats and long integers) and several new features are added to make programming easier, like lists and dictionaries.

Would you like to comment? This story has been viewed 41,475 times.
« RSS feed implemented at CMRPG GIDNetwork™ and the Blogs »


Copyright © GIDNetwork™ 2001 - 2024

Another website by J de Silva

Page generated in : 0.00684 sec.