Python While loops (With Examples)

Learn to perform iterations until a condition holds True in Python using while loops with examples.

Python While loops (With Examples)

The while statement in Python repeatedly executes a block of code as long as a test at the top keeps evaluating to True. It is referred to as a loop because control keeps looping back to the beginning until the test becomes False.

When the test becomes false, the code block following the while block is executed. If the test is false, to begin with, the body never runs.

Topics Covered

  1. Syntax
  2. Printing a statement forever
  3. Remove elements from a list until empty
  4. Take user inputs
  5. Problems to solve

Syntax

while <test>: # Loop test
    <code block> # Loop body
else: # Optional else
    <code block> # Run if didn't exit loop with break

Example 1 - Printing a statement forever

Code

while True:
    print("Press Ctrl-C to exit this while loop")

Output

Press Ctrl+C to exit this while loop
Press Ctrl+C to exit this while loop
Press Ctrl+C to exit this while loop
Press Ctrl+C to exit this while loop
Press Ctrl+C to exit this while loop
Press Ctrl+C to exit this while loop
Press Ctrl+C to exit this while loop
Press Ctrl+C to exit this while loop
Press Ctrl+C to exit this while loop
Press Ctrl+C to exit this while loop
Press Ctrl+C to exit this while loop
Traceback (most recent call last):
  File "some_file_location", line 2, in <module>
    print("Press Ctrl-C to exit this while loop")
KeyboardInterrupt

The above code will run forever as long as it is not interrupted. From the article on booleans, you should know that True is the boolean representation of integer 1. Similarly, False is the boolean representation of 0.

You can also use integer representations of booleans in while loops.

Code

# The code block won't be executed
while 0:
    print("It won't be executed!")

while 1:
    print("It will be executed. Press Ctrl-C to exit")

Output

It will be executed. Press Ctrl-C to exit
It will be executed. Press Ctrl-C to exit
It will be executed. Press Ctrl-C to exit
It will be executed. Press Ctrl-C to exit
It will be executed. Press Ctrl-C to exit
It will be executed. Press Ctrl-C to exit
Traceback (most recent call last):
  File "some_file_location", line 5, in <module>
    print("It will be executed. Press Ctrl-C to exit")
KeyboardInterrupt

Example 2 - Remove elements from a list until empty

This example demonstrates how to pop an element from a list until it is empty.

Code

x = [1, 2, 3]

while len(x) > 0:
    popped_elem = x.pop()
    print(f"The removed element is {popped_elem}")
    print("Now the list is", x)
    print()
else:
    print("The list is empty")

Output

The removed element is 3
Now the list is [1, 2]

The removed element is 2
Now the list is [1]

The removed element is 1
Now the list is []

The list is empty

As long as len(x) is greater than 0, the while block is executed. Once all the elements are removed from the list, the test returns False. Python then moves on the else block.

Note:- The empty print() statement within the while loop is used to create an empty line for better readability of results. To learn more about different variations of print statements, check out this article.


Example 3 - Taking user inputs

This example demonstrates the usage of user inputs with while loops to execute a certain task.

For e.g., the current example demonstrates, asking users to enter the names of their friends and terminate when the user presses q. Ultimately, show the list of friends entered by the user.

Code

# Set a variable to check if user wants to input
user_will_input = True
friends = []

while user_will_input:
    friend_name = input("Enter your friend's name ==> ")
    if friend_name != 'q':
        # Add to friends, only if user doesn't enter "q"
        friends.append(friend_name)
    else:
        # Set to False, if user enters "q"
        user_will_input = False
else:
    if len(friends) > 0:
        print(f"Your friends are {','.join(friends)}")
    else:
        print("You have no friends!")

Output

Enter your friend's name ==> Sonali
Enter your friend's name ==> Chinmayee
Enter your friend's name ==> Ashish
Enter your friend's name ==> q
Your friends are Sonali,Chinmayee,Ashish

The above code uses the join() method. Check out the linked article to learn what it does.


Problems

  1. Write a Python program to convert the input_list to output_list.

    input_lst = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    
    output_lst = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
    
  2. Write a Python program to solve the Fizbuzz problem

  3. Write a program to find the greatest common divisor (GCD) or highest common factor (HCF) of two numbers.

  4. Solve this Goldman Sachs Coding Interview Question

Subscribe to Pylenin

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe