<jupyter_start><jupyter_text>Spotted a mistake? Report it here  Beginner's Python—Session Five Template  Looping Through a Range  Introduction Use a for loop to print the numbers zero to four<jupyter_code>for counter_variable in range(5):
    print(counter_variable)<jupyter_output>0
1
2
3
4<jupyter_text>Use a for loop to print the numbers two to five<jupyter_code>for i in range(2, 6):
    print(i)<jupyter_output>2
3
4
5<jupyter_text>Standard Puzzles Use a for loop to print the number -5 to 5, inclusive<jupyter_code>for i in range(-5, 6):
    print(i)<jupyter_output>-5
-4
-3
-2
-1
0
1
2
3
4
5<jupyter_text>Print the first ten square numbers<jupyter_code>for x in range(1, 11):
    print(x, "squared is", x ** 2)<jupyter_output>1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
6 squared is 36
7 squared is 49
8 squared is 64
9 squared is 81
10 squared is 100<jupyter_text>Bonus Puzzles Experiment with passing a third argument into `range(...)`. What does it do?<jupyter_code>for i in range(1, 11, 2):
    print(i)<jupyter_output>1
3
5
7
9<jupyter_text>The third argument controls what step size to increase by, defaulting to one. Note, that we still don't include the second argument of `range()` in the output. Use this technique to count down from ten to zero<jupyter_code># from 10 (inclusive) to -1 (exclusive), increasing by -1 each time
for n in range(10, -1, -1):
    print(n)<jupyter_output>10
9
8
7
6
5
4
3
2
1
0<jupyter_text>Looping Through a List  Introduction Create a shopping list and print out each item in a sentence<jupyter_code>shopping_list = ['eggs', 'bacon', 'sausages', 'spam']
for item in shopping_list:
    print("Don't forget to buy", item)<jupyter_output>Don't forget to buy eggs
Don't forget to buy bacon
Don't forget to buy sausages
Don't forget to buy spam<jupyter_text>Standard Puzzles Calculate the product of the elements in a list (see the presentation for a similar example)<jupyter_code>numbers = [2, 4, 2, 5]
running_total = 1
for n in numbers:
    running_total *= n
print("Product of items is", running_total)<jupyter_output>Product of items is 80<jupyter_text>Use a for loop to count how many positive elements a numeric list contains<jupyter_code>numbers = [3, -1, 3, -4, 0]
count = 0
for n in numbers:
    if n > 0:
        count += 1
print("There are", count, "positive elements")<jupyter_output>There are 2 positive elements<jupyter_text>Early-stopping  Introduction Print the elements of a list until a negative number is encountered, at which point you should stop<jupyter_code>numbers = [4, 3, -1, 2, 7]
for n in numbers:
    if n < 0:
        print("Negative number found...stopping")
        break
    print(n)<jupyter_output>4
3
Negative number found...stopping<jupyter_text>Print the elements of a list, skipping any negative values<jupyter_code>numbers = [4, 3, -1, 2, 7]
for n in numbers:
    if n < 0:
        print("Negative number found...skipping")
        continue
    print(n)<jupyter_output>4
3
Negative number found...skipping
2
7<jupyter_text>Standard Puzzles Create a list as specified in the presentation<jupyter_code>my_list = [4, 3, 0, 2, -1, 4]<jupyter_output><empty_output><jupyter_text>Loop through the list, printing only positive elements and halting when a negative entry is encountered<jupyter_code>for item in my_list:
    if item == 0:
        continue
    if item < 0:
        break
    print(item)<jupyter_output>4
3
2<jupyter_text>Bonus Puzzles Setup up a nested for loop and attempt to break when a condition is met in the inner loop<jupyter_code>stop = False
for x in range(1, 6):
    for y in range(1, 6):
        print("x =", x, "| y =", y,
              "| x + y =", x + y)
        if x + y > 7:
            stop = True
            break
    if stop:
        break<jupyter_output>x = 1 | y = 1 | x + y = 2
x = 1 | y = 2 | x + y = 3
x = 1 | y = 3 | x + y = 4
x = 1 | y = 4 | x + y = 5
x = 1 | y = 5 | x + y = 6
x = 2 | y = 1 | x + y = 3
x = 2 | y = 2 | x + y = 4
x = 2 | y = 3 | x + y = 5
x = 2 | y = 4 | x + y = 6
x = 2 | y = 5 | x + y = 7
x = 3 | y = 1 | x + y = 4
x = 3 | y = 2 | x + y = 5
x = 3 | y = 3 | x + y = 6
x = 3 | y = 4 | x + y = 7
x = 3 | y = 5 | x + y = 8<jupyter_text>Enumeration  Introduction Use enumeration to print out the elements of a list alongside their ordinal indices<jupyter_code>agendum = ['meeting', 'one-on-one',
           'lunch', 'research']
print("Today's schedule:")
for index, item in enumerate(agendum):
    print(str(index + 1) + ')', item)<jupyter_output>Today's schedule:
1) meeting
2) one-on-one
3) lunch
4) research<jupyter_text>Standard Puzzles Define the list shown in the presentation<jupyter_code>numbers = [1, -2, 2, 5]<jupyter_output><empty_output><jupyter_text>Print whether each element is greater than, equal to, or less than its index<jupyter_code>for i, n in enumerate(numbers):
    if i < n:
        print("Index less than element")
    elif i == n:
        print("Index equal to element")
    else:
        print("Index greater than element")<jupyter_output>Index less than element
Index greater than element
Index equal to element
Index less than element<jupyter_text>Use enumeration to print every other element of a list<jupyter_code>numbers = [5, 2, 9, 7, 3, 4]
for i, n in enumerate(numbers):
    if i % 2 == 0:
        print(n)<jupyter_output>5
9
3
{'fraction_non_alphanumeric': 0.10236664162283997, 'fraction_numerical': 0.03831705484598046, 'mean_word_length': 3.562982005141388, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '532161', 'license': 'MIT', 'n_tokens_mistral': 2043, 'n_tokens_neox': 1853, 'n_words': 741, 'path': 'session-five/session_five_filled_template.ipynb', 'repo_name': 'Precel2000/beginners-python', 'size': '5324'}