Python 6.now you try

1.use a list comprehension to create a list,cubes_by_four
2.the comprehension should consist of cubes of the numbers 1 through 10 only if the cube is evenly divisible by four.
3.finally,print that list to the console.

cubes_by_four =[cube ** 3 for cube in range(1,11) if cube**3 % 4 == 0]
print cubes_by_four

cube有三次方的意思,开始按照语法写,出现错误,看了问答,原来是三次方没写。

14.try it

1.create a list,squares,that cosists of the squares of the numbers 1 to 10.a list comprehension could be useful here.
2.use filter() and a lambda expression to print out only the squares that are between 30 ad 70(inclusive).

syntax:
cubes =[x **3 for x in range(1,11)]
filter (lambda x: x % 3 == 0,cubes)

hint:
you'll want to filter for x >=30 and x <=70

squares = [x ** 2 for x in range(1,11)]
print filter(lambda x: x >=30 and x <=70,squares)

print 没写,看题目不仔细
x >=30 少了=号,不认真看题,只看到包括70,没注意30也包括在里面

你可能感兴趣的:(Python 6.now you try)