Python Tutorial v3.2.5 Lesson 23.7 - Random Items from a List

Опубликовано: 12 Октябрь 2024
на канале: Programming Tutorials by Steve
9,293
67

1:20 Skip the Introduction to Lesson 23.7
2:10 Creating a function using the random.shuffle() approach.
8:30 Creating a function using the len(list) and .pop(idx) approach.
18:55 Skip directly to the Lesson 23.7 Challenge Program.

Lesson 23.7 introduces two different methods of pulling random items out of a list. The first bases itself on the random.shuffle(list) method. While the simpler of the two methods, it has the disadvantage of constantly randomizing the entire list even though only a single element is pulled out. If the order of the list is important to your program, then this method should be avoided. The second uses the len(list) function to find valid index locations, then uses list.pop(index) to pull out the element. This has the advantage of not reordering the list each time an item is pulled out. In either case, pulling random items out of a list can be an important programming skill.

This is an introductory series of Python tutorials. This course, from start to finish, is designed to help someone who has never programmed before learn the basics of coding in Python. As this series continues, we examine more advanced Python techniques, functions, and methods.

You can download a copy of Python from http://www.python.org/downloads/.

Keep in mind this tutorial is using an older version of Python, v3.2.5. You will need to click on the "View Older Releases" button to use this specific version. Using the newer versions will not be an issue at this point, but when the lesson proceeds to basic graphics, the Pygame Module we'll use does not support v3.3+ at this time.