Categories
Python

Quick Intro to Python Loops

Spread the love

Python is a convenient language that’s often used for scripting, data science, and web development.

In this article, we’ll look at various kinds of loops that we can use in Python apps to run repeated code.

while Loop Statements

We can use the while loop to run code repeatedly while a condition is True .

It consists of the while keyword, a condition to evaluate, a colon, and then the code to run indented below it.

For example, we can write the following while loop to print a message repeatedly:

x = 0  
while x < 5:  
    print('Hello.')  
    x = x + 1

In the code above, we have x set to 0. Then we use the while loop to print ‘Hello.’. Next, we increment x by 1. We do this repeatedly until x reaches 5.

while loops are useful for looping keeping the loop running until we meet a condition. It doesn’t have to have a finite, determinate amount of iterations.

For example, we can use the while loop until the user guesses the right number as follows:

guess = 0  
while int(guess) != 5:  
  print('Guess a number')  
  guess = input()  
print('You got it')

In the code above, as long as guess doesn’t evaluate to 5 when we convert it to an integer, the while loop will keep running.

Once we entered the right guess, which is 5, the loop will end.

break Statements

The break keyword is used to terminate a loop before the loop ends.

For example, we can rewrite the example above, with break instead of the condition in the while loop as follows:

guess = 0  
while True:  
  if int(guess) == 5:  
    break  
  print('Guess a number')  
  guess = input()  
print('You got it')

In the code above, we have an infinite while loop that has the condition to end the loop with break when we int(guess) returns 5.

The rest of the code works the same way as before.

continue Statements

We can use the continue statement to move on to the next iteration of the loop.

For example, we can use it as follows:

x = 0  
while x < 5:  
  x = x + 1  
  if x == 2:  
    continue  
  print(x)

The code above prints the value of x if it’s not 2. This is because if x is 2, we run continue to skip to the next iteration.

Truthy and Falsy Values

Python has the concept of truthy and falsy values. Truthy values are automatically converted to True when we use them where we have condition checks.

Falsy values are converted to False when we use them for condition checks.

0, 0.0, and '' (the empty string) are all considered False , while all other values are considered True .

For example, we can write a program to prompt users to enter a name and won’t stop until they enter one as follows:

name = ''  
while not name:  
  print('Enter your name:')  
  name = input()  
print('Your name is', name)

In the code above, we use not name to check if name is an empty string or not. If it is, then we keep showing 'Enter your name.' until they enter one.

Once they did, we display the last line with the name .

for Loops and the range() Function

We can use the for loop to loop through a certain number of items.

For example, we can use the for loop with the range function to display numbers from 0 to 4 as follows:

for i in range(5):  
    print(i)

In the code above, the range function returns integers starting from 0 as we and increments the number as we loop up to the number passed into the range function minus 1.

As we can see, the for loop consists of the for keyword, a variable name, the in keyword, a call to the range function, a colon , and then the block of code to run in the loop.

We can also use break and continue statement inside for loops as we did in while loops.

The range function can take 3 arguments, where the first is the starting number and the 2nd argument is the ending number. The loop will terminate when it reaches the ending number minus 1.

The 3rd argument is the increment to increase the variable by in each iteration.

For example, we can write the following code to print all odd numbers between 1 and 10:

for i in range(1, 10, 2):  
    print(i)

We should see:

1  
3  
5  
7  
9

printed because in our range function call, we passed in 1 as the starting number, 10 as the ending number, and 2 to increment i by 2 in each iteration.

Conclusion

We can use while loops to repeatedly run a block of code until a condition is met. This means the loop can run an indeterminate number of iterations.

break is used for ending a loop before it ends. continue is used to skip the loop to the next iteration.

for loops are used for repeatedly run code a finite number of times. It’s used with the range function to do the finite iteration.

Leave a Reply

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