# Python While Loop

Python WhileViews 928

For , If and While are the primitive compound statements/loops in  Python. In this tutorial, we will be covering only the Python while statement.

## Python While Loop:

Python while loop is used to execute a set of statements as long as the condition is true. The below code prints the numbers from 1 to 10.

```a=1
while(a<=10):
print(a)
a+=1
print("Numbers 1 to 10 are printed")```

The above code works as below:

• Initializing variable a to 1
• While statement checks if the variable ‘a’ is less than or equal to 10
• If the condition is True, ‘a’ will be incremented by 1.
• When the last line has encountered, the condition of the while statement will be tested again.
• So the above code prints all the numbers from 1 to 10.
• When ‘a’ becomes 11, the condition becomes false and the loop will be exited.
• Whatever the statement was written outside the while loop will be executed.

### Infinite loop:

We have to be careful about the infinite loop while using the Python ‘while’ statement. In case, we forgot to increment the variable ‘a’, it will result in an infinite loop.

```a=1
while(a<=10):
print(a)
print("Numbers 1 to 10 are printed")```

### Python While and ELSE:

With the else statement we can run a block of code once when the condition given in the while statement becomes false.

```a=11
while(a<=10):
print(a)
a+=1
else:
print("a is greater than 10")```

### Looping through Lists:

We can loop through lists, strings, and tuples using the while loop.

```l=[8,9,1,4,5]
length=len(l)
i=0  # since the index starts with zero
while(i<length):
print(l[i])
i+=1```

### IF inside while loop:

We can also use the IF loop inside the while loop. The below code is to print the even numbers from 1 to 10.

```a=1
while(a<=10):
if a%2==0:
print(a)
a+=1```

You can modify the above code to print the odd numbers. Try more examples on while loops.

Translate »
{"ft_lang_label":"__label__en","ft_lang_prob":0.8501444,"math_prob":0.92034,"size":1797,"snap":"2023-40-2023-50","text_gpt3_token_len":460,"char_repetition_ratio":0.16452873,"word_repetition_ratio":0.041269843,"special_character_ratio":0.27434614,"punctuation_ratio":0.10997442,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9979459,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-04T15:37:15Z\",\"WARC-Record-ID\":\"<urn:uuid:44d2fdf3-67ee-4944-a165-7f9994eb31d9>\",\"Content-Length\":\"131701\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5ec9074e-2d6f-4b2b-b836-ea901d8c2ac2>\",\"WARC-Concurrent-To\":\"<urn:uuid:6086b2de-e71a-4c1e-91b6-ee797360a9b6>\",\"WARC-IP-Address\":\"172.67.168.78\",\"WARC-Target-URI\":\"https://tutorialcup.com/python/python-while-loop.htm\",\"WARC-Payload-Digest\":\"sha1:4A7CDNGGN567CJGXFAXF236CUXSV5YCM\",\"WARC-Block-Digest\":\"sha1:VNFSGWTTRKXXHP66HEK6FLUU2NXFMKHK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100531.77_warc_CC-MAIN-20231204151108-20231204181108-00669.warc.gz\"}"}