works similar to a for-each loop found in other languages. To do a simple counting loop the
range() function is needed. The programmer would define a range for the for-loop to count
through as each step is completed. Below is an example of a simple counting loop starting a 0
and going to 4.
For count in range(5):
This block of code produces [0,1,2,3,4]. A range can also be set to begin and end at certain
integers such as range(2,6) which would start the count at 2 and go to 5 because the range
function never reaches the last integer for a range.
Subprograms in Python are declared with the def header followed by the functions name
and then any parameters for the function. Every function in Python returns a value, even ones
that are not set to return a value. In those cases the value “none” is returned. This is usually
ignored by the interpreter but can be seen through a print statement.
One of the features that sets Python apart from most of the other common languages is
that subprograms are executable. Whenever a def statement is executed, the given name is
assigned to the given function body. Until the def statement is executed, the function cannot be
called. For example, you can have two different versions of the function fun nested in an if –
statement. Each one can be set up with completely different parameters and functionality. Based
upon which clause of the if-statement is executed, determines which version of the fun function
is used and which one can now be called. Python also allows the use of nested subprograms. This
means that a subprogram can have its own subprogram contained within it. The benefit from this
is that it provides a highly structured way of granting access to nonlocal variables in the enclosed
As with almost every other language Python uses positions to correspond between formal
parameters and actual parameters. In addition to position parameters Python allows keyword
parameters. With keyword parameters the order doesn’t matter and each of the parameters
passed are assigned to each of the formal parameters of the function. The benefit of keyword
parameters is when a programmer needs to use a function with a long parameter list. This insures
that a mistake with the position cannot occur since each of the parameters is directly assigned.
Python also allows default values in formal parameters. In the event that a value is not passed
into a parameter that has a default value, the function is executed with that default value.
Python has a fairly unique way of passing parameters. Unlike in most other languages,
Python doesn’t do type checking on the parameters being passed. This is because everything in
Python is an object. While objects have types, variables do not. The method Python uses is called
pass-by-assignment. Since all of the data values in Python are objects, every variable is a
reference to an object. The actual parameter value is assigned to the formal parameter. Pass-by-
assignment is basically the same as pass-by-reference because all of the actual parameters are
Abstract Data Types and Encapsulation Constructs
Python doesn’t support encapsulation. Although you can declare a semi private function by using an underscore before the function name. However this isn’t a private function in a traditional sense and doesn’t stop the function from being accessed directly from a different class. It only prevents other classes from accessing it indirectly.
Python is a primarily an object-oriented language despite the lack of encapsulation. Everything in Python is considered an object. This includes every class, function, and variable. One of Python’s key object-orientation features is multiple inheritance. This allows a class to inherit traits from multiple different classes. Another one of Pythons object-orientated features is the way Python handles polymorphism. Instead of using traditional polymorphism, Python uses what is called duck typing. The operator “is” tests an objects identity in order to figure out what kind of object it is. Pythons method changes based on the type of object being given to it.
Since Python is an interpreted language the only type of concurrency that can be used is logical concurrency. This means that a python program cannot directly take advantage of a machine that has multiple processors. However, Python does allow a programmer to create threads in Python. These threads can carry out tasks independently, adding concurrency to Python programs.
Exception Handling and Event Handling
Just like what is found in many languages, Python also supports exception handling. Instead of a Try..Catch block, in Python it is a Try..Except block but it works exactly the same. In addition to the standard catch block Python provides additional tools for working with exceptions. The raise statement allows a programmer to create an exception object in their program that will either re-raise the last exception within the current scope or if no exception is found in the current scope a runtime error is raised. This function is helpful for troubleshooting problems within sections of code. Python also allows users to create their own exceptions through class objects. However it is recommended that these new exceptions be derived from Pythons built-in exception class.
Python really doesn’t have built in event handling options other than the raw_input function. However Python does have a great deal of modules available for event handling. One of these modules is called tKinter. This is a GUI module that also provides event handlers for a Python programs GUI interface. It also seems to be the most popular.
Other Python Features
Python has two modes, programmer mode and interactive mode. In programmer mode the finished .py files are run as normal in the Python interpreter. Interactive mode is a command line shell that gives immediate feedback for each statement as it goes through memory. In this mode any code you type is immediately run. This mode is useful for troubleshooting issues with a program.
Evaluation of Python
Readability is probably Pythons strongest feature. Python is well known as an easy to understand language. This is why many teachers use it as a introduction language. Pythons greatest strength is in its simplicity. Python forces a programmer to use indentions for their control structures and the lack of a goto function ensures the code will be easier to read.
Python excels at writability as well, although this is diminished by the lack of built in event handlers. Due to the lack of built in event handlers, a programmer will either have to learn an existing module or create his own. Overall Python is easy to write with its simple syntax. Python makes it easy for programs to add their own classes or packages to Python. Python can also use C or C++ packages which makes it easier for programmers to implement code they find in those languages.
While Python has many features making it reliable, I found the language lacking in this aspect. Python doesn’t do type checking when passing parameters, doesn’t support encapsulation, and uses aliasing. While the readability/writability helps insure there are fewer errors in the code, the lack of certain features lowers it reliability.
One of Pythons key features is portability which makes it very cost effective. Python is open source and easy to learn. C or C++ programmers will already be able to make packages for Python. However the cost is increased with the lack of encapsulation or native event handlers increases cost. Python is a scripting language, which means the memory manager must always keep track of the type for each of the variables which slows the code down and makes it not as fast as C code. This is why many programmers use C packages for certain sections of their program to speed up performance. Overall Python is very flexible and easy to work with but without using C code packages, wouldn’t be the best language to use for real-time results or heavy processing.
Beazley, David. "An Introduction to Python Concurrency." An Introduction to Python Concurrency. USENIX Technical Conference, June 2009. Web. 20 Nov. 2012. .
"Learnpython.org." Learn Python. Learnpython.org, n.d. Web. 01 Oct. 2012. .
Lott, Steven. "Python - Raising Exceptions." Python - Raising Exceptions. Linuxtopia.org, 2002. Web. 20 Nov. 2012. .
"Python V3.3.0 Documentation." Overview. Python Software Foundation, n.d. Web. 01 Oct. 2012. .
Sebesta, Robert W. Concepts of Programming Languages. Boston: Pearson, 2012. Print.
Shaw, Zed A. "Learn Python The Hard Way, 2nd Edition." Learn Python The Hard Way, 2nd Edition â Learn Python The Hard Way, 2nd Edition. N.p., n.d. Web. 01 Oct. 2012. .
Swaroop, CH. A Byte of Python. Vol. 1.9. N.p.: n.p., 2008. Print.
Vrajitoru, Dana. "C311 Name, Scope, Binding." C311 Name, Scope, Binding. IUSB, n.d. Web. 01 Oct. 2012. .