Explore loops and string methods, two foundational programming concepts that automate repetitive tasks and enhance string manipulation. Understand how loops work, and how string methods can simplify your coding workflow.
Key Insights
- Understand loops as repeating blocks of code that continue executing as long as a Boolean condition remains true, with the "for" loop being a common approach to iterate over ranges or lists.
- Discover that iterables like lists and ranges can be looped over directly, removing the need to explicitly convert them into lists, streamlining your code.
- Review common Python string methods and how they differ from list methods like append or sort, expanding upon earlier lessons from Noble Desktop's programming tutorials.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
This is a lesson preview only. For the full lesson, purchase the course here.
In this lesson, we're going to look at loops. Every programming language under the sun has what are called loops—a routine, a code block that will run like a hamster wheel again and again, as long as the condition is true. The loop is dependent on something being true in the Boolean sense for the code to repeat.
And then there's some kind of change going on—some value changing—typically with each iteration, as it's called, of the loop so that ultimately the condition that's keeping the loop going finally becomes false so that the loop can stop. This is important, of course, because you don't want to have what is known as an infinite loop. In fact, that's Apple's address. I don't know if it still is, but for years, the Apple Corporation's address was One Infinite Loop Way, Cupertino, California.
So infinite loops are famous—and to be avoided. We're going to look at loops in this lesson. Let's dive right in—also into string methods.
We looked a lot in Lesson One at methods that are called on lists: `append`, `extend`, `pop`, `insert`, `sort`, `reverse`, and others. And in this lesson, we're going to look at methods that are called on strings. I made my backup file, `04prog`.
You'll want to get into `04start` unless you'd also like to have a backup file. We're going to open it up. A loop is code that repeats as long as the condition is true. The condition could also be something like "until I run out of numbers."
Let's say I have a range of numbers from 1 to 10 and go through them one by one. When I get to the end of the range, the loop stops. A loop could also go through an array or a list. For example, we could go item by item through a list of fruits—starting at apple and ending with the last fruit, like watermelon.
Once you've gotten through the entire list, you stop. So the condition could be just "start at the beginning and go to the end, " or there could be other conditions. The most common kind of loop is known as a `for` loop.
A `for` loop has the word `for` in it. We're going to get into the syntax of that. A couple of other terms to know:
When we looked at a range in the last lesson, we'll be using that to generate a range of numbers we want to loop through. You can iterate over a list of fruits or pets.
A list having multiple items is called an iterable. Anything you can run a loop over—like going from the beginning to the end—is known as an iterable. Therefore, lists are considered iterables because you can loop through them—you can iterate over them.
All right, we're going to need a couple of modules here. Let's import: we're going to import `random`, which you remember from the last lesson, and we'll import `pprint` (PrettyPrint) as `pp`. Now we're going to define a range.
If you remember from the last lesson as well, if you want the numbers from 1 to 10, you would use `range(1,11)` because the stop index is exclusive, right? So start at 1, and you'll go to 10 because 11 is not included. Let's run it. And if you recall, I also mentioned last time that we have to unpack the range.
The numbers are there, but they’re packed up like in a suitcase—you can't see them. So convert the range to a list (listify), unpack it, and run that. And there you go—there’s 1 to 10.
Now that we have a range—a list of numbers—we have something we can loop through. We can start at 1 and go to 10. As we loop, we’ll have access to every single item in the list, one by one, represented as a variable that we’ll name.
Part of the loop syntax is that when you set up a loop, you give the current item a variable name. What should I call 1? What should I call 2? Since it’s repeating (same code running repeatedly), you'd use the same variable name each time—such as `X` or `n` when dealing with numbers. So here’s the syntax. Let’s run a loop. We're going to say `for`, and we're going to say `n`. `n` will represent our current number as we iterate.
As we loop through these 10 numbers, each number—one by one—will be referred to as `n`. The first time the loop runs, `n` will be 1. The second time, `n` will be 2, and so forth. Because it's iterating over a range (a list), it will know when to stop. The stopping of the loop is built in—it stops when it gets to the end. There you go.
Now typically, if all you wanted were 10 numbers to loop over, you wouldn’t need to make an actual list. You could feed `range(1,11)` directly into the `for` statement. Let’s make a new code block out of that. Oh, by the way, you can clear your output by selecting and clearing selected outputs.
So here, let’s make a new box. We’ll call it `2B`. We’ll just say it’s the same as above, but we iterate the range directly.
No need to make an actual list from `range`, right? You don’t have to. Just take this exact code and, instead of iterating over your `nums` list, iterate over `range(1,11)`. You do not need to convert it to a list or unpack. It’ll work directly.
And there you go—it still works. That’s more typical, right? You just want to go 10 times.
If I want to do something 10 times, I just say `range(1,11)`. Now let’s make one more box—omit the start index.
The start argument from `range` will default to starting at 0. If we just take out the 1, it’s going to start from 0 now. So just know that. But I only want to go 10 times though, right? Okay, so your 10 times won’t be 1 to 10 anymore like it was here.
It’ll have to be 0 to 9. So you’d lower your range’s upper limit. This would be your end value. If you omit the start value and just have one argument—like `range(end_value)`—then the start value will default to 0. You see that a lot, because if you just want to do something 10 times and don’t even care what the number is, you just want to loop, it’s easier.
But if the number itself matters—like you're going to use it inside the loop—then you might need to pick your start and end point. You could start at 11 and go to 20.
So that might matter. And inside the loop, you can do whatever you want. You can include conditional logic. We’re just literally printing `n`, but you can do all kinds of stuff—as this lesson is all about showing you that.