Starting Programming with Python

How to Create Scripts with the Python Programming Language

© Mark Alexander Bain

Aug 6, 2009
Starting Programming with Python, Mark Alexander Bain
Python is a simple yet effective scripting language, and it's one that the programmer can learn very quickly regardless of the platform that they're using.

Python has been a popular programming language for nearly 20 years. It's popular for scripting and for rapid application development, and that popularity is for a number of reasons:

  • it is cross platform, running as happily on a Linux computer as it does on a Windows computer
  • it is free and open source
  • it comes with its own IDE (Integrated Design Environment) known as IDLE (Python's Intergrated DeveLopment Environment)
  • it's a dynamic language
  • it is object oriented
  • it has no type declarations
  • it can act as a bridging language between the likes of C and Perl

But, most importantly, it is easy to read and easy to maintain, and so any programmer (regardless of any previous experience) can learn to use Python very quickly.

A Simple Python Script

The simplest Python script contains a single line of code, something like:

print ("Hello World")

This can be run interactively in IDLE (as shown in figure 1 at the bottom of this article), or it can be saved to a file and then run from the command line (as shown in figure 2). However, this will only work if the system environment PATH variable has been updated correctly.

Adding Some Process Control with Python

Python, just like most programming languages, has an if..then..else process control structure. However, it's handled in a slightly different way. Most programmers will be familiar with formats such as:

  • brackets, for example {}, to encapsulate any code
  • if ... end if
  • if ... fi

Python does not use any of those to denote nesting, instead is uses indentation. For example:

light = "Y"
if light == "Y":
___print ("It's day time")
else:
___print ("It's night time")

The result of running this code can be seen in figure 3, but before moving on it's important to stress that the indentation forms part of the code. If the indentation is remove then the code will run differently (if it actually runs at all). However, once the programmer remembers that fact then it's a simple task to start extending the script.

Creating and Running a Python Function

The Python programmer creates a function by using the def keyword:

def daycheck():

And again rather than using brackets, the indentation is used to define the nesting of the code within the body of the function:

___light = input ("Is it light outside?[Y/N]")
___if light.upper() == "Y":
______print ("It's day time")
___else:
______print ("It's night time")

This can then be called from the script:

daycheck()

And, as figure 4 shows, it is very easy to write and extend a Python script. Because of this any application is not limited due to the complexity of the programming language. In fact, the only limit is the time and the programming abilities of the programmer themselves.


The copyright of the article Starting Programming with Python in Computer Programming Languages is owned by Mark Alexander Bain. Permission to republish Starting Programming with Python in print or online must be granted by the author in writing.


Starting Programming with Python, Mark Alexander Bain
Figure 1: The Python IDLE GUI, Mark Alexander Bain
Figure 2: Running a Python Script, Mark Alexander Bain
Figure 3: Simple Python Control, Mark Alexander Bain
Figure 4: A Python Function, Mark Alexander Bain


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo