November 25th, 2004, 09:41 PM
Say i wanna create a function called "notvowel" and it'll take a character as its argument. then if the character is a vowel, i want it to return "false". if it's not a vowel, then "true". And i need to do this for both upper and lower case..
so far i have...
vowel = "A"
if m == vowel:
but that works only if i type in print notvowel("A").. so i was wondering what do i need to do to get A E I O U and a e i o u in there..?
November 25th, 2004, 10:07 PM
You cld do it this way....
>>> def notVowel(m):
... if m in vowel:print m,'is a vowel!'
... else: print m,'is not a vowel!'
r is not a vowel!
e is a vowel!
A is a vowel!
November 26th, 2004, 07:21 AM
A slightly different approach to this would be to return a boolean value for the expression: character not in vowels
Besides being smaller this also gives you more controle than a hard coded if statment and makes the function more reusable . Although since this function is so small anyway you probably wouldn't need it; you could use the expression above.
>>> vowels = 'aeiou'
>>> def notVowel(character):
... return character.lower() not in vowels
November 26th, 2004, 01:04 PM
Just a thought: wouldn't it be better if vowels was a list, not a string?
November 26th, 2004, 07:31 PM
I don't see why it would be...
- A string takes up a string indicator plus an amount per character.
- A list takes a list indicator, plus a type indicator per item, plus the item space, possibly plus overhead for locating the items in the list in memory.
Searching: A string is iterable, so is a list. I don't know how 'in' does it's searching, so I don't know how to compare the two.
t2 runs about ten times faster than t4 (and removing the .lower() function seems to be worth the extra 5 characters of typing too).
>>> from timeit import Timer
>>> t1 = Timer("'I'.lower() in 'aeiou'")
>>> t2 = Timer("'I' in 'aAeEiIoOuU'")
>>> t3 = Timer("'I'.lower() in ['a', 'e', 'i', 'o', 'u']")
>>> t4 = Timer("'I' in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']")
The second gets my vote for being less cluttered and 'busy'.
>>> 'I' in ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']
>>> 'I' in 'aeiouAEIOU'
Lines of code: No more lines, no fewer.
Amount of typing: Significantly more characters in the list version, as you need two apostrophes and a comma per entry and two list brackets compared to two apostrophes total.
Filesize: More characters for the list, so a larger file.
Time to compile:
Using the built in compile() function, I get:
Compiling the list is getting on for taking twice as long.
>>> import compile
>>> tc1 = Timer("compile(\"'aeiou'\", '', 'single')")
>>> tc2 = Timer("compile(\"['a', 'e', 'i', 'o', 'u']\", '', 'single')")
If we change that to include both upper and lower case vowels, it becomes:
Much the same for the string, significantly slower for the list...
>>> tc1 = Timer("compile(\"'aeiouAEIOU'\", '', 'single')")
>>> tc2 = Timer("compile(\"['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']\", '', 'single')")
November 26th, 2004, 07:46 PM
Hmmm.... interesting... I gotta remember that.
November 27th, 2004, 08:19 PM
Thank you all for the help^^