<jupyter_start><jupyter_text>Python loops  While loop While loop executes a set of statements as long as a condition is true.<jupyter_code># print i as long as i is less than 5
i = 0
while i < 5:
    print(i)
    i += 1<jupyter_output>0
1
2
3
4<jupyter_text>Remember to increment i, or the loop will continue forever. The `break` statement: we can stop the loop even if the while condition is true:<jupyter_code># Exit the loop when i is 3
i = 0
while i < 5:
    i += 1
    if i == 3: 
        break
    print(i)
print('Outside the loop')<jupyter_output>1
2
Outside the loop<jupyter_text>The `break` statement broke the loop. We finished it after 2 iterations. The `continue` statement: we can stop the current iteration, and continue with the next<jupyter_code># continue to the next iteration if i is 3
i = 0
while i < 5:
    i += 1
    if i == 3:
        continue
    print(i)
print('Outside the loop')<jupyter_output>1
2
4
5
Outside the loop<jupyter_text>Notice that we never printed 3, because we used there the `continue` statement. The `else` statement: we can run a block of code once when the condition no longer is true:<jupyter_code># Print a message once the condition is false:
i = 0
while i < 5:
  print(i)
  i += 1
else:
  print("i is no longer less than 5")<jupyter_output>0
1
2
3
4
i is no longer less than 5<jupyter_text>For loop A  `for` loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.<jupyter_code># Print each name in the students list
students = ['John', 'Mary', 'Anna']
for s in students:
    print(s)<jupyter_output>John
Mary
Anna<jupyter_text>Looping through a string Strings are iterable objects, they contain a sequence of characters.<jupyter_code>for s in 'string':
    print(s)<jupyter_output>s
t
r
i
n
g<jupyter_text>The `break` statement stops the loop before it has looped through all the items.<jupyter_code># Exit the loop when s is 'Mary'
students =  ['John', 'Rose', 'Mary', 'Anna']
for s in students:
    if s == 'Mary':
        break
    print(s)<jupyter_output>John
Rose<jupyter_text>The `continue` statement stops the current iteration of the loop, and continue with the next.<jupyter_code>students =  ['John', 'Rose', 'Mary', 'Anna']
for s in students:
    if s == 'Mary':
        continue
    print(s)<jupyter_output>John
Rose
Anna<jupyter_text>Notice that in this case the foor loop printed all the name but Mary. **range()**: returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default).<jupyter_code>for x in range(5):
    print(x)
# print the values from 2 to 5-1
for x in range(2,5):
    print(x)
# increment the sequence by 2 (default is 1)
for x in range(2,10,2):
    print(x)
# increment the sequence by 10 
for x in range(3,45,10):
    print(x)<jupyter_output>3
13
23
33
43<jupyter_text>`else` in For Loop: The else keyword in a for loop specifies a block of code to be executed when the loop is finished.<jupyter_code># Print all numbers from 0 to 5 
# Print a message when the loop ends
for x in range(6):
  print(x)
else:
  print("Finally finished!")<jupyter_output>0
1
2
3
4
5
Finally finished!<jupyter_text>Nested for loops<jupyter_code># print each adjective for every student
adj = ['smart', 'polite']
students = ['John', 'Mary', 'Anna']
for a in adj:
    for s in students:
        print(a,s)
# Two dice
for i in range(6):
    for j in range(6):
        print((i+1,j+1))<jupyter_output>(1, 1)
(1, 2)
(1, 3)
(1, 4)
(1, 5)
(1, 6)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(2, 5)
(2, 6)
(3, 1)
(3, 2)
(3, 3)
(3, 4)
(3, 5)
(3, 6)
(4, 1)
(4, 2)
(4, 3)
(4, 4)
(4, 5)
(4, 6)
(5, 1)
(5, 2)
(5, 3)
(5, 4)
(5, 5)
(5, 6)
(6, 1)
(6, 2)
(6, 3)
(6, 4)
(6, 5)
(6, 6)<jupyter_text>The `pass` statement `for` loops cannot be empty, but if you for some reason have a for loop with no content, put in the `pass` statement to avoid getting an error.<jupyter_code># The pass statement: 
for x in [0, 1, 'h']:
  pass<jupyter_output><empty_output><jupyter_text>**Iterating through a Dictionary**<jupyter_code>d1 = {1:'a', 2:'b', 3:'c'}
type(d1)
for k in d1:
    print(k)<jupyter_output>1
2
3<jupyter_text>As you can see, when a for loop iterates through a dictionary, the loop variable is assigned to the dictionary’s keys.To access the dictionary values within the loop, you can make a dictionary reference using the key as usual:<jupyter_code>for k in d1:
    print(d1[k])<jupyter_output>a
b
c<jupyter_text>We can use `d1.keys()` to iterate over every key in the dictionary:<jupyter_code>for k in d1.keys():
    print(k)<jupyter_output>1
2
3<jupyter_text>We can use d1.values() to iterate over every value in the dictionary:<jupyter_code>for k in d1.values():
    print(k)<jupyter_output>a
b
c
{'fraction_non_alphanumeric': 0.11372141372141373, 'fraction_numerical': 0.03575883575883576, 'mean_word_length': 4.156484458735263, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 50, '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': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '216083', 'license': 'MIT', 'n_tokens_mistral': 1904, 'n_tokens_neox': 1775, 'n_words': 723, 'path': '02-Basic Python/08-Loops.ipynb', 'repo_name': 'Goliath-Research/Introduction-to-Data-Science', 'size': '4810'}