Programming in Python

Programming - Other
Typography
  • 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.

Numbers

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.

Strings

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:

20210407HelgrenPython1

The output would be:

  8

 PETE HELGREN

 pete helgren

 Pete Helgren

 

Lists

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

20210407HelgrenPython2

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:

20210407HelgrenPython2.1

The output would be:

 JavaScript

 ['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']

 

Tuples

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:

20210407HelgrenPython2
 

would be the same as this:

20210407HelgrenPython3 

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

in methods:

Will this work?

20210407HelgrenPython4 

Nope!

20210407HelgrenPython5 

Maybe a slightly different assignment?

20210407HelgrenPython6 

Nope! It’s a tuple by default:

20210407HelgrenPython7 

There are plenty of nice functions available to us.

20210407HelgrenPython8 

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.

Dictionary

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:

20210407HelgrenPython9 

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”.

 

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$