Fruitful Functions, Files and OOP: Classes & “Objects”
Fruit Functions
When determining the length, a String can demonstrate this while using the fruit function. The following illustration demonstrates how to apply the fruit function. On this case the len function, return the number characters on the string.
>>> fruit = “banana”
>>> len(fruit)
6
In some cases, to get the last number of the string, the following function can be of significant help, but sometimes it indicates as error on the computer.
length = len(fruit)
last = fruit[length]
The main reason why the function indicates error during the Program is that there is no number 6 since the number of counting normally starts from 0 to 5. This reason makes the indication of the term error in the program known as runtime error indicating Index error: string index out of range.on this casde, in order to get the last character, it is compulsory to subtract the last 1 from the length. The following illustration shows the resultof this particular fruit function.
length = len(fruit)
last = fruit[length-1]
Another alternative method is to use negative indices. The indices must be counting backward from the end of the string. This means that the expression fruit(-l)yields the last letter, the expression fruit(-2) yields the second to the last.
Another example of how to express fruit function in python is as follows:
prices = {‘apple’: 0.40, ‘banana’: 0.50}
my_purchase = {
‘apple’: 1,
‘banana’: 6}
grocery_bill = sum(prices[fruit] * my_purchase[fruit]
for fruit in my_purchase)
print ‘I owe the grocer $%.2f’ % grocery_bill
Objects and values
When referring to the objects in python, the following statement will fully explain on how to go
After taking for example a= “banana”
b= “banana”,
A programmer is advaised to note that ‘a’ and ‘b’ normally refers to a string that has letter, “banana” but one cannot identify on which string that the two arrows point at the same string. Acciording to the above expression, there are two possible states that ought to result due to the activity of the two arrows and they are as shown below.
a “banana”
b ‘banana”
Or
a
“banana”
b
According to the above illustrations, it is advisable to note that the above case demonstrates ‘a’ and ‘b’ that refer to different things that has the same value. In python, the thing that they are pointing is one that is known as python. It is also advisable to note that on the second case, ‘a’ and ‘b’ are pointing at the same object. All the objects tend to have the same identifier that the programmer can be able to obtain with the id function. With the aid of the id function, the programmer can be able to identify whether ‘a’ and b’ refer to the same object. For example,
>>> id(a)
135044008
>>> id(b)
135044008
The advantage that the programmer identifies on this case is that through the help of python, it is possible to get the same identifier twice, this gives an indication that the python only created one string and both of a and b refer to it. After creating both of the two objects, the programmer ends up getting two objects. For example:
>>> a = [1, 2, 3]
>>> b = [1, 2, 3]
>>> id(a)
135045528
>>> id(b)
135041704
Both of ‘a’ and ‘b’ have the same value but they do not refer to the same object. The diagram of the object will now look as demonstrated below:
a [1,2 3,]
b [1,2,3]
Reference
Downey, A, Elkner J, Meyers, c. (2003). How to Think Like a Computer Scientist: Learning with Python. New York: Biolateral Pty, Limited.
Lutz M. (2010). Programming Python. New York: O’Reilly Media, Inc.,.