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.). 

2 comments:

  1. This entry is redundantly titled "March 4" (see my comment from yesterday).

    Slicing, which is the python term for extracting a subsequence from a sequence using [low:high] syntax, is a wonderful tool.

    ReplyDelete
  2. Oh, I keep forgetting to suggest to you that you rename your blog. "Gaming" is misleading, and is likely to disappoint anyone coming here attracted by the title.

    Calling it something like "Learning Game Programming" would be a lot less misleading.

    ReplyDelete