Archive for the ‘Python’ Category.

Find max or min in Python dictionary

Suppose that mydict is a dictionary defined by

1
mydict = {'a': 2, 'c': 5, 'b': 1, 'd': 4}

How to find the key for the max or min value in the items?

Continue reading ‘Find max or min in Python dictionary’ »


Sort Python dictionary

Suppose that mydict is a dictionary defined by

1
mydict = {'a': 2, 'c': 5, 'b': 1, 'd': 4}

Remember that dictionary has no function sort, since it is unordered, not like a list, or tuple.

However, the key function, introduced in version 2.4, is helpful in sorting a dictionary.

Continue reading ‘Sort Python dictionary’ »


Evaluate Ankermann Function using Python

The Ankermann function, A(m, n) is defined as

Continue reading ‘Evaluate Ankermann Function using Python’ »


Even-valued Fibonacci terms summation

Problem #2 of Project Euler

Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.

Analysis
The first 10 terms of Fibonacci sequence are
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

We need to add the even numbers terms
2 + 8 + 34, …

Continue reading ‘Even-valued Fibonacci terms summation’ »


Generate Fibonacci numbers with Python

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

Mathematically, we write it as:
F(n) = F(n) + F(n-1)

This can easily be done in Python, using generators.

Continue reading ‘Generate Fibonacci numbers with Python’ »


Komodo Edit save and run python macro

Komodo Edit is a good editor for many programming language. However, since it is free as compared to its brother Komodo IDE, there is lack of debugging function.

Continue reading ‘Komodo Edit save and run python macro’ »


Executing Python Code Within Komodo Edit

Komodo Edit is a good free editor for Python development. However, there is no debugging feature in Komodo Edit. Therefore, it is a cumbersome process to execute Python scripts. Fortunately, there is a feature called Run command to satisfy this job.

To make use of the Run command feature to execute Python code, you can follow these steps:

  1. click Toolbox > Add > New Command.
  2. type “Run Python file” in the top field.
  3. enter
    %(python) “%F”
    in the command field.
  4. Click on the “Key Binding” tab and you can set the keyboard shortcut key (optional). I personally like the F5 key.
  5. Click OK.

komodo

Now, you can easily execute the Python script by pressing “F5″, or by double clicking the “Run Python file” in your toolbox pane, which is usually located at the right. The output will be displayed in the Command output pane at the bottom.

For more information on the “Run Command”:

http://aspn.activestate.com/ASPN/docs/Komodo/4.4/run.html


Add all the natural numbers below 1000 that are multiples of 3 or 5

Problem #1 of Project Euler

Add all the natural numbers below 1000 that are multiples of 3 or 5.

Continue reading ‘Add all the natural numbers below 1000 that are multiples of 3 or 5’ »


Python: Parse Apache log to sqlite database

This Python script was written for a friend in Australia, for part of his Ph.D project. The script will parse the Apache server log into sqlite3 database.

Continue reading ‘Python: Parse Apache log to sqlite database’ »


Python: Conditional Statement with lambda

Suppose that we have a conditional statement:

In Python, we can write it in three ways:

Continue reading ‘Python: Conditional Statement with lambda’ »