Chapter 11. Collections
This chapter covers
- Arrays
- The for statement and loop
- Dictionaries
- Creating a state-name lookup app
Almost every app you’ll ever write will need some way to store data. You already learned about storing data in variables in chapter 8, but sometimes you need to store several pieces of data together in a grouping of some sort. This is where the idea of collections comes in—a collection of values that can be stored together. Imagine if you went to the grocery store and had to buy one egg at a time instead of a nice dozen packaged together. It would be a bit annoying, for sure. This chapter covers two different collection types (egg cartons) to store your data.
The first kind of collection that we’ll discuss is called the array. Let’s go back to the egg carton example—but this time, the eggs are numbered 0 through 11 (remember, in the programming world, counting starts with 0). The eggs were put in the carton in order, starting with egg 0 and ending with egg 11. The only way to get the eggs out of the carton is also in order. If you want to start cooking and you need an egg, you must start with the first egg—or to write it in a programming way, you want egg[0]. When you want the second egg, you get egg[1], and so on.