I decided to pick up another programming language after Java and C#, mostly influenced by the post: what programming language to learn (next) and recommendations from various other people. It seems that if I want to learn another language, Python is the one to go.

I will use this post as my learning journal.

Day 1 (July 15, 2012) : Installation.

Day 2 (July 16, 2012): Basic Tutorial

  • From the beginner’s guide, picked and read the Instant Python Tutorial. Grasp the basic idea of Python.
  • Checked out Google’s python class
    • Finished reading the introduction class
      • Convention for the main file –> if __name__ == ‘__main__’ : main()
      • Both the interpreter (Python 2.7->Python (Command Line)) and command line commands “C:\Python27\Python hello.py Alice” works
      • Code is checked at Runtime, so mistake might not be caught if the running code bypass that branch
      • Online help with
        • Google python XXX
        • Type help(XXX) or dir(XXX) to get help

Day 3 (July 17, 2012)

  • Continue to check out  Google’s python class
    • Strings
      • Unlike Java “+” does not automatically convert other types to string. Need to use str() to convert them.
      • Int division: use “//” i.e., 6 // 5 is 1.
      • A “raw” string literal is prefixed by an ‘r’ and passes all the chars through without special treatment of backslashes, i.e., raw = r’this\t\n and that’
      • Python has a printf()-like facility to put together a string. The % operator takes a printf-type format string on the left (%d int, %s string, %f/%g floating point)
      • To create a unicode string, use the ‘u’ prefix on the string literal, i.e., ustring = u’A unicode \u018e string \xf1′
      • The boolean operators are the spelled out words *and*, *or*, *not* (Python does not use the C-style && || !)
      • omitting the “:” is my most common syntax mistake when typing if and else statements
        if speed >= 80: print 'You are so busted'
          else: print 'Have a nice day'
    • Downloaded google-python-exercises.zip.
      • Passed all tests in string1.py (need to figure out how to develop unit tests for python)
    • Lists
      • The beauty of python is in its simple loop constructs: the for/in construct and range() function are totally cool.
      • Passed all tests in list1.py

Day 4 (July 19, 2012)

  • Continue to check out Google’s python class
    • Sorting
      • the sorted(strs, key=funName) is really cool
      • To create a size=1 tuple, the lone element must be followed by a comma. i.e., tuple = (‘hi’,)   ## size-1 tuple
    • Dicts and files
      • “Strategy note: from a performance point of view, the dictionary is one of your greatest tools, and you should use where you can as an easy way to organize data. For example, you might read a log file where each line begins with an ip address, and store the data into a dict using the ip address as the key, and the list of lines where it appears as the value. Once you’ve read in the whole file, you can look up any ip address and instantly see its list of lines. The dictionary takes in scattered data and make it into something coherent.”
    • Finished exercise wordcount.py

Day 5 (July 21, 2012)

  • Unittest for Python
    • PyScripter has unit test built in. To invoke it,  go to menu (Tools –> Unit Test Wizard …), it will generate the tests for each method for a piece of code. Make sure you save the file in the same directory of the file you want to test before testing.
    • When perform the test, make sure the method of interest has the module name associated with it. For example, if the string1 module is imported, and you want to test the “donuts” method in the string1 module, make sure you use string1.donuts in the test cases. I try to use donuts directly without the string1 prefix and it fails the test.
    • Rewrite the tests in string1.py with unittest

Google’s code string1.py with tests written inside of the program.

#!/usr/bin/python -tt
# Copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0

# Google’s Python Class
# http://code.google.com/edu/languages/google-python-class/

# Basic string exercises
# Fill in the code for the functions below. main() is already set up
# to call the functions with a few different inputs,
# printing ‘OK’ when each function is correct.
# The starter code for each function includes a ‘return’
# which is just a placeholder for your code.
# It’s ok if you do not complete all the functions, and there
# are some additional functions to try in string2.py.

# A. donuts
# Given an int count of a number of donuts, return a string
# of the form ‘Number of donuts: <count>’, where <count> is the number
# passed in. However, if the count is 10 or more, then use the word ‘many’
# instead of the actual count.
# So donuts(5) returns ‘Number of donuts: 5’
# and donuts(23) returns ‘Number of donuts: many’
def donuts(count):
# +++your code here+++
s_prefix = ‘Number of donuts: ‘
if count < 10:
return s_prefix + str(count)
return s_prefix + "many"

# B. both_ends
# Given a string s, return a string made of the first 2
# and the last 2 chars of the original string,
# so ‘spring’ yields ‘spng’. However, if the string length
# is less than 2, return instead the empty string.
def both_ends(s):
# +++your code here+++
if len(s) < 2 :
return ”
return s[:2] + s[-2:]

# Provided simple test() function used in main() to print
# what each function returns vs. what it’s supposed to return.
def test(got, expected):
if got == expected:
prefix = ‘ OK ‘
prefix = ‘ X ‘
print ‘%s got: %s expected: %s’ % (prefix, repr(got), repr(expected))

# Provided main() calls the above functions with interesting inputs,
# using test() to check if each result is correct or not.
def main():
print ‘donuts’
# Each line calls donuts, compares its result to the expected for that call.
test(donuts(4), ‘Number of donuts: 4’)
test(donuts(9), ‘Number of donuts: 9’)
test(donuts(10), ‘Number of donuts: many’)
test(donuts(99), ‘Number of donuts: many’)

print ‘both_ends’
test(both_ends(‘spring’), ‘spng’)
test(both_ends(‘Hello’), ‘Helo’)
test(both_ends(‘a’), ”)
test(both_ends(‘xyz’), ‘xyyz’)

# Standard boilerplate to call the main() function.
if __name__ == ‘__main__’:

Take the testing part out and rewrite it in the unittest module

#This file was originally generated by PyScripter’s unitest wizard

#Make sure you save this file first in the same directory with the original string1.py

#This file was originally generated by PyScripter’s unitest wizard

import unittest
import string1

class TestGlobalFunctions(unittest.TestCase):

def setUp(self):

def tearDown(self):

def testdonuts(self):
self.assertEqual(string1.donuts(4), ‘Number of donuts: 4’, ‘donuts is 4’)
self.assertEqual(string1.donuts(9), ‘Number of donuts: 9’, "donus is not 9")
self.assertEqual(string1.donuts(10), ‘Number of donuts: many’)
self.assertEqual(string1.donuts(99), ‘Number of donuts: many’)

def testboth_ends(self):
self.assertEqual(string1.both_ends(‘spring’), ‘spng’)
self.assertEqual(string1.both_ends(‘Hello’), ‘Helo’)
self.assertEqual(string1.both_ends(‘a’), ”)
self.assertEqual(string1.both_ends(‘xyz’), ‘xyyz’)

def testfix_start(self):

def testmix_up(self):

if __name__ == ‘__main__’:

Note: The above code visualization is achieved using the Syntax Highlighter Evolved plugin for WordPress. You can download it below http://wordpress.org/extend/plugins/syntaxhighlighter/.

  • Regular Expressions
    match = re.search(pat, str)
    • a, X, 9, < — ordinary characters just match themselves exactly. The meta-characters which do not match themselves because they have special meanings are: . ^ $ * + ? { [ ] \ | ( ) (details below)
    • . (a period) — matches any single character except newline ‘\n’
    • \w — (lowercase w) matches a “word” character: a letter or digit or underbar [a-zA-Z0-9_]. Note that although “word” is the mnemonic for this, it only matches a single word char, not a whole word. \W (upper case W) matches any non-word character.
    • \b — boundary between word and non-word
    • \s — (lowercase s) matches a single whitespace character — space, newline, return, tab, form [ \n\r\t\f]. \S (upper case S) matches any non-whitespace character.
    • \t, \n, \r — tab, newline, return
    • \d — decimal digit [0-9] (some older regex utilities do not support but \d, but they all support \w and \s)
    • ^ = start, $ = end — match the start or end of the string
    • \ — inhibit the “specialness” of a character. So, for example, use \. to match a period or \\ to match a slash. If you are unsure if a character has special meaning, such as ‘@’, you can put a slash in front of it, \@, to make sure it is treated just as a character.
  • Use Regulazy.exe to do this automatically
  • Finished the Baby Names Exercise. Python is totally awesome!
  • Utilities

Day 6 (July 22nd)

  • Discovered ActivePython. The python that allows you to control window objects, such as your powerpoint slides
    • Simply download it and install it, and now you can use your favorite python IDE to manipulate window objects
  • Here is a demonstration that allows you to manipulate your PowerPoint slides Automating PowerPoint with Python
  • Created a post to document the process of automatically creating PowerPoint slides using Python

Written by Shengdong Zhao

Shen is an Associate Professor in the Computer Science Department, National University of Singapore (NUS). He is the founding director of the NUS-HCI Lab, specializing in research and innovation in the area of human computer interaction.