<jupyter_start><jupyter_text>While Loops The **while** loop is a way to repeatg a block of code as long as a specified condition is met. ``while :    code block``<jupyter_code>i = 0
while i < 5:
    print(i)
    i += 1<jupyter_output>0
1
2
3
4<jupyter_text>Note that there is no guarantee that a **while** loop will execute at all, not even once, because the condition is tested **before** the loop runs.<jupyter_code>i = 5
while i < 5:
    print(i)
    i += 1<jupyter_output><empty_output><jupyter_text>Some languages have a concept of a while loop that is guaranteed to execute at least once:``do    code blockwhile `` There is no such thing in Python, but it's easy enough to write code that works that way.We create an infinite loop and test the condition inside the loop and break out of the loop when the condition becomes false:<jupyter_code>i = 5

while True:
    print(i)
    if i >= 5:
        break<jupyter_output>5<jupyter_text>As you can see the loop executed once (and will always execute at least once, no matter the starting value of i.)This is a standard pattern and can be useful in a variety of scenarios.A simple example might be getting repetitive user input until the user performs and action or provides some specific value.<jupyter_code>For example, suppose we want to use the console to let users enter their name. We just want to make sure their name is at least 2 characters long, contains printable characters only, and only contains alphabetic characters:<jupyter_output><empty_output><jupyter_text>We might try it this way:<jupyter_code>min_length = 2

name = input('Please enter your name:')

while not(len(name) >= min_length  and name.isprintable() and name.isalpha()):
    name = input('Please enter your name:')

print('Hello, {0}'.format(name))<jupyter_output>Please enter your name:a123
Please enter your name:a
Please enter your name:fred
Hello, fred<jupyter_text>This works just fine, but notice that we had to write the code to elicit user input **twice** in our code. This is not good practice, and we can easily clean this up as follows:<jupyter_code>min_length = 2

while True:
    name = input('Please enter your name:')
    if len(name) >= min_length  and name.isprintable() and name.isalpha():
        break

print('Hello, {0}'.format(name))<jupyter_output>Please enter your name:a
Please enter your name:123
Please enter your name:fred
Hello, fred<jupyter_text>We saw how the **break** statement exits the **while** loop and execution resumes on the line immediately after the while code block.Sometimes, we just want to cut the current iteration short, but continue looping, without exiting the loop itself.This is done using the **continue** statement:<jupyter_code>a = 0
while a < 10:
    a += 1
    if a % 2:
        continue
    print(a)<jupyter_output>2
4
6
8
10<jupyter_text>Note that there are much better ways of doing this! We'll cover that in later videos (comprehensions, generators, etc) The **while** loop also can be used with an **else** clause!! The **else** is executed if the while loop terminated without hitting a **break** statement (we say the loop terminated **normally**) Suppose we want to test if some value is present in some list, and if not we want to append it to the list (again there are better ways of doing this): First, here's how we might do it without the benefit of the **else** clause:<jupyter_code>l = [1, 2, 3]
val = 10

found = False
idx = 0
while idx < len(l):
    if l[idx] == val:
        found = True
        break
    idx += 1
    
if not found:
    l.append(val)
print(l)<jupyter_output>[1, 2, 3, 10]<jupyter_text>Using the **else** clause is easier:<jupyter_code>l = [1, 2, 3]
val = 10

idx = 0
while idx < len(l):
    if l[idx] == val:
        break
    idx += 1
else:
    l.append(val)

print(l)
l = [1, 2, 3]
val = 3

idx = 0
while idx < len(l):
    if l[idx] == val:
        break
    idx += 1
else:
    l.append(val)

print(l)<jupyter_output>[1, 2, 3]
{'fraction_non_alphanumeric': 0.09107007336200354, 'fraction_numerical': 0.016190235264356184, 'mean_word_length': 4.075738125802311, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '189371', 'license': 'MIT', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1220, 'n_words': 555, 'path': 'FCP Review/A Quick Review/04 - The While Loop.ipynb', 'repo_name': 'Rafiei-tayebe/AP4002', 'size': '3953'}