counter = 0
simple_list = [0,1,2,3,4,5,6,2143,123,7834]
for i in simple_list:
print(i) # print all values in array
counter = counter + 1
print('The loop cycled',counter,'times')
0 1 2 3 4 5 6 2143 123 7834 The loop cycled 10 times
# Nested for loops
for i in simple_list[1:4]:
for j in simple_list[4:7]:
print(i*j)
print('outside of j loop',"inside i",i)
print('outside of both loops')
4 5 6 outside of j loop inside i 1 8 10 12 outside of j loop inside i 2 12 15 18 outside of j loop inside i 3 outside of both loops
less memory intensive way of storing a list.
First number is inclusive and second number is exclusive of.
print (range(6))
range(0, 6)
for i in range(6): #requires a colon at the end
print(i)
0 1 2 3 4 5
# specify the starting point of a range.
for i in range(4,10):
print(i)
4 5 6 7 8 9
# increment by a specific amount
for i in range(2,10,2): # Third numer is the amout to increment by
print(i)
2 4 6 8
#convert a range to a list trhough
list(range(6,15,3))
[6, 9, 12]
At each cycle it checks a condition
i=4
while i < 10: # requires a colon at the end
print(i)
i = i+1
print('ouside of loop')
4 5 6 7 8 9 ouside of loop
1. if statement
2. else statement
3. elif statement
i = 3
if i<10:
print(i,'is less than 10')
else:
print(i,'is greater than or equal to 10')
3 is less than 10
i = 8
if i > 10:
print(i,'is greater than 10')
elif i < 3:
print(i,'is less than 3')
else:
print(i,'is between 3 and 10')
8 is between 3 and 10
i=3
if i<10:
print(i,'is less than 10')
elif i<=3:
print(i, 'is less than or equal to 3')
elif i>4:
print(i)
else:
print(i,'is between 3 and 10')
3 is less than 10