Files
coding-mentor-agent/kb/python-course-kb-practical-python/wiki/exercises/2-14-more-sequence-operations.md
T

1.4 KiB
Raw Blame History

id, source_exercise_id, title, section, source_path, source_repo, source_commit, student_visible_solution, has_private_solution, skip
id source_exercise_id title section source_path source_repo source_commit student_visible_solution has_private_solution skip
practical-python-2.14 2.14 More sequence operations 2.4 Sequences 02_Working_with_data/04_Sequences.md https://github.com/dabeaz-course/practical-python 93dca856b41c61a0a0f85ae334116e4c125629ea false false false

Exercise 2.14: More sequence operations

Source: Practical Python Programming, 02_Working_with_data/04_Sequences.md.

Exercise 2.14: More sequence operations

Interactively experiment with some of the sequence reduction operations.

>>> data = [4, 9, 1, 25, 16, 100, 49]
>>> min(data)
1
>>> max(data)
100
>>> sum(data)
204
>>>

Try looping over the data.

>>> for x in data:
        print(x)

4
9
...
>>> for n, x in enumerate(data):
        print(n, x)

0 4
1 9
2 1
...
>>>

Sometimes the for statement, len(), and range() get used by novices in some kind of horrible code fragment that looks like it emerged from the depths of a rusty C program.

>>> for n in range(len(data)):
        print(data[n])

4
9
1
...
>>>

Don’t do that! Not only does reading it make everyone’s eyes bleed, it’s inefficient with memory and it runs a lot slower. Just use a normal for loop if you want to iterate over data. Use enumerate() if you happen to need the index for some reason.

关联来源