Transcript 01A Classes

Python Classes
Dr. Philip Cannata
1
# The following python statement has python variables which are intended to represent the fact that each cartoon
character has the given amount of money.
tom, jerry, phineas, ferb, mickey, donald = 1000, 2000, 3000, 4000, 5000, 6000
print tom, jerry
# The following function can be passed three integer arguments, it subtracts the third argument from the first and
adds the third argument to the second. It returns the changed values of the first two arguments.
def takeMoney(c1, c2, amt) :
c1-=amt
c2+=amt
return c1, c2
# The following are data structures that pair appropriate cartoon characters.
# This data structure is a list of lists.
duo=[[tom, jerry], [phineas, ferb], [mickey, donald]]
# This data structure is a tuple of lists.
duo=([tom, jerry], [phineas, ferb], [mickey, donald])
# This data structure is a tuple of tuples.
duo=((tom, jerry), (phineas, ferb), (mickey, donald))
# This data structure is a list of tuples.
duo=[(tom, jerry), (phineas, ferb), (mickey, donald)]
# The following "for loop" iterates over duo calling takeMoney for each pair in duo.
cnt=0
for i in duo :
if cnt == 0:
tom, jerry = takeMoney(i[0], i[1], 50)
elif cnt == 1 :
phineas, ferb = takeMoney(i[0], i[1], 50)
elif cnt == 2 :
donald, mickey = takeMoney(i[0], i[1], 50)
cnt+=1
print tom, jerry, phineas, ferb, mickey, donald
# Returns 950 2050 2950 4050 6050 4950
Dr. Philip Cannata
2
Dr. Philip Cannata
3
Dr. Philip Cannata
4