CHAPTER 2 · 24 lessons · 306 exercises

Loops, and where your data lives

Repeat the same work over and over, and hold many values at once. This is where a program starts to be useful.

0 / 306 solved

The programs in the last chapter had one big limit: they could only handle as many values as you had lines. Three people's data means three lines; a hundred people means a hundred. You cannot build anything real that way.

This chapter knocks that wall down, with two tools: repeating, and containers that hold a lot at once.

Repeating starts with for. Python's for is less "count to a number" and more "take the things in a row, one at a time". Write for fruit in fruits: and each item in the list arrives in fruit in turn. When you do want a fixed number of goes, you bring in range. The other one is while, which means "for as long as this holds". Known number of goes, for; the end decided by a condition, while.

Somewhere in here you will meet the infinite loop. Get the ending condition wrong and the program never stops. This course cuts it off after a while and tells you what happened, so do not be shy — make one on purpose. Once you can explain to yourself why it will not end, you have understood it.

For containers we start with the list. Write ["apple", "orange", "grape"] and several values sit in order under one name. You take them out by a number that starts at zero, like fruits[0]. Zero rather than one always throws people at first. Python also lets you write fruits[-1] for the last one, which is a kindness most languages do not offer.

After that you will see that a string works the same way. name[0] is the first character, name[0:3] the first three. The indexing and slicing you learned on lists carry straight over. Python is built so that "things in a row" all behave alike, and noticing that cuts down enormously on what you have to remember.

The other container is the dictionary. If a list is a container you index by number, a dictionary is one you look up by name. Write {"name": "Yui", "age": 15} and pull it out with person["name"]. It suits anything made of several named parts — a person, a shop, a product. We will cover tuples too, the container you cannot change afterwards.

The real point is putting the two together. Line dictionaries up in a list and you can represent actual data — a register, a product list — as it is. Put a loop round that and the same code handles a hundred records or a thousand. The chapter ends with a piece of work that uses loops, branching and lists all at once.

Lessons in this chapter

Repeat the same work

  1. 21Repeating a set number of timesNo more writing the same line three times. for and range hand the repeating over to the program.Go to the exercises
  2. 22Taking things out of a rowMeet the list, which holds values in a row, and take them out one at a time with for. This is what Python's for really is.Go to the exercises
  3. 23Repeating while a condition holdswhile is repeating where the end is decided by a condition. Moving on to the next go becomes your job.Go to the exercises
  4. 24The loop that never endsA program that will not stop is not something to fear; it is something to read. Learn to say out loud why it never ends.Go to the exercises
  5. 25Stopping early, and skipping one gobreak calls a loop off and continue skips one go. You can start giving a loop finer instructions.Go to the exercises
  6. 26A loop inside a loopWrite a loop inside a loop. Times tables, grids, combinations — this one shape builds all of them.Go to the exercises

Line values up in a list

  1. 27Lining values up in a list[...] puts values in a row. You stop having to make one variable after another.Go to the exercises
  2. 28Getting one by numberfruits[0] grabs a single item. Counting starts at 0 — that is the Python way.Go to the exercises
  3. 29Swapping the contents aroundName a number and write over it. append adds one, remove and pop take one away.Go to the exercises
  4. 30Counting how manylen() tells you how many are in at a glance. It also lets you loop over the numbers.Go to the exercises
  5. 31Putting them in ordersorted returns a new list; sort changes the one you have. That difference is the whole lesson.Go to the exercises
  6. 32Cutting out a partxs[1:3] takes several at once. The end number is not included — that is the rule.Go to the exercises

More with strings

  1. 33Taking characters out by numberThe [0] you learned on lists works on strings unchanged. A string is a row of things too.Go to the exercises
  2. 34Cutting out a pieceThe slicing you learned on lists works on strings unchanged. Only changing one behaves differently.Go to the exercises
  3. 35Searching inside a stringIs it in there, where does it start, how many are there. Before you take things out by number, you look for them.Go to the exercises
  4. 36Working on a stringupper, lower, strip, replace. Every one of them returns a new string and leaves the original as it was.Go to the exercises
  5. 37Splitting up and joining backsplit turns a string into a list, join turns a list into a string. Going both ways makes life far easier.Go to the exercises

Look values up by name

  1. 38A container you look up by nameA list took things out by number. A dictionary takes them out by name. You will be writing person["name"] by the end.Go to the exercises
  2. 39Change it, add to it, take things outChanging and adding are the same shape. del removes, and in and get keep KeyError away.Go to the exercises
  3. 40Taking them out one at a timeHand a dictionary to for and what comes round is the keys. Learn items and you get the key and the value together.Go to the exercises
  4. 41Lining dictionaries up in a listA register or a product list is dictionaries lined up in a list. It is the shape that real data falls into.Go to the exercises
  5. 42A group you cannot change afterwardsThe shape (150, 3), very like a list. The difference is that you cannot change it, and that turns out to be reassuring.Go to the exercises

Put it together

  1. 43Putting the tools togetherA piece of work that uses loops, branching and lists at once. It is a round-up, so there are no basics — ten questions only.Go to the exercises
  2. 44Holding it in dictionaries insteadLine dictionaries up in a list and handle real data — a register, a product list. No basics here either, only the ten.Go to the exercises

Next up is “Functions”. Give a chunk of work a name so you can call it any time. This is where your code turns into parts.