Chapter 5: Repetition Flashcards
Types of Repetition:
- Fixed Repetition
- Variable Repetition
The number of repetitions in FIXED REPETITION…
…can be determined in advance
the number of loops in a FIXED REPETITION is….
INDEPENDENT of what happens inside the loop
number of times a code will be repeated in VARIABLE REPETITION….
…CANNOT BE DETERMINED
Counter-controlled loops
- also known as “counting loops”
- controlled by a var whose value represents a counter
- implement FIXED REPETITION
- FOR & WHILE loop
Event-driven loops
- “Conditional loops”
- implement VARIABLE REPETITION
- WHILE loop
keyword break function:
- immediately terminate a loop
- continue w the next statement after loop
keyword “continue” function:
ends current iteration and goes to the end if the loop
list[-2] is the same as list[-2 + len(list1)] in the sense that both gives..
the SECOND LAST element in the list
List Slicing [start:end]
slice is a sublist from index start to index end-1
list1=[2,3,5,7,9,1]
list1 [2:4] #4-1
[5, 7]
in list:
positions != index
index = position - 1
extra notes
append()
adds an element at the end of list
count()
return the number of elements with specified value
insert()
adds an element at the specified POSITION
pop()
removes element at the specified position
remove()
removes element with the specified position
sort()
sorts the list in ascending matter
TUPLES
- elements are FIXED
- cannot add new elements, delete, replace nor reorder
functions for both list and tuple
- len
- max()
- sum()
- min()
- shuffle()
For-loop Format
for i in range({finalValue}):
assume initialValue is 0
({initialVal},{finalValue}):
in range({initialValue},{finalValue},{stepValue})
Nested loop
- consist of outer loop+inner loop
- each time the outer is repeated, inner loop are reentered started anew