Section Objectives:

  • Express an algorithm that uses iteration without using a programming language
  • Determine the result or side effect of iteration statements
  • Write iteration statement

Section Vocabulary:

Iteration: a repeating portion of an algorithm, repeats a specified number of times or until a given condition is met

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.



i = 0
while (i < 4):
    print("Hello, World!")
    i = i + 1
Hello, World!
Hello, World!
Hello, World!
Hello, World!
i = 0
while (i < 3): #Try changing the 5 and see what happens!
    print("Hello, World!")
    i = i + 1
    if (i == 5): #Try Changing the 3 and see what happens!
        break
Hello, World!
Hello, World!
Hello, World!

Lesson Objectives

  • Determine the result or side effect of iteration statements
  • Learn how to use iterations with for loops and while loops
  • Creating code to learn how to incrementally list numbers
  • Learn the range function and using variables in code

Definition: Iteration Statement - cause statements to be executed zero or more times, subject to some loop-termination criteria

The first function we will learn is the range function, which we will use with for loop. As you may be able to guess, this will give us the sum based on the input provided. We always use a variable, such as i, to represent what the range of numbers the output will show. For example, if I wanted to list the numbers from 1-10 using the range function, it would look like this:

for i in range(7):
    print(i)
0
1
2
3
4
5
6

Now with while loops, we can provide a similar output with a variation in the input. Similar to for loops, it requires a variable which is the starting value.

i=1
while i<=10:
	print(i)
	i=i+1
1
2
3
4
5
6
7
8
9
10
pets = ["cat", "dog", "fish", "snake"]
for i in pets:
    if i == "fish":
        break
    print(i)
cat
dog

Hacks

Hacks 1

  1. Define an Iteration
  • An iteration is a repeating part of an algorithm, it repeats until a certain condition is met.
  1. Make your own example of an iteration with at least 4 steps and a stopping condition(Similar to mine that I did)
  2. Program a simple iteration.
2-3. 
letter = ["a", "b", "c", "d"]
for i in letter:
    if i == "c":
        break
    print(i)
a
b

Hacks 2

  1. What is an iteration statement, in your own words?
  2. Create a descending list of numbers using for loop
  3. Using while loop, make a list of numbers which will form an output of 3,16,29,42,55,68,81
  1. An iteration is a repeating part of an algorithm that keeps running until a certain condition is met.
2. 
i=10
while i>=0:
	print(i)
	i=i-1
10
9
8
7
6
5
4
3
2
1
0
3. 
name = ["George", "Luka", "Josh", "Ethan"]
for i in name:
    if i == "Josh":
        break
    print(i)
George
Luka

Secion 10

Section Objectives:

  • For list operations, write expressions that use list indexing and list procedures
  • For algorithms involving elements of a list, write iteration statements to traverse a list
  • For list operations, evaluate expression that use list indexing and list procedures
  • For algorithms involving elements of a list, determine the result of an algorithm that includes list traversals

Secion Vocabulary:

Traversing Lists: where all elements in the list are accessed, or a partial traversal, where only a portion of elements are accessed (can be a complete traversal)

What to know

  • List procedures are implemented in accordance with the syntax rules of the programming language

  • Iteration Statements can be used to traverse a list

  • !!! AP EXAM provides pseudocode for loops
  • Knowledge of existing algorithms that use iteration can help in constructing new algorithms:
nums = ["10", "15", "20", "25", "30", "35"]
lowestNumber = nums[1]
print("The lowest number is " + lowestNumber)
The lowest number is 15

Lists Quiz

import getpass, sys
import random

def ask_question (question, answer):

    print(question)
    ans = input(question)
    print(ans)
   
    if ans == answer:
        print("Correct!")
        return 1

    else:
        print("Wrong")
        return 0

question_list = ["What allows a value to be inserted into a list at index i?" , "What allows an element at index i to be deleted from a list?" , "What returns the number of elements currently in a specific list?" , "What allows a value to be added at the end of a list?"]
answer_list = ["index()", "remove()", "length()" , "append()"]

# Set points to 0 at the start of the quiz
points = 0

# If the length of the quiz is greater than 0, then random questions will be chosen from the "question_list" set
while len(question_list) > 0:
    index = random.randint(0, len(question_list) - 1)
    
# The points system where a point is rewarded for each correct answer    
    points = points + ask_question(question_list[index], answer_list[index])
    
# If a question or answer has already been used, then it shall be deleted    
    del question_list[index]
    del answer_list[index]

# Calculating score using the points system and dividing it by the total number of questions (6)
score = (points / 4)

# Calculating the percentage of correct answers by multiplying the score by 100
percent = (score * 100)

# Printing the percentage, and formatting the percentage in a way where two decimals can be shown (through "{:.2f}")
print("{:.2f}".format(percent) + "%")

# Adding final remarks based upon the users given scores
if points >= 5:
         print("Your total score is: ", points, "out of 4. Amazing job!")

elif points == 4:
         print("Your total score is: ", points, "out of 4. Not too bad, keep on studying! " )

else:
         print("Your total score is: ", points, "out of 4. Its alright, better luck next time!")
What allows a value to be inserted into a list at index i?
index()
Correct!
What allows an element at index i to be deleted from a list?
remove()
Correct!
What returns the number of elements currently in a specific list?
length()
Correct!
What allows a value to be added at the end of a list?
append()
Correct!
100.00%
Your total score is:  4 out of 4. Not too bad, keep on studying!