Fruitful Functions, Files and OOP: Classes & “Objects Case”
Fruitful Functions
The for loop
The for loop in python can be able to iterate on various items depending on the structure of every sequence. This is mostly common in a list or in the string. In python, the part that programmers consider to be a single block of code are usually indented by the same code. The most significant issue to note is that python usually uses indentation as its method of grouping. A good illustration on this, is on the case of fruitful function as illustrated below.
#!/usr/bin/python
for letter in ‘Python’: # First Example
print ‘Current Letter :’, letter
fruits = [‘banana’, ‘apple’, ‘mango’]
for fruit in fruits: # Second Example
print ‘Current fruit :’, fruit
print “Good bye!”
According to the above case, the possible result that the function will produce is as indicated below.
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!
Another example that most of the programmers tend to use is by indexing offset into the sequence itself. The following case illustrates on how this is possible.
#!/usr/bin/python
fruits = [‘banana’, ‘apple’, ‘mango’]
for index in range(len(fruits)):
print ‘Current fruit :’, fruits[index]
print “Good bye!”
According to the above illustration, the most possible result will be as indicated below.
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!
Classes & “Objects Case
The break statement
When operating on a python program, the most common use of breaks is in the case, where there is a need of a compulsory exit. The most advantage of the break loop is that most of the programmers can be able to apply them in both while and for loops. This is well illustrated in the example below.
#!/usr/bin/python
for letter in ‘Python’: # First Example
if letter == ‘h’:
break
print ‘Current Letter :’, letter
var = 10 # Second Example
while var > 0:
print ‘Current variable value :’, var
var = var -1
if var == 5:
break
print “Good bye!”
According to the above example, the most possible result that the function will give is as indicated below.
urrent Letter : P
Current Letter : y
Current Letter : t
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Good bye!
The else Statements used in loop
According to most of the programs used in python, they tend to associated with an else statement mostly in the in the case of loop statements. The significant importance of the else statement indicates that when this type of statement is used with for loop, it will be executed after the loop finishes in iterating the list. However, on the case of the while loop, the else statement is usually executed when the condition becomes false. The following statement is a good example of how the else statement operates while in a combination with the for statement. The example illustrates both of the statements searching for prime numbers that are between the digits 10and 20. It is also good to note that a programmer can also use a similar way to make to operate the else statement with the while loop.
#!/usr/bin/python
for num in range(10,20): #to iterate between 10 to 20
for i in range(2,num): #to iterate on the factors of the number
if num%i == 0: #to determine the first factor
j=num/i #to calculate the second factor
print ‘%d equals %d * %d’ % (num,i,j)
break #to move to the next number, the #first FOR
else: # else part of the loop
print num, ‘is a prime number’
The possible result of the above function will be as indicated below.
10 equals 2 * 5
11 is a prime number
12 equals 2 * 6
13 is a prime number
14 equals 2 * 7
15 equals 3 * 5
16 equals 2 * 8
17 is a prime number
18 equals 2 * 9
19 is a prime number
Reference
Hall, T& Stacey, J. P. (2009). Python 2: For Beginners Only”. New York: Apress.
Lutz M. (2010). Programming Python. New York: O’Reilly Media, Inc.