<jupyter_start><jupyter_text>Chapter 5 - Loops A loop in terms of computing means to do something over and over again. Loops are one of those things that computers excel in. In Python, there are two ways to loop, a **While** loop and a **For** loop.  While loops A while loop cycles as long as the while condition is true. For example,```pythonwhile True:    print("Looping")```will never stop running because it will always be true. Once the condition is no longer true, the loop stops and goes to the next code block after the while block. If we want to prematurely exit a loop, we use the **break** keyword.<jupyter_code>while True:
    print("Looping")
    break<jupyter_output><empty_output><jupyter_text>We can see that in the first time the code loops, there is a break so it exits as soon as it gets to the break. If we want to loop again without going through the rest of the looping code block, we use keyword **continue**<jupyter_code># init
condition = 0

# loop
while condition != 5:
    print("Looping", condition)

    # making it odd
    if condition % 2 == 0:
        condition -= 1
        # anything after this continue will not be used
        print("Now odd...")
        continue
        print("hello")
        
    condition += 2<jupyter_output><empty_output><jupyter_text>For loops For loops are a power tool to loop through containers such as lists, sets, dictionaries, strings, and more Here are some examples:<jupyter_code>numbers = [1, 5, 8, 30, 42, 1023]
total = 0

# for loop adds all the numbers and prints them out individually
for number in numbers:
    print(number)
    total += number
    
print("Total:", total)
word = "mississippi"

for character in word:
    print(character)<jupyter_output><empty_output><jupyter_text>Another way this is useful is the use of range based for loops. That means it will go through a range of numbers (ie. you  can specify how many times it can loop)<jupyter_code>for i in range(5):
    print(i)<jupyter_output><empty_output><jupyter_text>If we want to go through a list and get the index value, we can use the **enumerate()** method<jupyter_code>for index, number in enumerate(numbers):
    print(f"The {index} number is {number}")<jupyter_output><empty_output><jupyter_text>For loops can also be handy with dictionaries. Since dictionaries have key-value pairs, we can loop through both at the same time, we can use the **items()** method<jupyter_code>pets = {
    "cat": 1,
    "dog": 3,
    "fish": 9
}

for animal, value in pets.items():
    if value != 1:
        animal += "s"
    print("We have", value, animal)<jupyter_output><empty_output><jupyter_text>Extra You may have seen the **in** keyword being used in the For loop section. You may assume that the keyword **in** only acts as a way to traverse through a list, but in can also be used to find if an item is in a list. For example;<jupyter_code>numbers = [1, 3, 4, 6, 8]

is_in_1 = 1 in numbers
is_in_9 = 9 in numbers

print(is_in_1, "1 is in the numbers list")
print(is_in_9, "9 is in the numbers list")<jupyter_output><empty_output><jupyter_text>Exercise 5 Ask the user for 2 words and using a for loop, reverse it and print it<jupyter_code># write your code below<jupyter_output><empty_output><jupyter_text>Chapter 4 Answer:<jupyter_code># first word
first_word = input("First word: ")
first_description = input("Word description: ")

# second word
second_word = input("Second word: ")
second_description = input("Word description: ")

# inits word dictionary
word_dict = {}

# adds word and description to dictionary
word_dict[first_word] = first_description
word_dict[second_word] = second_description

# prints dictionary
print(word_dict)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08897276894041521, 'fraction_numerical': 0.010784578053383662, 'mean_word_length': 4.431918008784773, 'pattern_counts': {'":': 3, '<': 37, '<?xml version=': 0, '>': 37, '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': '457817', 'license': 'MIT', 'n_tokens_mistral': 1156, 'n_tokens_neox': 1103, 'n_words': 482, 'path': 'Chapter 5 - Loops.ipynb', 'repo_name': 'Zeyu-Li/learn-coding', 'size': '3709'}