<jupyter_start><jupyter_text>A for loop executes commands once for each value in a collection.*   A *for loop* tells Python to execute some statements once for each value in a list,    a character string,    or some other collection.*   "for each thing in this group, do these operations"<jupyter_code>for number in [2, 3, 5]:
    print(number)
# this is equivalent to
print(2)
print(3)
print(5)<jupyter_output>2
3
5<jupyter_text>The first line of the for loop must end with a colon, and the body must be indented.*   The colon at the end of the first line signals the start of a *block* of statements.*   Python uses indentation rather than `{}` or `begin`/`end` to show *nesting*.    *   Any consistent indentation is legal, but almost everyone uses four spaces.<jupyter_code>for number in [2, 3, 5]:
print(number)<jupyter_output><empty_output><jupyter_text>Indentation is always meaningful in Python.<jupyter_code>firstName="Jon"
  lastName="Smith"<jupyter_output><empty_output><jupyter_text>A for loop is made up of a collection, a loop variable, and a body.*   The collection, `[2, 3, 5]`, is what the loop is being run on.*   The body, `print(number)`, specifies what to do for each value in the collection.*   The loop variable, `number`, is what changes for each *iteration* of the loop.<jupyter_code>for number in [2, 3, 5]:
    print(number)<jupyter_output>2
3
5<jupyter_text>The body of a loop can contain many statements.Long `for` loops can be hard to understand.<jupyter_code>primes = [2, 3, 5]
for p in primes:
    squared = p ** 2
    cubed = p ** 3
    print(p, squared, cubed)<jupyter_output>2 4 8
3 9 27
5 25 125<jupyter_text>Use `range` to iterate over a sequence of numbers.*   The built-in function `range` produces a sequence of numbers.    *   *Not* a list: the numbers are produced on demand        to make looping over large ranges more efficient.*   `range(N)` is the numbers 0..N-1    *   Exactly the legal indices of a list or character string of length N<jupyter_code>print('a range is not a list:', range(0, 3))
for number in range(0,3):
    print(number)<jupyter_output>a range is not a list: range(0, 3)
0
1
2<jupyter_text>Use an accumulator to turn many values into one.*   A common pattern in programs is to:    1.  Initialize an *accumulator* variable to zero, the empty string, or the empty list.    2.  Update the variable with values from a collection.<jupyter_code># Sum the first 10 integers.
total = 0
for number in range(10):
   total = total + (number + 1)
print(total)<jupyter_output>55<jupyter_text>Exercise 1: Reversing a StringFill in the blanks in the program below so that it prints “nit” (the reverse of the original character string “tin”).    original = "tin"    result = ____    for char in original:        result = ____    print(result)<jupyter_code>original = "tin"
result = ""
for char in original:
    result = char + result
print(result)<jupyter_output>nit<jupyter_text>Exercise 2: Practice AccumulatingFill in the blanks in the program below to print the total length of strings in the list     Total length of the strings in the list:    total = 0    for word in ["red", "green", "blue"]:        ____ = ____ + len(word)    print(total)<jupyter_code># Total length of the strings in the list:
total = 0
for word in ["red", "green", "blue"]:
    total = total + len(word)
print(total)<jupyter_output>12<jupyter_text>Exercise 3Fill in the blanks in the program below to print the a list of length of each string in the list    lengths = ____    for word in ["red", "green", "blue"]:        lengths._____(____)    print(lengths)<jupyter_code>lengths = []
for word in ["red", "green", "blue"]:
    lengths.append(len(word))
print(lengths)<jupyter_output>[3, 5, 4]<jupyter_text>Exercise 4Fill in the blanks in the program below to print a concatenated string of all words in the list.    words = ["red", "green", "blue"]    result = ____    for ____ in ____:        ____    print(result)<jupyter_code>words = ["red", "green", "blue"]
result = ""
for word in words:
    result = result + word
print(result)<jupyter_output>redgreenblue<jupyter_text>Exercise 5Create an acronym by taking the first letter of each word and converting to uppercase : ["red", "green", "blue"] => "RGB". **write the whole thing**<jupyter_code>words = ["red", "green", "blue"]
result = ""
for word in words:
    result = result + word[0].upper()
print(result)<jupyter_output>RGB
{'fraction_non_alphanumeric': 0.10161034248128828, 'fraction_numerical': 0.016557042413245633, 'mean_word_length': 4.281437125748503, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 40, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '569105', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1455, 'n_tokens_neox': 1382, 'n_words': 569, 'path': 'notebooks/complete/10_For_Loops.ipynb', 'repo_name': 'utplanets/PHY224PythonReview', 'size': '4409'}