Wednesday, March 9, 2011

Mar. 9

Working with the in command, I've noticed that it basically tells what it does. For example:

>>> 'p' in 'apple'
True
>>>
>>> 'i' in 'apple'
False
>>> 'ap' in 'apple'
True
>>> 'pa' in 'apple'
False
This program will only come out to saying its True only if the letter your asking for is inside the word you plug in. Like for apple, you already know you'd find a-p-p-l-e in the word. So if you ask the program if q or like if w is in the word, it will come out False.
>>>'apple' in 'apple'
True
You get this because well you'd find apple when you look in the word apple. This is basically self explanatory. 
>>> def remove_vowels(s):
...     vowels = "aeiouAEIOU"
...     s_without_vowels = ""
...     for letter in s:
...             if letter not in vowels:
...                     s_without_vowels += letter
...     return s_without_vowels
... 
With this program, you must first assign a word to a variable.
a = 'apple'
Once you have the variable you just plug the variable into the program and get:
'ppl'
So the program basically just deletes all the vowels in the word and just spits out what ever is left. 
>>> fruit = "banana"
>>> count = 0
>>> for char in fruit:
...     if char == 'a':
...             count += 1
...     print count
... 
0
1
1
2
2
3
This program just outputs how many times you input 'a'. So as the program inputs 'banana' into its system, it starts with b which is 0, then the a which now is 1, then the n which stays as one saying thats there has been an a before. Then another a which now is 2. Then another n which will stay 2 as previously stated. Then the 3rd and final a which is now called 3, thus getting the above results. 

No comments:

Post a Comment