<jupyter_start><jupyter_text>Loops<jupyter_code>word = 'notebook'
print (word[4])
# for loops, char is counter variable
for char in word:
    print (char)<jupyter_output>n
o
t
e
b
o
o
k<jupyter_text>get a list of all the filenames from disk<jupyter_code>import glob
# global file system library has only one function in it
print (glob.glob('data/weather*.csv'))
# all files start with weather and end with .csv * means there can be anything in between<jupyter_output>['data\\weather-01.csv', 'data\\weather-02.csv', 'data\\weather-03.csv', 'data\\weather-04.csv', 'data\\weather-05.csv', 'data\\weather-06.csv', 'data\\weather-07.csv', 'data\\weather-08.csv', 'data\\weather-09.csv', 'data\\weather-10.csv', 'data\\weather-11.csv', 'data\\weather-12.csv']<jupyter_text>Putting it all together<jupyter_code># new variable
filenames = sorted(glob.glob('data/weather*.csv'))

#overwrite 'filenames' with a different value to create a smaller slice 
#filenames = filenames[0:3]
#can add a '#' before code to not use it without deleting it

for f in filenames:
    print (f)
    #loop across individual file names in 'filenames' and indent lines below so that it is part of the loop
    
    data = numpy.loadtxt(fname=f, delimiter=',')
    
    if numpy.max (data, axis=0)[0] == 0 and numpy.max (data, axis=0)[20] == 20:
        print ('suspicious looking maxima')
    elif numpy.sum(numpy.min(data, axis=0)) ==0:
        print ('minimum add to zero')
    else:
        print ('data looks ok')
    #if the first data point is o and the 20th one is 20 or if all the data is 0, hen there is something wrong otherwise carry on
    #shows message over graph
    
    # create a wide figure to hold the sublots
    fig = matplotlib.pyplot.figure (figsize=(10.0, 3.0))

    # create placeholder for plots
    subplot1 = fig.add_subplot(1,3,1)
    subplot2 = fig.add_subplot(1,3,2)
    subplot3 = fig.add_subplot(1,3,3)

    subplot1.set_ylabel('average')
    subplot1.plot(numpy.mean(data, axis=0))

    subplot2.set_ylabel('max')
    subplot2.plot(numpy.max(data, axis=0))

    subplot3.set_ylabel('min')
    subplot3.plot(numpy.min(data, axis=0))

    fig.tight_layout()
    matplotlib.pyplot.show()<jupyter_output>data\weather-01.csv
suspicious looking maxima<jupyter_text>Making decisions<jupyter_code>num = 107
if num > 100:
    print ('Greater')
else:
    print ("Not Greater")
    print ('Done')
num = -3

if num >0:
    print (num, "is positive")
elif num == 0:
    print (num, "is zero")
else:
    print (num, "is negative")
    
    #single = is to assign a value to the variable, double == is for matamaticaly equal, =? is not equal to
    #elif is short for else if, do another if test if the first one fails<jupyter_output>-3 is negative<jupyter_text>Functions<jupyter_code># functions start with def ie define
def fahr_to_kelvin(temp):
    return((temp-32)*(5/9)+273.15)
#doesn't do anything but this function is there to be used whenever
print ('freezing point of water:', fahr_to_kelvin(32))
print ('boiling point of water:', fahr_to_kelvin(212))
def fahr_to_cel(temp):
    return((temp-32)*(5/9))
print ('cross over point:', fahr_to_cel(-40))
def analyse(filename):
    """ Calculate the mean max and min and create three graphs within one plot
    """
    data = numpy.loadtxt(fname=filename, delimiter=',')
    # create a wide figure to hold the sublots
    fig = matplotlib.pyplot.figure(figsize=(10.0, 3.0))

    # create placeholder for plots
    subplot1 = fig.add_subplot(1, 3, 1)
    subplot2 = fig.add_subplot(1, 3, 2)
    subplot3 = fig.add_subplot(1, 3, 3)

    subplot1.set_ylabel('average')
    subplot1.plot(numpy.mean(data, axis=0))

    subplot2.set_ylabel('max')
    subplot2.plot(numpy.max(data, axis=0))

    subplot3.set_ylabel('min')
    subplot3.plot(numpy.min(data, axis=0))

    fig.tight_layout()
    matplotlib.pyplot.show() 
def detect_problems(filename):
    """Reports on odd looking maxima and minima. The function does not return data it adds
    a label to the graph
    """
    data = numpy.loadtxt(fname=filename, delimiter=',')
    
    if numpy.max(data, axis=0)[0] == 0 and numpy.max (data, axis=0)[20] == 20:
        print('suspicious looking maxima')
    elif numpy.sum(numpy.min(data, axis=0)) ==0:
        print('minimum add to zero')
    else:
        print('data looks ok')
        #the string is a comment on the function to describe what it does. triple quote strings cover multiple lines
for f in filenames [0:5]:
    print (f)
    analyse (f)
    detect_problems (f)
help(numpy.loadtxt)
help(detect_problems)
# we want a description about what this function does, it is useful to have this to remember in the future
help(analyse)<jupyter_output>Help on function analyse in module __main__:

analyse(filename)
    Analyse a set of data, calculate the mean max and min and create three graphs side by sid within one plot
{'fraction_non_alphanumeric': 0.12253694581280788, 'fraction_numerical': 0.02832512315270936, 'mean_word_length': 3.7774509803921568, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '519399', 'license': 'MIT', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1655, 'n_words': 523, 'path': '02-plots-and-subplots.ipynb', 'repo_name': 'DavidMEdwards1/Python', 'size': '4872'}