Mastering Python Loops - A Challenge on Conditional Logic

Create a loop that iterates numbers 1 through 10, squaring even numbers and cubing odd numbers, then printing each result.

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, challenge. I want you to try this please. Do your loop.

Loop the nums list or you get the nums list can be looped as nums for n in nums or since nums was the result of range 1 to 11, you can just directly say for n in range 1 to 11. These are 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. We're going to, I'd like you to do 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. But 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 9 because 3 cubed is, oops, that would be 27, right? 3 times 3 times 3. Then we get 16 because 4 squared is 16. Then we would get 125 because 5 times 5 times 5 is 125. Hint, use the modulus operator to check if n is even or odd.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

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 the program. The teacher would give me these assignments, and I couldn't do them, these little five-minute challenges, and I couldn't do them. 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 was 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, right? 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 to 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 mod 2 equals 0, right? If you take n and you 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, even number, every time.

So that is our odd even check. Odd being if that's false. You don't even have to check the 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 else n squared.

Else, we're going to print, right, else n is odd, right? It can only be the other. In which case, we're going to cube it. We're going to print n cubed here, right? So let's look at, 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 to 11, we're going 10 times. Iterate from 1 to 10.

If n divided by 2 gives a remainder of 0, the very definition if n is 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 have to, we learn all these things so that we can use them later, so that 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 here? And 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 fruits. F is a little short.

You could say for fruit and fruits, but what I don't like to call the current list item by the 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 1 to the end of the length of the list.

Brian McClain

Brian is an experienced instructor, curriculum developer, and professional web developer, who in recent years has served as Director for a coding bootcamp in New York. Brian joined Noble Desktop in 2022 and is a lead instructor for HTML & CSS, JavaScript, and Python for Data Science. He also developed Noble's cutting-edge Python for AI course. Prior to that, he taught Python Data Science and Machine Learning as an Adjunct Professor of Computer Science at Westchester County College.

More articles by Brian McClain

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram