In [1]:
import glob
import numpy
import matplotlib.pyplot
  • glob contains function glob that finds files that match a pattern
  • * matches 0+ characters; ? matches any one char

In [3]:
print(glob.glob('data/inflammation*.csv'))


['data/inflammation-01.csv', 'data/inflammation-02.csv', 'data/inflammation-03.csv', 'data/inflammation-04.csv', 'data/inflammation-05.csv', 'data/inflammation-06.csv', 'data/inflammation-07.csv', 'data/inflammation-08.csv', 'data/inflammation-09.csv', 'data/inflammation-10.csv', 'data/inflammation-11.csv', 'data/inflammation-12.csv']
  • results in a list of strings, we can loop oer
  • we want to create sets of plots

In [5]:
# loop here
counter = 0
for filename in glob.glob('data/*.csv'):
    #counter+=1
    counter = counter + 1
print("number of files:", counter)


number of files: 15

In [7]:
counter = 0
for filename in glob.glob('data/infl*.csv'):
    #counter+=1
    counter = counter + 1
print("number of files:", counter)


number of files: 12

In [8]:
counter = 0
for filename in glob.glob('data/infl*.csv'):
    #counter+=1
    data = numpy.loadtxt(fname=filename, delimiter=',')
    print(filename, "mean is: ", data.mean())
    counter = counter + 1
print("number of files:", counter)


data/inflammation-01.csv mean is:  6.14875
data/inflammation-02.csv mean is:  5.99083333333
data/inflammation-03.csv mean is:  4.20458333333
data/inflammation-04.csv mean is:  6.10958333333
data/inflammation-05.csv mean is:  6.11833333333
data/inflammation-06.csv mean is:  6.04333333333
data/inflammation-07.csv mean is:  6.01958333333
data/inflammation-08.csv mean is:  4.20458333333
data/inflammation-09.csv mean is:  6.03291666667
data/inflammation-10.csv mean is:  6.0525
data/inflammation-11.csv mean is:  4.20458333333
data/inflammation-12.csv mean is:  6.06166666667
number of files: 12

We can ask Python to take different actions, depending on a condition, with an if statement:

Making choices

  • last lesson we discovereed something suspicious in our inflammatin data by drawing plots
  • how can python recognized these different features and act on it
  • we will write code that runs on certain conditions

In [6]:
#We use an if statement to take different actions 
#based on conditions
num = 37
if num > 100:
    print('greater')
else:
    print('not greater')
print('done')


not greater
done
  • second line of code above uses keyword if to denote choice
  • if the test after if is true, the body of the if are executed
  • if test false the body else is executed
  • conditional statements don't have to include else - if not present python does nothing

In [7]:
num = 53
print('before conditional...')
if num > 100:
    print('53 is greater than 100')
print('...after conditional')


before conditional...
...after conditional

we can also chain several tests together using elif, short for else if


In [8]:
num = -3

if num > 0:
    print(num, "is positive")
elif num == 0:
    print(num, "is zero")
else:
    print(num, "is negative")


-3 is negative
  • NOTE: we use == to test for equality rather than single equal b/c the later is the assignment operator
  • we can also combine tests using and and or. and is only true if both parts are true

In [9]:
if (1 > 0) and (-1 > 0):
    print('both parts are true')
else:
    print('at least one part is false')


at least one part is false

while or is true if at least one part is true:


In [10]:
if (1 < 0) or (-1 < 0):
    print('at least one test is true')


at least one test is true

Challenge - making choices:

Which of the following would be printed if you were to run this code? Why did you pick this answer?

A B C B and C

if 4 > 5:
    print('A')
elif 4 == 5:
    print('B')
elif 4 < 5:
    print('C')

In [1]:
if 4 > 5:
    print('A')
elif 4 == 5:
    print('B')
elif 4 < 5:
    print('C')


C

Challenge - making choices 2:

True and False are special words in Python called booleans which represent true and false statements. However, they aren’t the only values in Python that are true and false. In fact, any value can be used in an if or elif. After reading and running the code below, explain what the rule is for which values are considered true and which are considered false.

if '':
    print('empty string is true')
if 'word':
    print('word is true')
if []:
    print('empty list is true')
if [1, 2, 3]:
    print('non-empty list is true')
if 0:
    print('zero is true')
if 1:
    print('one is true')

In [3]:
if '':
    print('empty string is true')
if 'word':
    print('word is true')
if []:
    print('empty list is true')
if [1, 2, 3]:
    print('non-empty list is true')
if 0:
    print('zero is true')
if 1:
    print('one is true')


word is true
non-empty list is true
one is true

In [ ]: