Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Philip Cannata 1 Python Overview. Dr. Philip Cannata 2 “Bad program” developed during class # The following python statement has python variables.

Similar presentations


Presentation on theme: "Dr. Philip Cannata 1 Python Overview. Dr. Philip Cannata 2 “Bad program” developed during class # The following python statement has python variables."— Presentation transcript:

1 Dr. Philip Cannata 1 Python Overview

2 Dr. Philip Cannata 2 “Bad program” developed during class # 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

3 Dr. Philip Cannata 3 Better version of the program developed by Magda Gomez in Fall 2013 tom, jerry, phineas, ferb, mickey, donald = 1000, 2000, 3000, 4000, 5000, 6000 def takeMoney(c1, c2, amt) : return c1-amt, c2+amt duo=[("tom", "jerry"), ("phineas", "ferb"), ("mickey", "donald")] keys = globals() # See the next page for a discussion about the globals() function. print keys["tom"] print(tom, jerry, phineas, ferb, mickey, donald) for i in duo: keys[i[0]], keys[i[1]] = takeMoney(keys[i[0]], keys[i[1]], 50) print(tom, jerry, phineas, ferb, mickey, donald)

4 Dr. Philip Cannata 4 The globals() Function The globals() function returns the dictionary which stores all global objects. It is amazing to see that you can actually define a global variable by simply typing: globals()["newVariable"] = "tom". And yes, you may refer to newVariable directly by saying print(newVariable) right after the previous statement. According to the Python Doc, there is a dictionary that stores all names (keys) and values of global variables. And when we hit an assignment statement, say tom = 1000, what the system really does is to access the value of tom by the key "tom" (which is a string), and set the value of that entry with key "tom" to 1000 in the global dictionary. So the globals() function does not initialize a new piece of memory and copy the keys and values, rather, it returns a reference of the dictionary in which Python stores all global objects. And here is a funny experiment to prove that python relies on that dictionary to remember all of its global variables. Try the following program: x = 1 print(globals()) print(x) globals().clear() print(globals()) print(x) And the program should report an error for the second print indicating that x is NOT defined! Additionally, this is also true for the locals() function, which stores all local variable names and values in the current scope.

5 Dr. Philip Cannata 5 Relations (A subset of the cross product of a set of domains) Examples of Relations: > 10 20 21 30 31 32...... keys tom1000 jerry2000 phineas3000 ferb4000 mickey5000 donald6000...... person nameagesalary phil651000 chris252000 Math Data Dictionary Function Definition Function Application Relations with Inheritance (see next 2 pages also) Table Classes (p 1 p 2 … p n body) (body a 1 a 2 … a n ) e.g., (x y x+y) (x+y 2 3)

6 Dr. Philip Cannata 6

7 Dr. Philip Cannata 7

8 Dr. Philip Cannata 8 Python class code for cartoon character example (Yes, this is a reformulation of the problem using classes instead of variables.) class Person(object): _registry = [] # _registry starts off as an empty list. name = "" money = 0 def __init__(self, name, amount): self._registry.append(self) # Each instance of Person is added to _registry. self.name = name self.amount = amount tom, jerry, phineas, ferb = Person('tom', 1000), Person('jerry', 2000), Person('phineas', 3000), Person('ferb', 4000) for p in Person._registry: print p.name + ", " + str(p.amount), # The comma at the end of the print statement causes all to print on one line. def transfer(p1, p2, amnt) : ______________________________________ # Fill in these 2 lines for Homework 1. Note, the “transfer” function ______________________________________ # requires no return statement. transfer(tom, jerry, 50) transfer(phineas, ferb, 50) print for p in Person._registry: print p.name + ", " + str(p.amount),


Download ppt "Dr. Philip Cannata 1 Python Overview. Dr. Philip Cannata 2 “Bad program” developed during class # The following python statement has python variables."

Similar presentations


Ads by Google