a = [ 'one', 'two', 'three', 'four' ]
# Make a copy of a. Note: not a deep copy because references within
# point to the same object.
# In order to make a deep copy, use copy.deepcopy
#
a_copy = a[:]
# Create a reference to the array pointed at by 'a'.
a_ref = a
a_copy.append('five')
a_ref .append('FIVE')
print(a_copy) # ['one', 'two', 'three', 'four', 'five']
print(a_ref ) # ['one', 'two', 'three', 'four', 'FIVE']
print(a ) # ['one', 'two', 'three', 'four', 'FIVE']
#/usr/bin/python
#
# Does python pass by value or by reference?
#
# Output of program is
#
# B[two] = 2
# B[one] = 1
#
#
def f(X, k, v):
X[k] = str(v)
A=dict()
f(A, 'one', 1)
B=A
f(A, 'two', 2)
for k in B:
print " B["+k+"] = " + B[k]
import Image
im = Image.new('RGB', (256, 256), "black")
pixels = im.load()
for x in range(0, 256):
for y in range(0, 256):
pixels[x, y] = (x, y, 127)
im.show()
im.save('pixels.png')
#!/usr/bin/python3
from FooModule import module_func_sum
print(module_func_sum (38,4)) # 42
# print FooModule.module_func_mult (38,4) # NameError: name 'FooModule' is not defined
# print module_func_mult(21,2) # NameError: name 'module_func_mult' is not define
#/usr/bin/python
import collections
cnt=collections.Counter()
for word in "foo bar baz more or less foo most foo even more foo or is it less".split():
cnt[word]+=1
print "for word in cnt"
for word in cnt:
print " %-10s: %d" % (word, cnt[word])
print "\nfor word in cnt.most_common(3)"
for word in cnt.most_common(3):
print " %-10s: %d" % word
print "\nsum(): "
print " There are " + str(sum(cnt.values())) + " words in the original string"