Python While — Loop

Mayuresh shewale
2 min readMar 2, 2022

--

Loops are a set of instructions that run repeatedly until a condition is met.

A while loop will always first check the condition before running.

If the condition evaluates to True then the loop will run the code within the loop's body.

Flowchart

1]

number = 0
while number < 10:
print(f"Number is {number}!")
number = number + 1

Output:

Number is 0!
Number is 1!
Number is 2!
Number is 3!
Number is 4!
Number is 5!
Number is 6!
Number is 7!
Number is 8!
Number is 9!

2]

# Program to add natural
# numbers up to
# sum = 1+2+3+…+n

# To take input from the user,
#n = int(input(“Enter no: “))

n = 10

# initialize sum and counter
sum = 0
i = 1

while i <= n:
sum = sum + i
i = i+1
# update counter

# print the sum
print(“The sum is”, sum)

When you run the program, the output will be:

Enter n : 10

The sum is 55

--

--