Thursday, March 10, 2011

Mar. 10

When working with the string module, I realized that the expression dir means directory.
>>>import string
>>>dir(string)

['Formatter', 'Template', '_TemplateMetaclass', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_float', '_idmap', '_idmapL', '_int', '_long', '_multimap', '_re', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'atof', 'atof_error', 'atoi', 'atoi_error', 'atol', 'atol_error', 'capitalize', 'capwords', 'center', 'count', 'digits', 'expandtabs', 'find', 'hexdigits', 'index', 'index_error', 'join', 'joinfields', 'letters', 'ljust', 'lower', 'lowercase', 'lstrip', 'maketrans', 'octdigits', 'printable', 'punctuation', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitfields', 'strip', 'swapcase', 'translate', 'upper', 'uppercase', 'whitespace', 'zfill']
The above is the full list of the items in the string module.
When i ask to see all the digits I get from the string module I get:
>>> print string.digits
0123456789
Not surprising you get all the digits. 
>>> string.find("banana", "na")
2
The out come of this program is 2 because in the word "banana", you will find 2 occurrence of "na". >>> >>>print string.lowercase
abcdefghijklmnopqrstuvwxyz
>>> print string.uppercase
ABCDEFGHIJKLMNOPQRSTUVWXYZ
>>> print string.digits
0123456789
When working with the string module, I've noticed that it has many possible outputs to the program. Basically this program has self explanatory, such as lowercase, uppercase, and digits. 
>>> print string.whitespace
When you put this into the terminal you get just a few open lines of blank space.

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. 

Tuesday, March 8, 2011

Mar. 8

To re-enforce what I've learned on string slices. An example to help me out is:
>>>s = "Andrew"
>>>print s[:]
Andrew
>>>print s[1:2]
n
>>>print s[1:5]
ndre
>>>print s[:-1]
Andre
>>>print s[-1:]
w
And so on......
This technique is really easy to understand. I just had to keep in mind the basic principle that instead of starting to count at one, you start at zero.
With immutable strings, the following program is easy to understand.

>>> greeting = "Hello, world!"
>>> newGreeting = 'J' + greeting[1:]
>>> print newGreeting
Jello, world!
When you want to change the out put of "Hello, world!" to "Jello, world!" all you'd do like the program shows is put new before your variable, then in order to change the letter of the text you'd have to put the exact point to change. Like it says to change from greeting change it would print everything after the first letter, which is H. The [1:] would print every other letter after the H, so then when  you add the 'J' you get the output of "Jello, world!" But if i first started out with trying to change the first word on in greeting by just asking for greeting[0] = 'J' then id get the error of : TypeError: 'str' object does not support item assignment.

Friday, March 4, 2011

Mar. 4

Starting on chapter 7 with compound data types. 
When I put into the terminal:
>>> fruit = "banana" 
>>> letter = fruit[1]
>>> print letter 
I would get a, I really expected to get like f or b but reading on I was able to find out that with computer programing, you start off by counting at 0. So instead, if i used fruit[0] then id get b instead of a. I then after realizing that it would print a, that instead of printing letters from fruit, it would print from banana, which I should have realized when I first put in fruit = "banana". 
Here area all the letters and the fruit[n] you can use in order to finish typing out banana:
fruit[0]
b
fruit[1]
a
fruit[2]
n
fruit[3]
a
fruit[4]
n
fruit[5]
a
The next step to chapter 7 was length, which the abbreviation for length on python is len. 
>>>fruit = "banana"
>>>len(fruit)
so for this program, length would be used as to tell how many letters long the word was. You would get an output of 6 for this program. 
example:
Instead of using fruit, I used word for this program. 
>>> word = "pneumonoultramicroscopicsilicovolcanoconiosis"
>>> len(word)
45
The word used was the longest word that can be found in a major dictionary. This word as the program exclaims has 45 letters/ length of the word is 45 characters. 
In order to get the program to say the letter from the end of the word you would put in(this is back with the banana program:
>>> length = len(fruit)
>>> last = fruit[length-1]
>>>last
'a'
Then if you change fruit[length-1] to fruit[length-2] you would get 'n' and so on and so forth. 
A transversal program like the following puts on its own line a letter of "banana":
 index = 0
>>> while index < len(fruit):
...     letter = fruit[index]
...     print letter
...     index += 1
... 
b
a
n
a
n
a
You can also get the same results using:
>>> for char in fruit:
...     print char
... 
b
a
n
a
n
a
The following program is used to assign the prefix with the suffix of a word and have them in alphabetic order:
 >>> prefixes = "JKLMNOPQ"
>>> suffix = "ack"
>>> 
>>> for letter in prefixes:
...     print letter + suffix
... 
Jack
Kack
Lack
Mack
Nack
Oack
Pack
Qack
>>> 
Even though Oach and Qack are missed spelled do to they having more letters in the word. Correction is Ouack and Quack
Working with a program that produces just the letters out of a word, I've used the [:] code:
>>> fruit = "banana"
>>> fruit[:3]
'ban'
>>> fruit[3:]
'ana'
When using the [:], this allows me to choose which letters I want displayed. For instance from the program above I used fruit[:3] to get the first 3 letters of "banana", and then used fruit[3:] for the last 3. Basically the side the number is on depends on what it will display but one side must be blank to show the numbers you want to display instead of setting a guide to another area to stop at(I don't know if I'm explaining this clearly, please comment if it makes sense to you, if not I'll update this when I get feedback.). 

Thursday, March 3, 2011

This is a rewritten sequence program:
>>> def sequence(n):
...     while n != 1:
...             print n, 
...             if n % 2 == 0:
...                     n = n / 2
...             else:
...                     n = n * 3 + 1
... 
>>> sequence(3)
3 10 5 16 8 4 2
The program above also so the program working. 
This is the num_digits program that I've failed to put on here. This program also has three examples of the program working and getting an output.
>>> def num_digits(n):
...     count = 0
...     while n:
...             count = count + 1
...             n = n / 10
...     return count
... 
>>> num_digits(710)
3
>>> num_digits(999999999999999)
15
>>> num_digits(111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111)
96
>>> 
This program is basically self explanatory, it basically just counts the number places in the number. 
This is the num_zero_and_five_digits. This program counts the number of zeros and number of fives in the number:
>>> def num_zero_and_five_digits(n):
...     count = 0
...     while n:
...             digit = n % 10
...             if digit == 0 or digit == 5:
...                     count = count + 1
...             n = n / 10
...     return count
... 
>>> num_zero_and_five_digits(1055030250)
7

Wednesday, March 2, 2011

Mar. 2

Sorry I messed up on writing Newton's algorithm, the program is:

>>> def sqrt(n):
...     approx = n/2.0
...     better = (approx + n/approx)/2.0
...     while better != approx:
...         approx = better
...         better = (approx + n/approx)/2.0
...     return approx
...
These are the questions for chapter 6


Tuesday, March 1, 2011

Mar. 1

This program is basically a program that shows ab = ba as a table.

def print_multples(n):
i = 1
while i <= 6:
print n * i, '\t',
i += 1
print
def print_mult_table(high):
    i = 1
    while i <= high:
        print_multiples(i)
        i += 1
So for example, if we plug in print_mult_table(7) we get the following table:
1   2   3   4   5   6   7
2   4   6   8   10   12  14
3   9   12  15  18  21
4   8   12   16  20  24  28
5   10   15   20   25   30   35
6   8   12  18   24   30   36   42
7   14   21   28   35  42  49
(sorry this time i didn't try to equal up the sides of the table)
This program makes it where the table will have a downwards slope as the number rises. This table also takes away the numbers 2-7 from the top row, then each row it adds another number. Here is the table so you understand what I mean:

1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6 12 18 24 30 36
7 14 21 28 35 42 49
I then started to work on Newton's method which the program was:
def sqrt(n): approx = n/2.0 better = (approx + n/approx)/2.0 while better != approx: approx = better better = (approx + n/approx)/2.0 return approx
I then plugged in sqrt(25) and got an out-put of 5.0:
>>> sqrt(25) 5.0