Chapter 7

 

 

Section 7.2 Initialization

 

51600
plist = []
51601
plist = ["spam", "eggs", "vikings"]
51192
plist = [10,20,30,40,50,60,70,80,90,100]
51193
tax_rates = [0.10, 0.15, 0.21, 0.28, 0.31]
51194
denominations = [1,5,10,25,50,100]
Section 7.2 Indexing  
51280
for i in range(len(list1)-1) :
    if list1[i] in list1[i+1:] :
        has_dups = True
        break
    else :
        has_dups = False
51203
plist[-1] += 5
51201
salary_steps[-1] = 160000
51195
plist[0]
51196
plist[-1]
51197
x=plist[-2]
51199
plist[0] = 3
51200
salary_steps[0] = 30000
51202
plist[0]+= 10
51204
plist[k] = 15
51205
plist[-1] = -1
51206
plist[k+1] = 9
51207
plist[k-1] = 22
51208
plist[j] = 2 * plist[j+1]
51209
play_list[0] = 2*play_list[-1]
Section 7.2 Length  
51611
plist1 + plist2
51612
plist3 = plist1 + plist2
51602
len(play_list)
51211
a[len(a) - i - 1]
Section 7.3  
51613
play_list[0:5]
51614
play_list[k:j+1]
51615
play_list[k:j]
51616
play_list[0:3] = ["spam", "eggs", "vikings"]
51617
L1[5:9] = L2
51618
lesser_offenders = worst_offenders[5:]
51619
plist2 = plist1[k:]
Section 7.4  
51620
3 in plist
51621
k in play_list
51213
is_a_member = member_id in current_members
Section 7.5  
51212
a.reverse()
51256
words.reverse()
51610
play_list.sort()
51294
lst.sort()
51603
plist.append(5)
51609
del alist[k]
51607
del alist[0]
51608
del alist[3]
51275
list3 = []  
for i in range(len(list1)):
list3.append(list1[i])
list3.append(list2[i])
51276
list3 = []  
for i in range(len(list1)-1, -1, -1):
list3.append(list1[i])
list3.append(list2[i])
51277
list3 = []  
for i in range(min(len(list1), len(list2))):
list3.append(list1[i])
list3.append(list2[i])
51278
list3 = []  
for i in range(max(len(list1), len(list2))):
if i < len(list1) : list3.append(list1[i])
if i < len(list2) : list3.append(list2[i])
51284
arith_prog = [i for i in range(1, n+1, distance)]
51285
arith_prog = [i for i in range(m, n+1, distance)]
51287
geom_prog = []  
i = 1
while i <= n :
geom_prog.append(i)
i *= ratio
51288
fib = [0, 1]  
i = 1  
lastI = 0  
while i+lastI <= n :
lastI, i = i, lastI + i
fib.append(i)
51289
fib = []  
if m <= 0 and n >= 0 : fib.append(0)  
if m <= 1 and n >= 1 : fib.append(1)  
i = 1  
lastI = 0  while i+lastI <= n :
lastI, i = i, lastI + i
if i >= m :
fib.append(i)
51295
new_list = []  
for i in lst1 :
if i in lst2 :
new_list.append(i) new_list.sort()
51296
new_list = []  
for i in lst1 :  
if not i in lst2 :
new_list.append(i) for i in lst2 :
if not i in lst1 :
new_list.append(i) new_list.sort()
Section 7.7  
51214
duplicates = False  
k = 0  
while k < len(zipcode_list)-1 and not duplicates:     
duplicates = zipcode_list[k] == zipcode_list[k+1]
k += 1
51215
duplicates = False
j = 0
while j < len(zipcode_list)-1 and not duplicates:
    k = j + 1
    while k < len(zipcode_list) and not duplicates:
        if zipcode_list[k] == zipcode_list[j]:
         duplicates = True
        k += 1
    j += 1
51210
total = 0.0  
for k in temps:   
total += k avg_temp = total / len(temps)
51216
number_of_incompletes = 0  
for k in incompletes:
if student_id == k:
number_of_incompletes += 1
51219
most_tickets = parking_tickets[0]  
for k in parking_tickets:
if k > most_tickets:
most_tickets = k
51263
sum = 0  
for i in numbers :
if i >= 0 : sum += i
51290
for e in lst :
if not e in s :
contains = False
break else :
contains = True
51291
if n <= 1 :
    result = n
else :
    count = 2
    i = 1
    lastI = 0
    while count <= n :
        lastI, i = i, lastI + i
        count += 1
    result = i
51292
triangulars = []  
t = 0  
for i in range (1, n+1) : 
t += i
triangulars.append(t)
51293
triangulars = []  
t = 0  
i = 1  
while t+i <= n :
t += i
if t >= m : triangulars.append(t)
i += 1
51297
max_dup = -1  
for i in range(len(lst)) :
if lst[i] in lst[i+1:] and max_dup < lst[i] :
max_dup = lst[i]
51150
print_list(inventory)
51163
def is_reverse(a, b):
a.reverse()
rv = a == b
a.reverse()
return rv
51283
maxPos = 0  
for i in range(1, len(names)) :
if names[i] > names[maxPos] :
maxPos = i names[maxPos], names[len(names)-1] = names[len(names)-1], names[maxPos]
51162
def sum_list(list):
sum = 0 for i in list:
sum += i
return sum
Section 7.9  
51700
t = ()
51701
t = (42, 56, 7)
51702
len(t)
51703
t[0]
51704
k = t[0]
51705
t[k]
51706
play_list = list(t)
51707
t = tuple(play_list)
51708
tmp = list(t)  
tmp.sort()  
t = tuple(tmp)
51007
repeats = 0
if len(t) != 0 :
    i=1
    while i<len(t) :
        if t[i] == t[0] :
            repeats += 1
        i += 1
MPL Extra  
51257
odds = list(range(1, n+1, 2))
51258
odds = list(range(m, n+1, 2))
51262
new_list = [i for i in my_list if i % 2 == 0]
51264
negatives = [i for i in numbers if i < 0]
51266
factors= [i for i in range(2, n) if n % i == 0]
51267
factors= [i for i in range(n-1, 1, -1) if n % i == 0]