Archive for the ‘Support’ Category

Useful Functions in Python

February 5, 2008

If you look at the sample solutions to the exercises for Week 2 you will notice a few things that you have not met so far. In the post below, which discusses one aspect of the ‘raw_input()’ function, a method for converting numbers held as strings into their actual number value is demonstrated. This works fine if the contents of the string given to the ‘int()’ function are digits e.g. “24” however if the string contains letters e.g. “john” then the function fails. This is a common dilemma in programming – you need to ensure that the type of data you are using is the correct (expected) data type before you use it.

The ‘wk2_ex1_age.py’ program shows one way that you can check what type of data is in a string before you use, namely to use ‘str.isdigit()’, one of a family of functions that work with strings:

isalnum(), isalpha(), isdigit(), islower(), isupper(), isspace(), and istitle()

The length of the string being compared must be at least 1, or the is* methods will return False. In other words, a string object of len(string) == 0, is considered “empty”, or False.

  • isalnum returns True if the string is entirely composed of alphabetic or numeric characters (i.e. no punctuation).
  • isalpha and isdigit work similarly for alphabetic characters or numeric characters only.
  • isspace returns True if the string is composed entirely of whitespace.
  • islower, isupper, and istitle return True if the string is in lowercase, uppercase, or titlecase respectively. Uncased characters are “allowed”, such as digits, but there must be at least one cased character in the string object in order to return True. Titlecase means the first cased character of each word is uppercase, and any immediately following cased characters are lowercase. Curiously, ‘Y2K’.istitle() returns True. That is because uppercase characters can only follow uncased characters. Likewise, lowercase characters can only follow uppercase characters. Hint: whitespace is uncased.

see http://en.wikibooks.org/wiki/Python_Programming/Strings for other useful things to know about strings.

Using raw-input() to read in data

February 4, 2008

If you have attempted exercise one for week 2 you may have experienced some difficulty in creating an answer for the second part of this exercise. This is due to the fact that raw_input() always provides the keyboard input that it captures as a string. The second part of the the first problem asks you to calculate the age next year for the person responding to the program.  You have probably tried to add 1 to the value read in and found that this generated a lot of red text when you tried to run the program.

So you now know that you can’t perform arithmetic operations on a string, but how do you fix the problem? Well you need to convert the value that you typed at the keyboard from a string to a number and this can be done using another function called ‘int()’. This takes a string as it’s argument and transforms the digits in that string into their number value e.g. “21” – the character 2 followed by the character 1 needs to become the value 21.

One solution to this exercise is given below:

age = int(raw_input("Please enter your age ..> "))

print "your current age is ", age, " and next year you will be ", age + 1

Common Early Problems People Have With Python

January 30, 2008

1. Python files need to be called ‘filename.py’ e.g. ‘outputExample.py’; if you don’t add the ‘.py’ extension to the file name then your file will be saved as a ‘.txt’ file and when you load it into Python or IDLE the colour coding of keywords, strings etc will not appear. If this has happened to you resave your file with the ‘py’ extent.

2. By default when you save a file from the IDLE environment it will be saved in the Python installation directory. You need to ensure that as you follow the save dialogue you set the path for the file to be saved in as part of your student file structure.

3. Work in the IDLE development window – you get this by starting the Python shell and then selecting the File menu and new window from there – and save your files to a suitable part of your student file structure. I suggest that you create a subdirectory called Python and within that create weekly subdirectories so that you can find work done for each week.

4. Remember, variables need to start with a letter (preferably lowercase)

5. Write one statement per line – an example of a statement would be ‘print “John”‘

Email me at johnrichard.gray@gmail.com’ or post a blog to this site if you want to comment on any of these points or suggest other points that would be helpful.