GIDNetwork > Beginning Python Tutorial (Part 10)
« Fetching Unique Ads for a Contextual Advertising System Beginning Python Tutorial (Part 11) »

Beginning Python Tutorial (Part 10)

by: crystalattice - Mar 11, 2007

Welcome back

Well, it's been quite some time since my last Python tutorial. I got sidetracked by actually trying to make some programs using Python. Some worked well, some not so well. But I learned from my mistakes and have a better grasp of how to use Python.

After having used it for almost two years now, I've come to find that Python is an extremely capable language, equal in power to C++, Java, et al. If you don't need the "finesse" the major languages provide, I highly recommend learning Python or another dynamic language like Ruby. You'll program faster with fewer errors (like memory management) and can harness the power of a built-in GUI for rapid prototyping of applications. You can also use these languages for quick scripts to speed repetitive tasks. Plus, they are inherently cross-platform so you can easily switch between OSes or find a larger market for your programs. Heck, Python is used extensively by Google, NASA, and many game publishers, so it can't be that bad.

One of the biggest complaints people have is the forced use of white space and indentation. But if you think about it, that's considered a "good coding practice"; it makes it easier to follow the flow of the program and reduces the chance of errors. Plus, since brackets are required, you don't have to worry about your program not working because you forgot to close a nested if statement. After a few days of using Python, you won't even notice, though I imagine you'll notice how "sloppy" other languages look.

Now, on with the show...

Making Python Do Something

So far I've talked about how Python is structured and how it differs from other languages. Now it's time to make some real programs. (Of course, if you've perused GIDForums' Python section, you've seen several programs I wrote already; good or bad, they are good at showing how Python works).

Python programs are comprised of functions, classes, modules, and packages.

  1. Functions are programmer created code blocks that do a specific task.

  2. Classes are object-oriented structures that I'll talk about later; suffice to say they are pretty powerful structures that can make programming life easier, though they can be difficult to learn and wield well.

  3. Modules are generally considered normal program files, i.e. a file comprised of functions/classes, loops, control statements, etc.

  4. Packages are programs made up of many different modules.

In reality, I consider modules and packages to be "programs". It just depends on how many separate files are required to make the program run. Yes, it is possible to have a single, monolithic file that controls the entire program but it's usually better to have different parts in different files. It's actually easier to keep track of what's going on and you can cluster bits of code that have common goals, e.g. have a file that holds library functions, one that handles the GUI, and one that processes data entry.

An important module to know is the Python standard library. The library is a collection of common code blocks that you can call when needed. This means you don't have to "rebuild the wheel" every time you want to do something, such as calculate the tangent of a function. All you have to do is import the portion of the standard library you need, e.g. the math block, and then use it like regular Python code. Knowing what's in the standard library separates the good programmers from the great ones, at least in my book.

That being said, let's make a simple Python program.

Python Code Example:

def square(x):	#define the function
	return x * x	#pass back to caller the square of a number

for y in range(1, 11):	#cycle through a list of numbers
	print square(x)	#print the square of a number

This is about as simple as it gets. First we define the function called square and tell it that the argument called x will be used for processing. Then we actually define what the function will do; in this case, it will multiply x times itself to produce a square. By using the keyword return, the square value will be given back to whatever actually called the function (the print statement).

Next we create a for loop that prints the squared value of each number as it increases from 1 to 11. This should be fairly easy to follow, especially with the comments off to the side. Please realize that most programs you'll see aren't commented this much; quite often, the programs aren't commented at all. I like to think that I have a sufficient amount of documentation in my code (see GIDForums) that it's pretty easy for even new programmers to figure out what's going on.


What? you didn't know snakes got bad breath? (I know, bad joke.) Seriously though, scope describes the area of a program where an identifier (a name for something, like a variable) can access the it's associated value. Scope ties in with namespaces because namespaces pretty much define where an identifier's scope is.

In simple terms, namespaces store information about an identifier and it's value. Python has three namespaces: local, global, and built-in. When an identifier is first accessed, Python looks for it's value locally, i.e. it's surrounding code block. In the example above, x is defined within the function square. Every function is assigned its own local namespace. Functions can't use identifiers defined in other functions; they're simply not seen. If a function tries to call a variable defined in another function, you'll get an error. If a function tried to define a previously defined variable, you'll just get a brand new variable that happens to have the same name but a different value.

However, if an identifier isn't defined locally, Python will check if it's in the global namespace. The global namespace is different from the local one in that global identifiers can be used by other functions. So if you made global variable cars_in_shop = 2, all functions in the program can access that variable and use it as needed. So you can define a variable in one location and have it used in multiple places without having to make it over and over.

The built-in namespace is set aside for Python's built-in functions. (Kinda convenient, huh?) So keywords and standard function calls like range() are already defined when the Python interpreter starts up and you can use them "out of the box".

As you may have figured out, namespaces are nested:

Generic Code Example:


If an identifier isn't found locally, Python will check the global namespace. If it's not there Python will check the built-in namespace. If it still can't find it, it coughs up and error and dies.

One thing to consider (and I touched on slightly) is that you can hide identifiers as you go down the namespace tree. If you have cars_in_shop = 2 defined globally, you can make a function that has the exact same name with a different value, e.g. cars_in_shop = 15. When the function calls this variable, it will use the value of 15 vs. 2 to calculate the result. This can cause problems if you don't have good variable names since you may forget which variable you're actually using.

Default Arguments

When you create a function, you can set it up to use default values for it's arguments, just in case the item calling it doesn't have any arguments. For example:

Python Code Example:

def perimeter(length = 1, width = 1): 
	return length * width

If you want to call this particular function, you can supply it with the necessary measurements ( perimeter(15, 25) ) or you can supply one ( perimeter(7) ) or you can just use the defaults ( perimeter() ). Each argument is matched to the passed in values, in order, so if you're going to do this make sure you know which arguments are going to be matched.

You can also use keyword arguments, which match arguments based on a corresponding keyword. This way, you don't have to worry about the order they are given. See the examples in the comments thread.

That's about it for programming with functions. Their pretty simple and the more examples you see, the more they'll make sense. Python is cool since you can mix functions and classes (with methods) in the same module without worrying about errors. This way you aren't constrained to one way of programming; if a short function will work, you don't have to take the time to make a full-blown class with a method to do the same thing.

If you don't want to deal with object-oriented programming, you can stick with functions and have a good time. However, I'll start to cover OOP in later tutorials to show you why it's good to know and use. And with Python, it's not as scary as OOP implementation in other languages.

Thanks for reading.

Would you like to comment? This story has been viewed 18,771 times.
« Fetching Unique Ads for a Contextual Advertising System Beginning Python Tutorial (Part 11) »


Copyright © GIDNetwork™ 2001 - 2024

Another website by J de Silva

Page generated in : 0.00538 sec.