You are on page 1of 4

Worksheet 1:

Task
For information on integer and float size limits:
>>> import sys
>>> sys.maxint
9223372036854775807
>>> sys.float_info
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308,
min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15,
mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
Task
For
more
info
on
identifiers,
see
2.3.
keywordshttp://docs.python.org/reference/lexical_analysis.html
(Fibonacci numbers)
>>> x0, x1 = 0, 1
>>> while x0 + x1 < 1e22:
... x0, x1 = x1, x0 + x1
...
>>> x1
6356306993006846248183L
Task
>>> x=1.0
>>> while x*2.0 < float("inf"):
... x=x*2.0
...
>>> x
8.98846567431158e+307
Task
(List manipulation)
>>> tmp = icecream_flavours[0]
>>> icecream_flavours[0] = icecream_flavours[1]
>>> icecream_flavours[1] = tmp

Identifiers

and

Reverse list:
>>> icecream_flavours = icecream_flavours[::-1]
Task
(Copying with nested lists)
Note is and == give True for all the elements.
For an independent copy we also need to copy the elements of the list:
>>> y=x[:]
>>> y[1] = x[1][:]
>>> y[1][0] = "different"
>>> y
['A', ['different', 'list']]
>>> x
['A', ['nested', 'list']]
Optional Alternatively, use copy.deepcopy (see http://docs.python.org/library/copy.html)
deepcopyrecursively constructs new copies, shallow copy only make new copies at the
top level.
Task
>>> ice=icecream_flavours
>>> ice[0],ice[1] = ice[1],ice[0]
Task
>>> student_grades.pop("Laura")
112
>>> student_grades["Callidus"]=85
>>> student_grades["Ignavus"]=['18','22','33']
>>> student_grades
{'Ignavus': ['18', '22', '33'], 'Callidus': 85, 'Simon': 60, 'Jenny': 68}
Task
>>> unavailable = ["vanilla", "chocolate", "chilli", "lemon"]
>>> new_flavours = ["bacon", "raspberry", "spam", "rutabaga ripple", "cheese"]
>>> for f in unavailable:
...
if f in icecream_flavours:
... icecream_flavours.remove(f)

...
>>> for f in new_flavours:
...
if f not in icecream_flavours:
...
icecream_flavours.append(f)
...
>>> icecream_flavours
['green egg', 'snail and lettuce', 'bacon', 'dorset naga', 'strawberry', 'raspberry', 'spam',
'rutabaga ripple', 'cheese']
Task
>>> [i**0.5 for i in x if i%2 == 0]
[9.273618495495704, 9.899494936611665, 6.928203230275509, 6.48074069840786,
9.38083151964686, 6.782329983125268, 5.0990195135927845, 9.591663046625438,
3.4641016151377544, 1.4142135623730951]
Task
>>> names = ["Bob", "Otto", "Sandra"]
>>> [name+"sson" for name in names]
['Bobsson', 'Ottosson', 'Sandrasson']
Task
>>> sq=[x**2 for x in range(1,1001)]
>>> ways=[[m for m in range(1,x) if x**2-m**2 in sq] for x in range(1,1001)]
>>> nways=[len(w)/2 for w in ways] # each combination is counted twice
>>> p=[i+1 for i in range(1000) if nways[i]>0]
>>> p[0:10]
[5, 10, 13, 15, 17, 20, 25, 26, 29, 30]
Task
>>> def fibo(xmax, xmin=0):
... results = []
... x0, x1 = 0, 1
... while x0 <= xmax:
... if x0 >= xmin:
...
results.append(x0)
... x0, x1 = x1, x0 + x1
... return results

>>> fibo(60,6)
[8, 13, 21, 34, 55]
Task
-blankTask
>>> icecream_flavours.remove('green egg')
>>> icecream_flavours.append('mango')
>>> icecream_flavours.append('orange')
>>> icecream_flavours.sort(cmp=lambda x,y:cmp(len(x),len(y)))
>>> icecream_flavours
['spam', 'bacon', 'mango', 'cheese', 'orange', 'raspberry', 'strawberry', 'dorset naga',
'rutabaga ripple', 'snail and lettuce']
Task
>>> import random
>>> [random.gauss(0,1.0) for i in range(10)]
[0.3868714597860198, 0.21032149488228238, 0.8798982260257919,
-0.607831904470859, 1.172017754748173, -1.6698857154468942,
0.015860820124381444, -0.10066867833485314, -0.18202467632219527,
-2.1157891654822016]
Task
>>> fid=open('myfile.pkl','wb')
>>> pickle.dump(icecream_flavours,fid)
>>> fid.close()
>>> fid=open('myfile.pkl','rb')
>>> IC=pickle.load(fid)
>>> fid.close()
>>> IC
['spam', 'bacon', 'mango', 'cheese', 'orange', 'raspberry', 'strawberry', 'dorset naga',
'rutabaga ripple', 'snail and lettuce']

You might also like