Programming in Python

Programming - Other
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Python pretty much follows the usual programming paradigms that we are all familiar with. It has all the variable assignment stuff you would expect, and it has five very basic data types.

Editor's Note: This article is excerpted from chapter 9 of Open Source Starter Guide for IBM i Developers, by Pete Helgren.


No big surprises here except for “complex” numbers that, frankly, I don’t understand. You math geeks may be encouraged by the presence of a complex number, but, well, I haven’t used them. But integers, longs, and floats I understand.


A string is, well, a string of characters enclosed by single or double quotes. As in other object-oriented languages, a string is an object and will have several useful methods already defined for it. Things like find, upper, and lower do what you think they do:


The output would be:



 pete helgren

 Pete Helgren



This is much like an array: just a list of items in what I would call a traditional array syntax. It looks like this:


OK, the last two entries weren’t programming languages, but what I wanted to demonstrate was that the values can be basically anything. And, since the list data type is an object, you can expect that there would be useful built-in functions. And there are! Like these:


The output would be:


 ['Python', 'Ruby', 'JavaScript', 42, 77.5]

 ['Python', 'Ruby', 'JavaScript', 'PHP', 42, 77.5]

 ['Python', 'Ruby', 'JavaScript', 'PHP', 77.5]

 [77.5, 'PHP', 'JavaScript', 'Ruby', 'Python']



Math geeks will already know what a “tuple” is, but for the benefit of those of us who live on planet Earth and will be writing Python code, a tuple is a series of immutable Python objects. Much like traditional arrays and lists in Python, tuples are sequences. The difference between tuples and lists is the tuples cannot be changed (immutable, remember?). Also unlike lists, tuples use parentheses instead of the square brackets. One other thing: if you build a list of items without putting parentheses or square brackets around them, the assumption is that they are a tuple. So this:


would be the same as this:


Let’s try some things on for size because tuples are also objects and will have some built-

in methods:

Will this work?




Maybe a slightly different assignment?


Nope! It’s a tuple by default:


There are plenty of nice functions available to us.


The last example is useful because it can be used with lists as well. We haven’t jumped into full programming in Python yet, but like many other languages, iteration is a foundational building block.


A dictionary is a sequence of name-value pairs. Similar to hashes in other languages, a dictionary can be used to “look up” one value with another. Anyone who has worked with JavaScript objects, particularly with JSON strings, would be comfortable with the concept of a dictionary. However, you can use both numbers and strings or really any valid data type for “name.” Any object can be used as a value. Take a look at the following:


The output would be:

 {42: 'This is the meaning of life', 'one': 'The loneliest number'}


print dict[42] would output “This is the meaning of life”, and if we accidentally used the same name to assign a value later, we would basically be reassigning the value:


 dict[42] = "This is NOT the meaning of life" print dict[42]


This would output “This is NOT the meaning of life”.