3.8-3.10- Iteration/Lists

  • Iteration: a repeating portion of an algorithm, repeats a specified number of times or until a given condition is met
  • Iteration Statement: change the sequential flow of control by repeating a set of statements zero or more times, until a stopping condition is met
  • Repeat Until: if the condition evaluates to true initially, the loop body is not executed at all, due to the condition being checked before the loop
  • We can use stopping conditions to stop an iteration if a condition is already met
  • Iteration Statement - cause statements to be executed zero or more times, subject to some loop-termination criteria
  • The two different types of loops are for loops and while loops
  • The break command stops the list at the designated value
  • Traversing a list is the process of visiting each element in a list in a sequential order. It can be used to access, search for, and modify elements in the list.
  • Complete Traversal: All elements in a list are assessed
  • Partial Traversal: Only a given portion of elements are assessed
  • Iterative Traversal: When loops are used to iterate through a list and to access each single element at a time.

Hack 3.8.1

  1. Iteration: A loop in a piece of code that repeats intself until a certain condition is met
  2. A college searching app. Serach the college you are looking for on the website. Ask if has the major you want. If not then repeat serach if it does procede. Ask if the tuition is expensive. Ff it is repeat serach if it is not then complete code.
teams = ["Bengals", "Cowboys", "Texans", "Saints"]
for i in teams:
    if i == "Saints":
        print(i + " is found")
        break
    else:  
        print(i + " are not Saints")
Bengals are not Saints
Cowboys are not Saints
Texans are not Saints
Saints is found

Hack 3.8.2

  1. An Iteration statment is like an iteration but still different. It repeats a loop until this loop is commanded to stop by a condition. It stops intead of proceeding.
for i in reversed(range(1,11)):
        print(i)
10
9
8
7
6
5
4
3
2
1
numbers = 0 
n = 1
while numbers < 80:
    numbers = 3+(n-1)*13
    n = n + 1
    print(numbers)
3
16
29
42
55
68
81

Section 10 Notes

nums = [10, 15, 20, 25, 30, 35]
mnums = min(nums)
for i in nums:
    if i > mnums:
        mnums = mnums + 5
    else:
        mnums = mnums + 5
print(mnums)
40

Quiz

Vocab

  • Iteration Statements: change the sequential flow of control by repeating a set of statements zero or more times, until a stopping condition is met
  • Repeat Until: if the condition evaluates to true initially, the loop body is not executed at all, due to the condition being checked before the loop
  • For Loop: a programming language conditional iterative statement which is used to check for certain conditions and then repeatedly execute a block of code as long as those conditions are met.
  • While Loop: used to repeat a specific block of code an unknown number of times, until a condition is met.
  • List: a predefined list from which some statistical coded concepts take their values
  • Traversing Lists: visiting each node of the list once in order to perform some operation on that.
  • List Operations: The different operations you can perform on a list such as addition.
# While Loop
Money = 30
Burger = 0
while Money >= 10:
    print('Here is your burger')
    Money = Money - 10
    Burger = Burger + 1
print("You have", Burger, "burgers")
Here is your burger
Here is your burger
Here is your burger
You have 3 burgers
Food = ['Carrot', 'Broccli', 'Apple']
OverallCost = 0
for element in Food:
    print(OverallCost + 1)
1
1
1