Friday, June 26, 2009

Python data

As with any programming language, handling data is a basic feature. Adding, subtracting, copying, counting, saving, reading and other means of changing data are basic features of a programming language.

Python data can be numeric (e.g., 1, 1000, 21.1, 3.4); it can be boolean (True, False); it can be string (or text, e.g., "Andrew") as well as a few other esoteric ones. We'll only be using simple data types.

You use variable names as aliases for the data you want to store. For example, the following assignment:


>>> age = 40


tells Python to take the number 40 and store it in the variable "age."

Assignment is a basic operation. Here are the others:




OperationNotes
+Addition
-Subtraction
/Division
//Floor division
*Multiplication
**Exponentiation


Python can also be used to evaluate expressions, like a calculator. You can get very fancy with calculations.


Python Math


In the screenshot above, you see some of the calculations you can do in Python. We've added a couple of numbers, done some division, and using parentheses, mainly to enforce order, we've gotten a bit fancy.




You should use parentheses as much as possible. Even though you know the order of operations, division will be done before addition, you should use parentheses to make it obvious. I would have done the following for the first division above.


Same answer, but the parentheses in the example above make it very obvious as to the order of operations.


Integer Division


Check out the following:




What's going on here?


The first one, 10/2 looks OK, but the second one, 10/3 is wrong. This is because Python has performed integer division. Python looks at the type of data that you're using and decides what type of operation it's going to use. In the case of 10/3, both the number 10 and the number 3 are integers, whole numbers. So, Python performs integer division and the answer is an integer. In short, the decimal part is truncated, or stripped off.




See the answer above. 20/3 is actually 6.666... If rounded, this is closer to 7. However, in Python, integer division results in truncation.


The rules that determine if integer division is going to be done, or floating point (with a decimal) division will be performed are quite simple. If any one of the numbers is a floating point number, a Python float, then the result will be floating pint division. In fact, what Python does is coerce the other numbers to floating point and then perform the division.


One way to force a number to become a floating point is to add a decimal point to it. For example, 20 can become 20.0. Mathematically, it's the same number. Another way to do this is to use Python's type-casting mechanism.


In the example above, we use the type-casting mechanism float(20) to force the number 20 to a floating point number. Now Python performs floating point division and we get the right answer. In the second example, we force the number 3 to become a floating point number.


Logical operations


This is an operation that results in a True or False answer. The words "True" and "False" are reserved keywords in the Python language and can be assigned to variable, much like you can assign a number or a string to a variable.




In the example above, we assign the number 1, to the variable "a." We assign the number 2, to the variable "b." Then we ask Python the question.


a > b ?


Python replies,


False


We can even assign the answer that Python gives us to a variable, as we do above.


c = a > b


Note that when we ask Python what "c" is, by typing it alone on its own line, Python replies with the answer:


False.


Strings are special in Python. A lot of computer programming is centred on the manipulation of text and some programming languages, notably Perl, are very good at handling text data. Python also makes it easy to manipulate strings, lines of text, and we'll devote an entire section on that.

Getting started with Python

Reader, this blog was started on June 26, 2009. Hopefully becomes a place to gather Python knowledge. To hone Python skills. And to share, anything Python.

I'm not a programmer by trade, but I do like writing code. Writing code, like writing a book or writing an article, satisfies the minds need for exercise. Solving a problem and getting to that "aha!" moment. That "eureka!" moment. I've read a dozen books on programming. My favourite book was "The C Programming Language" by Brian Kernighan and Dennis Ritchie. I remember well the phrase they used regarding the presentation style of the book; "C is not a big language, and it is not well served by a big book." In under three hundred pages they were able to completely describe all the features of one of the most popular system programming languages.

My hope is to try and capture a little of that C Programming Language style. Short and concise, yet complete enough with enough examples. A book that I think comes close to Kernighan and Ritchie's text but for Python is "Think Python: How to think like a Computer Scientist" by Allen B. Downey. Well written and absolutely easy to follow.

There are pros and cons when you read programming books. In many cases they describe the programming language but don't show you how to organise your thoughts and how to use it. In some cases, you won't get to the real meat until a few hundred pages later. A lot of time is spent on just describing the elements, or features, of the language and showing you trivial snippets of code. However, learning by doing is the best way to become proficient. It is said that if you want to gauge your knowledge about a subject, try to teach it to someone else. Well, here's my chance.

Like I said, I'm treating this as a conversation.



I'm sure the structure of this blog will change. There may be topics out of alignment since it's not supposed to be logically structured tutorial. Rather it is a knowledge base. My other hope is that it collects information about how things, real work, gets done in Python.

If you haven't written any computer programs you should be OK as long as you know how to use a text editor and have your Python environment set up. My operating system is Linux, but Python exists everywhere. On Linux, Windows and Macs. Hopefully all the code here is portable, but then again, who knows.



Here's the basic outline.

  • An explanation of Python data types. Get these out of the way.

  • Basic operations and program flow/control

  • Lists, tuples and dictionaries.

  • Functions

  • Strings (because we love them)