Classes & “Functions”, Classes & “Methods” Case Multiple Assignments

Classes & “Functions”, Classes & “Methods” Case
Multiple Assignments
According to the rule regarding multiple assignments, one may opt to make several assignments in the same variable. The best operation to perform problems associated with multiple assignments entails that a new assignment always makes the existing variable refer to a new value. In this case, the new assignment stops referring to the old value. For example:
Orange =5
Print orange
Orange =7
Print orange
In mathematics, there is a significant difference on how perform various expressions and how to do it in python. For example, in mathematics, if b= 5, then it is right to say that 5= b. however, in python, if b=5, it is wrong to say that 5=b. this means that in python an assignment statement can always make the same variables equal but in most of the time, they do not have to stay that way. For example,
c=8
d=c # c and d are now equal
c=4 # c and d are longer equal
The While Statement
The following is an example of the While statement. The function countdown helps in demonstrating how the while statement works. According to the statement, the expressions indicate that while is greater than o, always continue displaying the value of p and then continue to reduce the value of p by 1. When you manage to get to 0, display the word Master!.
def countdown(p):
while p > 0:
print p
p = p-1
print “Master!”

The following example also demonstrates how to use the while statement
def sequence(n):
while n != 1:
print n,
if n % 2 == 0: # n is even
n = n / 2
else: # n is odd
n = n * 3 + 1
Counting digitals
The following illustration shows how to count digitals while using python. The function counts the number of decimals digits in a positive intiger.

def num_digits(n):
count = 0
while n:
count = count + 1
n = n / 10
return count
Another example illustrating how a function works while counting the digitals either 0 or 5 is indicated below;

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
A Find Function
The following the function illustrates that through python programming, it is possible to take a character and find the index where the character appears. The result indicates that if the character is not found, the function will obvious return to normal.the string (index) abbreviated as ch, makes the function to break out of the loop prematurely. On this case, if the character fails to appear in the string, the program will now exit the loop and then return to zero.
def find(strng, ch):
index = 0
while index < len(strng):
if strng[index] == ch:
return index
index += 1
return -1

#assume strng is “banana” and ch is “a”
#if strng[index] == ch:
#return index
#the above 2 lines check if strng[index#] == a
#when the loop runs first index is 0 which is b (not a)
#so 1 is added to whatever the value of index is
#when the loop runs second time index is 1 which is a
#the loop is then broken, and 1 is returned.
#if it cannot find ch in strng -1 is returned

Reference
Downey,A. Elkner, A & Meyers, C (2003). How to Think Like a Computer Scientist: Learning with Python. New York: Biolateral Pty, Limited.=.
Wolber,D. A, H & Ellen S,Looney L. (2011). App Inventor. New York: O’Reilly Media, Inc.

Latest Assignments