Class on February 15 2018
Rob ran a class session on the use of conditional code logic by spending time describing and using the if, for,
and while Python keywords, including using for in a for each context.
Example 1:
food = 'spam'
if food == 'spam':
print('Ummmm, my favorite!')
elif food == 'tang':
print('I prefer spam over tang!')
else:
print("No, I won't have it. I want spam!")
Example 2:
x = 8
if 0 < x: # assume x is an int here
if x < 10:
print("x is a positive single digit.")
Example 3:
for letter in 'Python': # First Example
print ('Current Letter :', letter)
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # Second Example
print ('Current fruit :', fruit)
print ("Good bye!")
Example 4:
for num in range(10,20): #to iterate between 10 to 20
for i in range(2,num): #to iterate on the factors of the number
if num%i == 0: #to determine the first factor
j=num/i #to calculate the second factor
print ('%d equals %d * %d' % (num,i,j))
break #to move to the next number, the #first FOR
else: # else part of the loop
print (num, 'is a prime number')
Rob then ran a class exercise that revisited the Newport sea level data to deconstruct the rate of change into twenty-year
time periods. Examples of two of the periods are shown below.