We offen use loop statements for repeating statements. One of the most frequently used expression is while loop statements too.
Python “While Loop” Examples
Example 1: For every time the while loop runs, the value of the counter is increased by 2 until counter reach on 100.
1 2 3 4 5 6 7 8 |
#www.python-examples.com counter = 0 while counter <= 100: print (counter) counter=counter + 2 |
Example 2: The sum of the numbers from 1 to 100 with while loop in Python
1 2 3 4 5 6 7 8 9 10 11 |
#www.python-examples.com sumOfNumbers = 0 counter = 1 while counter <= 100: sumOfNumbers = sumOfNumbers + counter counter += 1 print("Sum of 1 until 100: %d" % (sumOfNumbers)) |
Example 3: Spelling the words
1 2 3 4 5 6 7 8 9 10 11 |
#www.python-examples.com yourName=input("Enter your name") counter=0 while counter < len(yourName): print(yourName[counter]) counter += 1 else: print("I spelled your name") |