Learn Python For Beginners

Loops In Python

Loops in python are a common tool when writing software. You could use loops to do things like print out every element in a list in python, run a function a certain number of times and much more. This is a lesson in a series for learning python for beginners and if you haven’t been following along so far make sure you understand the fundamentals of python. While loops are a perfect place to start.

While Loops In Python

A while loop is a loop that will keep running until a certain condition is met. I think the idea is easy to understand and implement. We can write a program that counts to a certain number would look like this:

The while loop executes a block of code until the condition is met and then terminates. You can terminate a while loop early by giving a secondary condition in nested python if statements.

Loops In Python

Even though the initial condition to be met is for i to be 25 for the loop to terminate the if statement is true and the break statement runs python knows to stop the loop. Conditions can be skipped in the loop with the continue statement.

Loops In Python

Notice the value 5 wasn’t printed out. The continue statement stops the loop at the if statement and then restarts the loop without breaking it. The while loop is just one of the multiple approaches to loops in python. There is also a for loop for iterating through data.

For Loops

For loops are better suited to iterate over a piece of data. Taking the lessons in lists in python a program that loops through a list and prints out the elements one-by-one would look like this:

Loops In Python

As the loop iterates through the list the ‘stuff’ variable becomes Spider-Man, then the number 7, then the nested list and so on. For loops can iterate over strings as well. It may be helpful for you to know that strings are essentially lists of characters in python.

Loops in python are an efficient way to repeat logic. It’s possible to print all elements of a list by printing each one by its index. For loops are an easier to read way to do that. I use loops on a daily basis. They are an important tool in python. For loops can also use the range function. The range function returns a sequence of numbers. We can combine for loops and the range function like this:

Loops In Python

In this way for loops and while loops are similar. You can determine exactly how many times to repeat the logic. I find myself using for loops much more frequently than while loops. This is because with for loops it’s easier to access the data you are iterating through. That doesn’t mean that while loops are useless. I use them frequently when writing GUI programs. Video games also seem to require while loops more often. Understanding what situation is best for each comes with practice. For now, get excited. We are about to start learning about functions in python. Learning functions can change the way you think about writing software.

Leave a Comment

Your email address will not be published. Required fields are marked *