Learn how to use Python while loops with clear examples, syntax rules, and best practices.
Syntax:
while condition:
# code block
Here, condition is a boolean expression that is evaluated before each iteration of the loop. If the condition is True, the code block is executed. The loop continues to execute as long as the condition remains True.
video: Python while loop example - Learn how to use while loop
Question: Write a Python program to print the numbers from 1 to 10, using a while loop?
count = 1 # Start counting at 1
while count <= 10: # Keep counting as long as we're less than or equal to 10
print(count) # Print the current number
count += 1 # Add 1 to the count for the next round
Question: Write a Python program to print the string “Hello, world!” 5 times, using a while loop?
i = 1
while i <= 10:
print('Hello, world!')
i += 1
Question: Write a Python program to calculate the sum of the even numbers from 2 to 20 using a while loop.
sum = 0 # Initialize a variable to store the sum
number = 1
while number <= 20:
if number%2 == 0:
sum += number # Add the current number to the sum
number += 1
print(f'The sum of even numbers from 1 to 20 is: {sum}')
Question: Write a Python program to prompt the user to enter lines of text until the user enters a blank line. The program should then display the message “You entered a blank line.”.
inputStr = 'Start'
while inputStr != "":
inputStr = input("Enter a line of text:")
print('You entered a blank line.')
Question: Write a Python program to add all the numbers entered by the user until the user enters zero. The program should display the sum of the numbers.
sum = 0; # Initialize the sum
# Prompt the user to enter a number
number = int(input('Enter a number: ')) # int() Convert string input to integer
# While the number entered is not zero, add the number to the sum and prompt the user to enter another number
while number != 0:
sum += number
number = int(input('Enter another number: ')) # int() Convert string input to integer
# Display the sum of the numbers
print(f'The sum of the numbers is: {sum}')
Video: Learn how to use INFINITE while loop
x = 1
while True:
print("To infinity and beyond! We're getting close, on %d now!" % (x))
x += 1
Description:
Write a Python program that asks the user to input a number and displays its multiplication table up to 10 using a while loop.
Input:
Output:
n x 1 = n
n x 2 = 2n
...
n x 10 = 10n
Example:
Input:
Enter a number: 5
Output:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Requirements:
while loop to generate and display the multiplication table.Write a Python program that calculates the sum of all integers from 1 to 100. The program should use a while loop to iterate through these integers and accumulate their sum.
Input:
i to 1 and a variable sum to 0.i is less than or equal to 100.Expected Output:
Sum = {sum}Example Output:
Sum = 5050
Explanation:
This code uses a while loop to iterate through the integers from 1 to 100.
In each iteration, the current value of i is added to the variable sum, and i is incremented by 1.
After the loop completes, the total sum is printed.
Write a Python program that calculates and prints the square of the numbers from 1 to 4. The program should use a while loop to iterate through these numbers.
Input:
i to 1 and continues looping while i is less than 5.Expected Output:
Square of {i} is {square}Example Output:
Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Explanation:
This code uses a while loop to calculate the square of the variable i,
which starts at 1 and increments by 1 in each iteration until it reaches 5.
The squares of the numbers 1 through 4 are printed during each iteration.
Write a Python program that prompts the user to enter numbers. The program should keep accepting numbers until the user enters a negative number. Once a negative number is entered, the program should stop and display the sum of all the numbers entered (excluding the negative number).
Sample Input:
Enter a number (negative number to stop): 10
Enter another number (negative number to stop): 20
Enter another number (negative number to stop): 5
Enter another number (negative number to stop): -1
Sample Output:
The sum of all numbers is: 35
for loop in Python is a programming statement that repeats a block of code a fixed number of times.
👉 Learn moreelse clause can be used with loops (for and while).
👉 Learn morematch-case is a modern way to handle data-driven decision-making.
👉 Learn moreTutorials, Roadmaps, Bootcamps & Visualization Projects