1 / 8

Python Overview

Python Overview. “ 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

oceana
Télécharger la présentation

Python Overview

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Overview

  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. 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. 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. Relations (A subset of the cross product of a set of domains) Examples of Relations: Math Data Dictionary Function Definition Function Application (p1 p2 … pn body) (body a1 a2 … an) e.g., (x y x+y) (x+y 2 3) Table Classes Relations with Inheritance (see next 2 pages also)

  6. 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) ,

More Related