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.

No comments:

Post a Comment