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.
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
- 21Repeating a set number of timesNo more writing the same line three times.
forandrangehand the repeating over to the program.Go to the exercises - 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'sforreally is.Go to the exercises - 23Repeating while a condition holds
whileis repeating where the end is decided by a condition. Moving on to the next go becomes your job.Go to the exercises - 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
- 25Stopping early, and skipping one go
breakcalls a loop off andcontinueskips one go. You can start giving a loop finer instructions.Go to the exercises - 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
- 27Lining values up in a list
[...]puts values in a row. You stop having to make one variable after another.Go to the exercises - 28Getting one by number
fruits[0]grabs a single item. Counting starts at 0 — that is the Python way.Go to the exercises - 29Swapping the contents aroundName a number and write over it.
appendadds one,removeandpoptake one away.Go to the exercises - 30Counting how many
len()tells you how many are in at a glance. It also lets you loop over the numbers.Go to the exercises - 31Putting them in order
sortedreturns a new list;sortchanges the one you have. That difference is the whole lesson.Go to the exercises - 32Cutting out a part
xs[1:3]takes several at once. The end number is not included — that is the rule.Go to the exercises
More with strings
- 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 - 34Cutting out a pieceThe slicing you learned on lists works on strings unchanged. Only changing one behaves differently.Go to the exercises
- 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
- 36Working on a string
upper,lower,strip,replace. Every one of them returns a new string and leaves the original as it was.Go to the exercises - 37Splitting up and joining back
splitturns a string into a list,jointurns a list into a string. Going both ways makes life far easier.Go to the exercises
Look values up by name
- 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 - 39Change it, add to it, take things outChanging and adding are the same shape.
delremoves, andinandgetkeepKeyErroraway.Go to the exercises - 40Taking them out one at a timeHand a dictionary to
forand what comes round is the keys. Learnitemsand you get the key and the value together.Go to the exercises - 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
- 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
- 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
- 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