Module 0.3.0 - Loops & Iteration

  • loops, run a set of code multiple times

  • an iteration is each time a loop reruns

0.3.1 - While Loops

evaluates code "as long as" a condition is true, if fasle, loop will end [break]

i = 0

while i <= 5:
	i += 1
	print(i)
	
print("loop has ended")

>>> 1
>>> 2
>>> 3
>>> 4
>>> 5
>>> "loop has ended"

loops can be infinite

  • to end a program from running, enter ctrl + c

  • "break" statement ends loops

"continue" statement, cancels current iteration and meves to the next one

0.3.2 - Range

a function that creates a sequence of numbers

  • the first argument is the start, leave blank to start from 0

  • the second argument is the end

  • the third argument is the interval, leave blank for no interval

  • list function, creates a list [a type of data structure] using an argument

  • remember, indexing starts from 0. so this list will have 10 values but will only go up to 9

  • removing the list function will make the variable an object, good for iteration

listed range with an iterval of 2

0.3.3 - For Loops

  • iterating through a set of data is excessive with "while" loops

  • "for" loops evaluates code "for each" item in a certain set of data

break and continue statements also work with for loops

0.3.4 - Looping With Conditionals

  • the "else" statement can also be used with loops.

  • it will run it's segmented code if the loop before it runs sucessfully