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
- Iteration: A loop in a piece of code that repeats intself until a certain condition is met
- 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")
for i in reversed(range(1,11)):
print(i)
numbers = 0
n = 1
while numbers < 80:
numbers = 3+(n-1)*13
n = n + 1
print(numbers)
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)
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")
Food = ['Carrot', 'Broccli', 'Apple']
OverallCost = 0
for element in Food:
print(OverallCost + 1)