Category: Python

Count the number of occurences using Counter() in Python

July 3rd, 2009 | No Comments

Counter is an object of collections module, and it is a dict subclass for counting hashable objects, in Python 3.

continue reading


How to list out Python reserved words

July 1st, 2009 | No Comments

To list out the Python reserved words, just enter the following lines into the Python interpreter.

>>> import keyword
>>> print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue',
'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global',
'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass',
'raise', 'return', 'try', 'while', 'with', 'yield']
Python Reserved Words

Python Reserved Words


Count the number of words using Python

June 1st, 2009 | No Comments

This article represents a way to count the number of words, the number of unique words, and the number of each word occurrences in a text file.

continue reading


How to choose a random item from a sequence in Python

May 31st, 2009 | No Comments

To choose a random item from a list, use random.choice().

Here is an example.

>>> items = ['Proton', 'Honda', 'Toyota', 'Nissan']
>>> random.choice(items)
'Nissan'
>>> random.choice(items)
'Proton'
>>> random.choice(items)
'Toyota'

Another example of usage is to choose a random file from a directory.

>>> random.choice(os.listdir("C:\\"))
'ubuntu'
>>> random.choice(os.listdir("C:\\"))
'tex4ht'

How to run Python inside vim

May 30th, 2009 | No Comments

To run the current open Python program in vim, type:

:!python %