Python: An Introduction to Programming with ActivePython

Advertisement
Do you ever wonder how your web browser magically displays web pages? Or how Google miraculously returns millions of search results in a fraction of a second? It all boils down to efficient code.

Computer code. Code makes the Internet go round. Computers can only understand code, following every code instruction like a blueprint. Programmers write computer code (called source code) so that computers know what to do. The Windows 7 OS is comprised of millions of lines of source code. Some programs are only a few lines long.

There are a multitude of programming languages that you can use to write your own software with. All of them have pros and cons. One of the most popular programming languages for beginners to cut their teeth on is called Python. Python is great for novices because it forces you to write code in a very structured, logical way, as opposed to more liberal languages (Perl, PHP, C++, et al.) that give you more freedom at the expense of picking up bad programming habits. Let's take a dive into the world of Python.

ActivePython: Easy Coding

First off, you'll need to install the python interpreter and libraries (download here). ActivePython is an excellent version to start off with, as it contains everything you need for Windows. After installing, run the "Python Interactive Shell" from the Start menu. Most programming languages require you to write source code in a separate text file before the program or script can be executed, but Python allows you start coding immediately via the command line. Upon first inspection, the shell may remind you of a DOS command prompt of yesteryear.

Python command line

Once inside the shell, type in (print 'hello world') (without the parentheses) at the >>> python prompt and hit enter. Python will respond by displaying "hello world" on the shell. You've just written your first python program. Easy, right? Many programmers begin a new coding language by testing out a "hello world" program. All programs are full of holding places in memory called "variables". Inside variables are "values". A value can be a number, a string of text, or any type of object the language allows you to abstract. A programmer assigns a "value" to a "variable". At the prompt type this:

>>> 2 + 2

Now hit enter and python will display the number 4. Python did the addition for you. Now let's place the values into variables. Type this and hit enter:

>>> x,y = 2,2

Now type this followed by enter:

>>> x + y

You'll see the number 4 again. Let's go through what we did. First off, you assigned the variables x and y the value 2, which means that x and y hold the value 2 and you can refer to that value via the variable names. Next, we added x and y together, and since both variables contain the value 2, we get the number 4 when we add them.

Type the following and press enter:

>>> s = 'hello world'

Next, type:

>>> print s

See what we just did? We assigned the variable "s" a text value of "hello world", and on the next line we told python to print the value of "s", which is of course "hello world".

ActivePython is Great for Beginning Programmers

Python is a wonderful programming language that is perfect for beginners. Not only is it a great language to learn with, but it also excels at powering enterprise-level desktop applications and web services that talk to databases and interact with users. Put simply, Python is perfect for any computer programmer, from beginner to superstar software hacker.

Content: 
Tags: 
TOPlist