<jupyter_start><jupyter_text>What you will lean- what is a for loop- how to iterate though lists using for loops  What is a for loopA for loop will iterate over a spesific section of code a number of timesIt is best seen in action Let's use a for loop to count to the number 10<jupyter_code>for i in range(10):
    print(i)<jupyter_output>0
1
2
3
4
5
6
7
8
9<jupyter_text>Note- Unlike matlab Python (and almost every other programming language) starts counting at 0    - This is because comuters start addressing memory at address 0 (to start at 1 would waist space)- The last number in the range is exclusive<jupyter_code>for i in range(10, 20):
    print(i)<jupyter_output>10
11
12
13
14
15
16
17
18
19<jupyter_text>Note- We can spesify the range we are wanting to loop for- In this case starting at 10 (inclusively) and ending at 20 (exclusively) Using range we can also incrase by counts other then 1<jupyter_code>for i in range(0, 50, 10):
    print(i)<jupyter_output>0
10
20
30
40<jupyter_text>Looping though a listOne of the most common things we need to do is loop though a list. Right off the bat there is a good way to do this, and a bad way. - This is probably what you do in Matlab<jupyter_code>fruits = ["Apples", "Bananas", "Oranges", "Pineapples"]

for i in range(len(fruits)):
    print(fruits[i])<jupyter_output>Apples
Bananas
Oranges
Pineapples<jupyter_text>While it works this is bad form in Python. Why? Becuase the Python language is not being used to its fullest extent<jupyter_code>fruits = ["Apples", "Bananas", "Oranges", "Pineapples"]

for fruit in fruits:
    print(fruit)<jupyter_output>Apples
Bananas
Oranges
Pineapples<jupyter_text>Note- This is easier to read    - NEVER underestimate the importane of readable code. Think of how bad it sucks to read though a terribly written essay. Now image reading though a terribly written essay written in a language built for computers doing complex logic  Common Things With Lists  EnumerateSometimes we need an index number while still iterating over a list. We can use enumerate to do this<jupyter_code>fruits = ["Apples", "Bananas", "Oranges", "Pineapples"]

for i, fruit in enumerate(fruits):
    print(f"{fruit} is at the {i} index")<jupyter_output>Apples is at the 0 index
Bananas is at the 1 index
Oranges is at the 2 index
Pineapples is at the 3 index<jupyter_text>Note- we take in the varable i followed by a comma then the variable fruit- If we reversed these then it would print "0 is at the Apples index" and so forth  ZipWhat if we need to iterate over two lists at the same time? Well we could use enuerate and then get the index of the other list ... BUT there is a better way. Zip<jupyter_code>fruits = ["Apples", "Bananas", "Oranges", "Pineapples"]
cars = ["Truck", "Jeep", "Sports Car", "SUV", "Sudan", "Hatch-Back", "Cross-Over"]

for fruit, car in zip(fruits, cars):
    print(f"{fruit} is a type of fruit")
    print(f"{car} is a type of car\n")<jupyter_output>Apples is a type of fruit
Truck is a type of car

Bananas is a type of fruit
Jeep is a type of car

Oranges is a type of fruit
Sports Car is a type of car

Pineapples is a type of fruit
SUV is a type of car<jupyter_text>Note- There are more items in the list 'cars' then in the list 'fruits'- zip will stop iterating when all items have been iterated though in the smallest list- Zip can unzip any number of list- Notice that fruit, car is in the same order as fruits, cars  Looping though a dictionaryThere are a few diffrent ways we can loop though a dictionary. We'll go though all of them  .keys()We can create a list of all the keys in a dictionary.<jupyter_code>books = {
    'H<NAME>': 'JK Rolling', 
    'The Kingkiller Chronicles': '<NAME>', 
    'Game of Thrones': '<NAME>', 
    'The Lord of the Rings': '<NAME>'}

for key in books.keys():
    print(key)<jupyter_output><NAME>
The Kingkiller Chronicles
Game of Thrones
The Lord of the Rings<jupyter_text>Some backaround theorybooks.keys() is not actally a list. It is a 'dict_keys' list<jupyter_code>books = {
    '<NAME>': 'JK Rolling', 
    'The Kingkiller Chronicles': '<NAME>', 
    'Game of Thrones': '<NAME>', 
    'The Lord of the Rings': '<NAME>'}

bookKeys = books.keys()

print(type(bookKeys))
print(bookKeys)<jupyter_output><class 'dict_keys'>
dict_keys(['<NAME>', 'The Kingkiller Chronicles', 'Game of Thrones', 'The Lord of the Rings'])<jupyter_text>So how can we iterate over bookKeys? Underneath the hood of varables we can see some information about then using dir()<jupyter_code>books = {
    '<NAME>': 'JK Rolling', 
    'The Kingkiller Chronicles': '<NAME>', 
    'Game of Thrones': '<NAME>', 
    'The Lord of the Rings': '<NAME>'}

bookKeys = books.keys()

print(dir(bookKeys))<jupyter_output>['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'isdisjoint']<jupyter_text>There is a lot of mumbo-jumbo in here you don't need to worry about. BUT notice that there is something called "_iter_",_iter_ tells python how to move to the next "thing". _iter_ has a function called __next__() that tells Python how to move to the next "thing"This is how the for loop knows how to move though the loopYou cannot loop though an iterable object backwards becuase there is no __back__(), only a __next__() call<jupyter_code># define a list
my_list = [4, 7, 0, 3]

# get an iterator using iter()
my_iter = iter(my_list)

## iterate through it using next() 

#prints 4
print(my_iter.__next__())

#prints 7
print(my_iter.__next__())

## next(obj) is same as obj.__next__()

#prints 0
print(my_iter.__next__())

#prints 3
print(my_iter.__next__())

## This will raise error, no items left
next(my_iter)<jupyter_output>4
7
0
3<jupyter_text>Note- can can dir() any varable in python- This can give you some insights on useageHere is an example on a list<jupyter_code>fruits = ["Apples", "Bananas", "Oranges", "Pineapples"]

print(dir(fruits))<jupyter_output>['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']<jupyter_text>Note- Notice how "append", "copy", "pop", etc are listed here? ...  Back to looping Looping by values<jupyter_code>books = {
    '<NAME>': '<NAME>', 
    'The Kingkiller Chronicles': '<NAME>', 
    'Game of Thrones': '<NAME>', 
    'The Lord of the Rings': '<NAME>'}

for val in books.values():
    print(val)<jupyter_output><NAME>
<NAME>
<NAME>
<NAME><jupyter_text>Looping by item<jupyter_code>books = {
    '<NAME>': '<NAME>', 
    'The Kingkiller Chronicles': '<NAME>', 
    'Game of Thrones': '<NAME>', 
    'The Lord of the Rings': '<NAME>'}

for key, val in books.items():
    print(f"{key} by {val}")<jupyter_output><NAME> by <NAME>
The Kingkiller Chronicles by <NAME>
Game of Thrones by <NAME>
The Lord of the Rings by <NAME><jupyter_text>Breaking out of a loopIf you are iterating though somehthing and what to stop before all items have been iteraged over you can use break<jupyter_code>numbers = [1,2,3,4,5,6,7,8,9,10]

# will get to 5 and then break from the loop
# so this will print 1,2,3, and 4
for num in numbers:
    if num == 5:
        break
    
    print(num)<jupyter_output>1
2
3
4<jupyter_text>What you need to doData samples for the temperature are collected one a day for 100 days. Each sample is tagged with a timestamp anda temperature value. These data samples are stored in the below variable "data".- Somewhere between days 24 - 67 (inclusively) there was an error reading the temperature. The sensor reported a valuefar too hot to be possible. Find this value and remove it from the list.- We need to know how many days the temperature was over 40. Print this to the screen (omitting the bad data point).- What is the the average temperature over the 99 days (omitting the bad).  Time stamps- The timestamps are in time sence epoch form- This is the number of seconds that have passed from midnight on Jan 1, 1970- Below is a quick example of how these time stamps were generated    - We will talk more on imports later    - We will talk more on dealing with time later<jupyter_code>import time

print(f"{time.time()} seconds ago was New Years 1970")
data = [{'Time Stamp': 1570648123.120855, 'Temp': 20}, {'Time Stamp': 1570734523.120863, 'Temp': 47},
        {'Time Stamp': 1570820923.120866, 'Temp': 43}, {'Time Stamp': 1570907323.1208682, 'Temp': 39},
        {'Time Stamp': 1570993723.1208708, 'Temp': 45}, {'Time Stamp': 1571080123.120873, 'Temp': 11},
        {'Time Stamp': 1571166523.1208751, 'Temp': 17}, {'Time Stamp': 1571252923.120877, 'Temp': 2},
        {'Time Stamp': 1571339323.1208792, 'Temp': 49}, {'Time Stamp': 1571425723.1208808, 'Temp': 16},
        {'Time Stamp': 1571512123.120883, 'Temp': 27}, {'Time Stamp': 1571598523.120885, 'Temp': 43},
        {'Time Stamp': 1571684923.120887, 'Temp': 44}, {'Time Stamp': 1571771323.1208892, 'Temp': 9},
        {'Time Stamp': 1571857723.1208909, 'Temp': 1}, {'Time Stamp': 1571944123.120893, 'Temp': 38},
        {'Time Stamp': 1572030523.120895, 'Temp': 57}, {'Time Stamp': 1572116923.120897, 'Temp': 5},
        {'Time Stamp': 1572203323.120899, 'Temp': 22}, {'Time Stamp': 1572289723.120901, 'Temp': 9},
        {'Time Stamp': 1572376123.120903, 'Temp': 9}, {'Time Stamp': 1572462523.120905, 'Temp': 11},
        {'Time Stamp': 1572548923.120907, 'Temp': 9}, {'Time Stamp': 1572635323.120909, 'Temp': 50},
        {'Time Stamp': 1572721723.1209111, 'Temp': 35}, {'Time Stamp': 1572808123.1209128, 'Temp': 49},
        {'Time Stamp': 1572894523.120917, 'Temp': 27}, {'Time Stamp': 1572980923.120919, 'Temp': 58},
        {'Time Stamp': 1573067323.1209211, 'Temp': 9}, {'Time Stamp': 1573153723.120925, 'Temp': 35},
        {'Time Stamp': 1573240123.1209269, 'Temp': 7}, {'Time Stamp': 1573326523.120928, 'Temp': 43},
        {'Time Stamp': 1573412923.12093, 'Temp': 37}, {'Time Stamp': 1573499323.120932, 'Temp': 33},
        {'Time Stamp': 1573585723.1209338, 'Temp': 47}, {'Time Stamp': 1573672123.120936, 'Temp': 11},
        {'Time Stamp': 1573758523.120938, 'Temp': 39}, {'Time Stamp': 1573844923.12094, 'Temp': 17},
        {'Time Stamp': 1573931323.120942, 'Temp': 333}, {'Time Stamp': 1574017723.120944, 'Temp': 37},
        {'Time Stamp': 1574104123.120946, 'Temp': 39}, {'Time Stamp': 1574190523.120948, 'Temp': 48},
        {'Time Stamp': 1574276923.12095, 'Temp': 58}, {'Time Stamp': 1574363323.1209521, 'Temp': 0},
        {'Time Stamp': 1574449723.120954, 'Temp': 29}, {'Time Stamp': 1574536123.120956, 'Temp': 26},
        {'Time Stamp': 1574622523.1209579, 'Temp': 8}, {'Time Stamp': 1574708923.12096, 'Temp': 16},
        {'Time Stamp': 1574795323.1209621, 'Temp': 8}, {'Time Stamp': 1574881723.120965, 'Temp': 49},
        {'Time Stamp': 1574968123.1209679, 'Temp': 17}, {'Time Stamp': 1575054523.12097, 'Temp': 22},
        {'Time Stamp': 1575140923.120973, 'Temp': 0}, {'Time Stamp': 1575227323.120975, 'Temp': 29},
        {'Time Stamp': 1575313723.120977, 'Temp': 14}, {'Time Stamp': 1575400123.1209788, 'Temp': 17},
        {'Time Stamp': 1575486523.120981, 'Temp': 45}, {'Time Stamp': 1575572923.1209831, 'Temp': 55},
        {'Time Stamp': 1575659323.120985, 'Temp': 3}, {'Time Stamp': 1575745723.120987, 'Temp': 26},
        {'Time Stamp': 1575832123.1209888, 'Temp': 25}, {'Time Stamp': 1575918523.120991, 'Temp': 3},
        {'Time Stamp': 1576004923.1209931, 'Temp': 44}, {'Time Stamp': 1576091323.120995, 'Temp': 10},
        {'Time Stamp': 1576177723.1209972, 'Temp': 49}, {'Time Stamp': 1576264123.120998, 'Temp': 46},
        {'Time Stamp': 1576350523.121, 'Temp': 3}, {'Time Stamp': 1576436923.121002, 'Temp': 19},
        {'Time Stamp': 1576523323.121004, 'Temp': 26}, {'Time Stamp': 1576609723.121006, 'Temp': 6},
        {'Time Stamp': 1576696123.1210082, 'Temp': 23}, {'Time Stamp': 1576782523.1210098, 'Temp': 8},
        {'Time Stamp': 1576868923.121012, 'Temp': 20}, {'Time Stamp': 1576955323.121015, 'Temp': 10},
        {'Time Stamp': 1577041723.121017, 'Temp': 36}, {'Time Stamp': 1577128123.121021, 'Temp': 58},
        {'Time Stamp': 1577214523.121023, 'Temp': 15}, {'Time Stamp': 1577300923.121025, 'Temp': 59},
        {'Time Stamp': 1577387323.121026, 'Temp': 8}, {'Time Stamp': 1577473723.1210282, 'Temp': 13},
        {'Time Stamp': 1577560123.1210299, 'Temp': 11}, {'Time Stamp': 1577646523.121032, 'Temp': 14},
        {'Time Stamp': 1577732923.121034, 'Temp': 14}, {'Time Stamp': 1577819323.121036, 'Temp': 34},
        {'Time Stamp': 1577905723.1210382, 'Temp': 35}, {'Time Stamp': 1577992123.1210399, 'Temp': 34},
        {'Time Stamp': 1578078523.121042, 'Temp': 30}, {'Time Stamp': 1578164923.121044, 'Temp': 23},
        {'Time Stamp': 1578251323.121046, 'Temp': 5}, {'Time Stamp': 1578337723.121048, 'Temp': 13},
        {'Time Stamp': 1578424123.1210501, 'Temp': 49}, {'Time Stamp': 1578510523.1210518, 'Temp': 36},
        {'Time Stamp': 1578596923.121054, 'Temp': 38}, {'Time Stamp': 1578683323.121056, 'Temp': 17},
        {'Time Stamp': 1578769723.121058, 'Temp': 37}, {'Time Stamp': 1578856123.1210601, 'Temp': 23},
        {'Time Stamp': 1578942523.1210618, 'Temp': 54}, {'Time Stamp': 1579028923.121063, 'Temp': 13},
        {'Time Stamp': 1579115323.121068, 'Temp': 36}, {'Time Stamp': 1579201723.121071, 'Temp': 17}]<jupyter_output><empty_output>