I’m Learning Python part 8

I’m Learning Python part 8

Python

Python 3.0

Since the last time I wrote an article Python many changes happened in my life, one of them is the release of Python 3.0 (Which is known also as Python3K and Python3000).

Python 3.0 is the first intentionally backwards incompatible Python release, which means that there are some changes you must notice before start coding in Python 3.0.

Don’t worry that much, the language has become more useful, and also there is 2to3 source-to-source conversion tool which converts your Python 2.x code to meet the requirements of Python 3.0 🙂

To find more about change in Python please refer to Python website.


P.S.

From now on all the code will be written in Python 3.0.

Decimal vs. float

Please try the following code in your python console:

You’d get this output:

0.10000000000000001

0.1
Strange ain’t it?
The second value is the one you put in the variable.
The first one is the real value represented in memory. This is not Python’s fault, this happens because of the way computer store float values in memory as 0’s and 1’s, computers use floating point IEEE-745 representation.

Using this representation unfortunately we can’t represent any float value using 0’s and 1’s, and 0.1 is one of those poor values (The reason is that it can’t be represented in sum of powers of 0.5).

So what all programming languages do is that they ignore the small fraction so it would be 0.1 instead of 0.10000000000000001, but Python doesn’t.

Try this code in Java if you have:

You’d get this output:

0.1

0.1

So Java doesn’t differ those two values while they’re different. (Such a difference could make a disaster in a nuclear reactor :twisted:)
If you want to go with the flow and ignore this difference in your calculations you’d have to use another data type than float, you’ll have to use Decimal.

0.1

Decimal data type allows you to represent any decimal value you want 🙂

It also supports addition, subtraction, multiplication, division and modulo, but the two sides must be Decimals.

Functions

Let’s write a function that prints Fibonacci series to a given boundary:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

Now let’s describe what we have done:

  1. def is a keyword in Python, def defines 🙂 it defines methods and classes.
  2. fib is the method’s name, method’s name must meet the same requirements for variable’s name. (first character a-z, A-Z or _, other characters a-z, A-Z, 0-9, _)
  3. (n) is the list of parameters, notice that no type names in this list. parameters are comma separated.
  4. : defines the scope of the method. All statements under this scope must be tab-aligned.
  5. “””Prints …””” is an optional string literal that describes what this method does, it will help you and other developers understand the purpose of the method, and it will also help you auto-generate documentation for your code.
  6. a, b = 0, 1. define two variables and give them two values. a handy way 🙂
  7. a, b = b, a + b. also assign two values to two variables.

The previous code defines a procedure (not a function) because it doesn’t return a value, let’s modify the code so it builds an array of Fibonacci values instead of printing them:

[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

What we have added is the return statement only, which returns a value to the caller.

One more thing to be told is default arguments:

Default arguments are assigned a default value if they aren’t when called.
You can set arguments values when calling by writing Name = Value.
Default parameters can’t be followed by non-default arguments.
Important note:
Default arguments are evaluated only once, so the following code will accumulate the values:

[1]

[1, 2]

[1, 2, 3]
If you don’t want the function to behave like this you can write:

Let’s explain what I meant by saying “are evaluated only once”:
When the Python interpreter reaches the line that defines the method, it allocates a list in the memory and assigns its address to L the reference.
Next time you call the function L will reference the same list it referenced the time it was created in memory, so it will always reference the same object.
The second code actually referenced to None, so every time you call the function it will assign None to L.
By understanding what I meant you can consider the following example:

12
That’s because b was given the value of i before the interpreter reached the line in which i was give the value of 11.

Bottom Line

Not that much I presented in this part, but you should consider upgrading your Python knowledge to Python 3.0 for the next time.
And by the way, I’m sorry, I’ve betrayed you and learned a lot during the last month, I have also started creating GUI Applications using Python and Qt, so please forgive me 🙂
Cheers.

2 thoughts on “I’m Learning Python part 8

  1. hello
    yesterday i discovered this blog
    since now i’ll read your blog every day

    now i’m reading the lesson 4

    since two years ago i had been programing in Delphi (Object Pascal), but now i’m interested to learn Python, and tyour blog is perfect for that

    thanks

Leave a Reply