Help with 11 practice makes perfect i dont understand


help me please. ineed a valubal answer


is thid correct

to_21 = range(1,22)

odds = to_21[::2]

middle_third = to_21[7:14]

print to_21
print odds
print middle_third


and bye coz i need to go to sleep its midnight


to_21 = range(1,22) doesn’t create a list with the numbers 1 to 21 to do that you could use list(range(1,22)) or to_21 = [x for x in range(1,22)]. The rest is fine.


In Python 2 it does. This site uses Python 2.7.x.

is Python 3.x.

List comprehensions are not covered until the Advanced Topics unit.


You’re right @mtf. I forgot they’re using Python 2. Mainly because I searched for the exercise in question and could not find it because I couldn’t remember how it was done in the exercise. I know it’s there somewhere. But anyway thanks for reminding me. @digitalninja71913 it’s right.


As it turns out, this IS from the advanced topics module.


Well spotted @mtf. Although it wouldn’t have helped me, I used a list comprehension.


The beauty of programming with a language such as Python (or JS or Ruby, etc.) is that there are multiple ways to accomplish the same end. Some may be more elegant, some more efficient.

For the above problem, range and list slicing is all that’s needed. The OP is using the simplest method.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.