W4 - LOOPS Flashcards
What is an action?
a set of statements which are grouped together, usually by indentation and are the “body” of a loop or conditional. it is literally the action that is being done if ____ is true or false or whatevs
how does range() function
range(n), used to sequence of integers ranging from 0, 1, 2, 3…. up until n-1 (think indices!)
if range is (i, j) starts sequence at i, then progresses by 1 to j-1
what will:
for i in range(3, 5):
print(i)
print?
3
4
range can be used as, well, a range!
break down the header statement of this for loop:
for i in range(5):
the “for i” establishes i as an iterator variable. in python, this setup automatically sets i to each value in the SEQUENCE VARIABLE (going left to right one by one) created by range (0, 1, 2, 3, 4 in this case).
what will this print?
ran = [1, 2, 5, 9, 13]
for i in ran:
print(i, end = ‘ ‘)
1 2 5 9 13
alphabet = “abcd”
for i in alphabet:
print(i, end = “ “)
print(i)
what will the above print?
a b c d d
create a while loop that adds from x = 0 to x = 5 then breaks off to print x = 5 when x = 5 using an else
x = 0
while x < 5:
x=x+1
else:
print(“x=5”)