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有三次方的意思,開始按照語法寫,出現(xiàn)錯誤,看了問答,原來是三次方?jīng)]寫。

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ì)
x >=30 少了=號,不認(rèn)真看題,只看到包括70,沒注意30也包括在里面

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容