HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

10) Conditionals and Loops Lesson

Introduction: Python Loops

7 min to complete · By Martin Breuss

Earlier in this section, you learned how to use conditional statements to influence the flow of execution of your code. With the keywords if, elif, and else you were able to make only some lines of your code execute, while others didn't.

There are more possibilities to influence what code gets executed and when. The second important concept that you'll learn about in this section is called looping.

What is a Python Loop

Looping allows you to execute lines of code repeatedly.

Could you please repeat that?

Sure thing :)

Loops in programming allow you to repeat lines of code according to certain conditions. Similar to how you used expressions to decide which of your conditional statements should execute, you can use code to decide how often certain lines of code should run.

Tasks

What's a real-life example of a loop? Post your thoughts in Discord.

Loop Examples

You use loops when you want to do something more than once in a row. Time to work through an example to use this concept in practice. Below, you'll learn how to use a loop to do an action on each character of a string.

Character Collections

Think back to when you learned about strings. When you read over the documentation for Python's string methods, you may have discovered a method called .isalpha().

If you haven't, feel free to go and look it up right now. As the documentation explains, the method does what its name suggests:

Return True if all characters in the string are alphabetic and there is at least one character, False otherwise.

You know that strings are collections of characters. Now add to that knowledge that loops are especially useful for collections!

What if you could check out each character of your string separately, call a string method on it to figure out whether it is an alphabetic character or not, and then do something with it? Turns out this is possible to do using loops, so get ready to give it a spin!

Decipher A Cipher Using A Loop

Your friend sent you a secret message and told you that the text is hidden in between a bunch of random numbers:

secret = "2349H30023388281E3299371l1l3094842O0333322883"

You could just read over the string and figure out where the characters are hiding. But Python can significantly speed up that process for you! Also, if you write a script that decodes this message, then you can use it for any future secret conversations that your friend might send your way.

Colorful illustration of a light bulb

Note: Don't worry if you don't yet fully understand the code shown below. You'll learn about the syntax of loops in the upcoming lessons.

Here's a way you can decipher the coded message using Python and a for loop:

secret = "2349H30023388281E3299371l1l3094842O0333322883"
solution = ""

for char in secret:
    if char.isalpha():
        solution += char

print(solution)

In this code snippet, you used a conditional statement with .isalpha() on the secret variable to pick out only the alphabetic characters and assemble them into a solution message. Finally, you're printing this message to your console output.

Colorful illustration of a light bulb

Info: You can see that you're combining a couple of different concepts that you've learned about in the course up to now. You'll see that adding loops to your skillset will considerably increase what you can do.

Try to run the code snippet in your text editor and see what the decoded message outputs.

Tasks

  • Play around with your script.
  • Create a new message and see whether your script can handle it as expected.

As you can see, using a loop to decode this message took a lot less manual work. Instead of looking at each character yourself, you let Python tackle the task in no time at all!

In the next lesson, you'll pick apart the syntax for building a for loop and practice with additional examples.

Summary: Introduction to Python Loop

  • A Python loop is a block of code that is repeated under a certain condition
  • Loops are very useful and extremely used in code
  • Deciphering a text message where each character is surrounded by many numbers is an example where a loop can be used