# python_exercise4_list_部分

#(a)
def list_printer(aList):
print(aList)
for x in aList:
print(x,end= ' ')
print()
#(b)
def list_printer_r(aList):
rList = aList[::-1]
print(rList)
for x in rList:
print(x,end = ' ')
print()
#(c)
def my_len(aList):
counter =0
for x in aList:
counter = counter +1
return counter

aList = [1,2,3,4,5,6,7,8]
list_printer(aList)
list_printer_r(aList)
print('the len of aList is '+str(my_len(aList)))

#(a)
a = [1,2,3,4,5,6,7,8]
b = a
b[1] = 9999
print('now print a -> ',end ='')
print(a)
c = a[:]
c[2] = -666
print('now print a -> ',end ='')
print(a)

def set_first_elem_to_zero(l):
l[1] = 0

set_first_elem_to_zero(a)
print('now print a -> ',end ='')
print(a)

a = [[]] * 3
b = [[] for _ in range(3)]
for i in a:
print(i,end = ' ')
print()

for x in b:
print(x,end = ' ')
print()

def list_and_func(aList,index):
aList[index] = 0

aList = [1,2,3,4,5]
print(aList)
list_and_func(aList,3)
print(aList)

import math
def count_prime(n,aList):
if(n >= 2 ):
aList.append(2)
if(n >= 3):
aList.append(3)
for x in range(4,n+1):
if((x-1)%6 !=0 and (x+1)%6 != 0 ):
continue
flag = True
for i in range(2,int(math.sqrt(n))):
if(x%i == 0):
flag = False
if(flag == True):
aList.append(x)
else:
flag = True

def count_prime_2(n,aList):
if(n >= 1):
aList.append(2)
if(n >= 2):
aList.append(3)
num_found = 2
x = 5
while(True):

if((x-1)%6 !=0 and (x+1)%6 != 0 ):
x = x+1
continue

flag = True
for i in range(2,int(math.sqrt(n))):
if(x%i == 0):
flag = False
if(flag == True):
num_found = num_found +1
aList.append(x)
if(num_found == n):
break
else:
flag = True
x = x+1

aList = []
count_prime(99,aList)
bList = []
count_prime_2(5,bList)
print(aList)
print(bList)

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120