Dive into Python loops and conditional logic by squaring even numbers and cubing odd ones. Learn effective coding practices with clear examples and helpful explanations.
Key Insights
- Use a loop with conditional logic to iterate numbers 1 through 10, squaring even numbers (e.g., 4 from 2 squared) and cubing odd numbers (e.g., 27 from 3 cubed), employing the modulus operator to determine if numbers are even or odd.
- Build on previously learned Python fundamentals such as if-else statements and modulus operators to simplify complex tasks, which reinforces learning by incrementally introducing new concepts.
- Improve code readability by carefully choosing variable names when looping over lists, as demonstrated by naming the current item "fru" rather than "fruit" to distinguish clearly from the list "fruits."
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.
Next up is a challenge. I want you to try this, please. Do your loop.
Loop the nums list—or you can loop the nums list as `for n in nums`, or since `nums` was the result of `range(1,11)`, you can just directly say `for n in range(1,11)`. These give you the same result, right? These give you 1 to 10. But what I'd like you to do is every time the loop runs, don't just print `n`; square `n` so that you'll get 1,4, 9,16, etc.
And then a little extra curveball. I'd like you to add conditional logic inside here. You can do an if-else statement inside the loop.
As long as you're indented in the loop, you can do any code you like. So what I'd like you to do is check to see if the number is even, right? And if the current value of `n` is even, I'd like you to square it. Else, if `n` is not even, it would have to be odd, like 3 or 5. Then you would cube the value.
We'd get 1 because 1 cubed is 1. 4 because 2 squared is 4. Then we get 27 because 3 cubed is 27. Then we get 16 because 4 squared is 16. Then we would get 125 because 5 cubed is 125. Hint: use the modulus operator (%) to check if `n` is even or odd.
That's kind of difficult. We haven't done anything. I just showed you loops this minute, and you haven't seen anything other than printing the current `n` inside the loop.
So it's a bit of a stretch challenge, but you can tackle it. Even if you can't do it, here's the benefit: when I do show you the solution, even if you couldn't do it, the fact that you wrestled with it for a couple of minutes will make it click better.
I remember that from my time learning to program. The teacher would give me these assignments, and I couldn’t do these little five-minute challenges. But then when he explained it, I was like, aha! I had my little eureka moment that I wouldn't get if he just showed me straight out.
I remember those. I remember watching his little five-minute clock he would put on the screen, and I would just sit there and sweat and feel like I can't do this and kind of feel dumb and all that. But all these years later, I remember those stand out the most for me as my learning experience—those little challenges, even when I couldn't do them, because the explanations would really click with me.
All right. So that's my pep talk in telling you—don't get discouraged by these challenges. They're not even necessarily about you being able to do it as much as having the solution really click when you do see it.
So pause, tackle it, struggle with it, and we'll do the solution. And we're back. Okay.
So the challenge: loop the `nums` list, which was made from a range—or we could just loop the range. If `n` is even, square it. Else, if `n` is odd, cube it.
Print the result each time. Expected results. Okay.
Boom, boom, boom. All the way to 100 (10 squared). Hint: little percent sign (%).
That's your modulus hint. We're going to say we actually do need two print statements. Fine.
We're going to say `for n in range(1,11)`. That is the same. That's what we just did before, right? That'll print us the numbers 1 to 10 if we just print `n`, but we don't want to just print `n`. We're going to run an if-else block in here.
We're going to say `if`. We're going to say `if n is even`. Here, we can just make that our comment.
How do we check if `n` is even? We'll say `if n % 2 == 0`, right? If you take `n` and divide it by 2, and you get a remainder of 0, that's an even number, right? Remember what modulus does: it takes the number on the left, divides it by the number on the right, and gives you the remainder. And the remainder of an even number is always 0 when you divide that even number by 2, right? 64 divided by 2 is 32—an even number every time.
So that is our odd/even check. Odd being if that's false. You don't even have to check for odd, right? It's just if it's not even.
That's the only other choice. If `n` is even, we're going to square it. We're going to print `n ** 2`.
Else, `n` is odd, in which case we’re going to cube it. We're going to print `n ** 3`. So let's maybe move the comments away a little bit so we can focus on the code.
So here we are: `for n in range(1,11)`—we're going 10 times. Iterate from 1 to 10.
If `n` divided by 2 gives a remainder of 0—the very definition of even—we're going to square `n`, printing the result every time. Else, it's not even. So it's odd.
And the instruction was: if it's odd, you're going to cube the number. Let's go ahead and run it. And there it is.
So you can do whatever you like inside. And this is just a little built-in review of if-else and the modulus operator, right? We always learn all these things so that we can use them later—so we can focus on a new thing, right? We're focusing on loops right now. Imagine if we had never seen modulus or if-else and I was trying to teach you all three of them at once.
I've seen teachers do stuff like that. It is not a good idea. You have to teach one thing at a time that builds logically on the previous things.
So hopefully this is making some sense. All right, good job. Let's go on to the next.
Let's declare a little list here. Our by-now familiar fruit list, right? Yeah, all right, we can leave this—no spaces. We're going to do a fruit loop.
Get it? You see what I did there? In the fruit loop, we're just simply going to print the current fruit. It's just like printing `n`, except we probably don't want to call the current fruit `n`. We could say `for f in fruits` or `for fruit in fruits`. `f` is a little short.
You could say `for fruit in fruits`, but I don't like to call the current list item by a name that's very similar to the list itself. In other words, the only difference between the current fruit and the entire list is the "s". So this is a little harder to read. They look alike, right? So I like to kind of split the difference between one letter and the whole word.
And I'm going to call it `fru`. That's just sort of my signature name for my fruits in my fruit list. I call it `fru`.
We're going to print `fru` every time. And there it is. There's each `fru` printed, right? So for the current item, it's the same as with the numbers, right? `for n in range`—`n` is the current number as you go from 1 to 10.
`fru` is the current fruit as you go through the `fruits` list—not from 1 to 10, but from the beginning to the end of the list.