How to make this Block of python code short and efficient

后端 未结 11 2509
天命终不由人
天命终不由人 2020-12-15 16:48

I am total newbie to programming and python. I was solving a problem. I found the solution but it seems like too slow.

    if n % 2 == 0 and n % 3 == 0 and\\         


        
11条回答
  •  盖世英雄少女心
    2020-12-15 17:38

    if all(n % i == 0 for i in range(2, 21)):
    

    all accepts an iterable and returns True if all of its elements are evaluated to True, False otherwise. The n % i == 0 for i in range(2, 21) part returns an iterable with 19 True or False values, depending if n is dividable by the corresponding i value.

提交回复
热议问题