Team Tech Talk: Iteration

Group members: Marcus, Hao, Ameer, Kayden

What is Iteration? In coding, "iteration" means to repetitively perform a set of actions or operations, typically with the purpose of processing or examining a collection of items, such as a list, array, or other data structures. It is a fundamental concept in programming and is often used when you need to perform a specific task multiple times or when you want to process a collection of data, such as a list or an array. Below is an example of an iteration, and its uses. ```python for number in range(5): print(number) ``` 0 1 2 3 4
  • for "number" in range(5): - This line initiates a for loop. The loop variable "number" is used to iterate through a sequence of numbers generated by the range(5) function. range(5) generates a sequence of numbers from 0 to 4, which means the loop will run 5 times.
  • print(number) - Inside the loop, this line prints the value of the loop variable "number" to the console. Since "number" takes on the values from the range(5) sequence, it will print the numbers 0, 1, 2, 3, and 4 in separate lines.

    You can also create an iteration using words or a list. Examples below. ```python fruits = ["apple", "banana", "cherry", "date", "fig"] for fruit in fruits: for number in range(3): print(fruit) ``` apple apple apple banana banana banana cherry cherry cherry date date date fig fig fig In this example, the input was a list of fruits, which is then put into the code where the fruits will be printed and repeated 3 times, because of the iteration of "for number in range(3)" keyword "range(3)"

    Popcorn Hack #1 now create your own iteration, wheter that be a list of numbers or words, its up to you. (3 minutes) ```python oreos = ["creme betweens, oreos, kaliedeos, twist and shout"] for oreos in oreos: for number in range(3): print(oreos) ``` creme betweens, oreos, kaliedeos, twist and shout creme betweens, oreos, kaliedeos, twist and shout creme betweens, oreos, kaliedeos, twist and shout

    Indefinite Iteration Indefinite iteration, also known as conditional or while-loop iteration, involves repeating a block of code as long as a specified condition remains true. Unlike definite iteration, where you know the exact number of iterations in advance, with indefinite iteration, the loop continues as long as the condition is met. If the condition becomes false at any point, the loop terminates. Below is an example of the layout ```python while condition: # Loop body ``` File "/tmp/ipykernel_1658/2079778900.py", line 2 # Loop body ^ SyntaxError: incomplete input
  • condition: This is a boolean expression. The loop will continue executing as long as this condition is True.
  • #Loop body: This is the block of code that gets executed during each iteration. It can include one or more statements.

    Below is an example code of indefinite iteration: ```python while True: user_input = input("Enter something (type 'exit' to quit): ") if user_input == "exit": break print("You entered:", user_input) ``` You entered: hello You entered: what
  • We use a "while True" loop, which means the loop will run indefinitely until we explicitly exit it.
    </br>
  • Inside the loop, we ask the user for input and store it in the user_input variable.
    </br>
  • We checked if the user input is equal to "exit." If it is, we use the break statement to exit the loop.
    </br>
  • If the input from the user is not "exit," we print what they entered. We can see this through the code ("You entered:", user_input)
    </br>
  • This code will keep asking for input and printing it until the user types "exit," at which point the loop will terminate.

    Loop Control Statements Loop control statements are used to manage the flow of loops and determine when to exit, skip, or repeat iterations within a loop. There are three primary loop control statements:
  • (Break) - Exit a Loop Prematurely:
    </br> break is used to exit a loop when a specific condition is met. Example: Exit a loop when the number 5 is encountered. ```python for i in range(10): if i == 5: break print(i) ``` 0 1 2 3 4
  • (Continue) - Skip the Current Iteration:
    </br> continue is used to skip the current iteration of a loop and move to the next. Example: Skip printing the number 2 in a loop. ```python for i in range(5): if i == 2: continue print(i) ``` 0 1 3 4
  • return (in Functions) - Exit a Function and Loop:
    </br> In functions, return is used to exit the function and any loop within it. Example: Exit a function and loop when a target value is found. ```python def calculate_rectangle_area(length, width): area = length * width return area length = 5 width = 3 area = calculate_rectangle_area(length, width) print("The area of the rectangle is:", area) ``` The area of the rectangle is: 15 Above is more of a complex iteration, but we use the return statement to specify that the result of the function is the value stored in the (area) variable. # Homework! create a code for each of the different scenarios below:
  • a list of numbers that repeats (10 times)
    </br>
  • a list of words (use your creativity)
    </br>
  • a hack using indefinite iteration. You could ask the user for a password, number, phrase, or something that will initiate the exit. Example below. ```python while True: user_input = input("Enter something (type 'exit' to quit): ") if user_input == "exit": break print("You entered:", user_input) ``` ```python numberlist = 1, 2, 3, 4, 5 for numberlist in numberlist: for number in range(10): print(numberlist) ``` 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 ```python grouplist = ["will, nihar, jayden, howie"] for grouplist in grouplist: for number in range(5): print(grouplist) ``` will, nihar, jayden, howie will, nihar, jayden, howie will, nihar, jayden, howie will, nihar, jayden, howie will, nihar, jayden, howie ```python while True: user_input = input("Enter something (type 'exit' to quit): ") if user_input == "will is the best": break print("You entered:", user_input) ``` You entered: hi You entered: password You entered: 1234