Transcript Overview

Python Overview
Dr. Philip Cannata
1
“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
Dr. Philip Cannata
2
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)
Dr. Philip Cannata
3
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.
Dr. Philip Cannata
4
Relations
(A subset of the cross product of a set of domains)
Examples of Relations:
Math
Data Dictionary
>
keys
Function Definition Function Application
(p1 p2 … pn body)
e.g., (x y x+y)
1
0
tom
1000
2
0
jerry
2000
2
1
phineas
3000
3
0
ferb
4000
name
age
salary
3
1
mickey
5000
phil
65
1000
3
2
donald
6000
chris
25
2000
.
.
.
.
.
.
.
.
.
.
.
.
Dr. Philip Cannata
Table
(body a1 a2 … an)
(x+y 2 3)
Classes
person
Relations with
Inheritance (see
next 2 pages
also)
5
Dr. Philip Cannata
6
Dr. Philip Cannata
7
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) ,
Dr. Philip Cannata
8