In [ ]:
# last time: for loops
In [1]:
# get computer to print hello multiple times
for n in range(5):
    print("hello")
hello
hello
hello
hello
hello
In [2]:
range(5)
Out[2]:
[0, 1, 2, 3, 4]
In [3]:
range(1,5) # can give range a starting point for list
Out[3]:
[1, 2, 3, 4]
In [6]:
# sum positive integers 1,...,9
run_sum = 0
for n in range(1,5):
    run_sum = run_sum + n
    print("n: ", n) # optional 
    print("run_sum: ", run_sum) # optional
print(run_sum)
('n: ', 1)
('run_sum: ', 1)
('n: ', 2)
('run_sum: ', 3)
('n: ', 3)
('run_sum: ', 6)
('n: ', 4)
('run_sum: ', 10)
10
In [ ]:
# CHALLENGE: compute sum of all positive integers n less than 15 such that
# n^3 - 29*n > 18
In [9]:
run_sum = 0
for n in range(1,15):
    if n^3 - 29*n > 18:
        run_sum += n # shorthand for run_sum = run_sum + n
        #print(run_sum)
print(run_sum)
90
In [ ]:
# Goal: Print out all odd positive integers less than 15
In [10]:
# To test whether n is odd/even, use the remainder operator %
7 % 2 # remainder when 7 is divided by 2
Out[10]:
1
In [11]:
14 % 5 # remainder when 14 is divided by 5
Out[11]:
4
In [12]:
# n % 2 is 0 if n even
# is 1 if n odd
10 % 2
Out[12]:
0
In [13]:
# Print out all odd positive integers less than 15
for n in range(1,15):
    if n % 2 == 1:
        print(n)       
1
3
5
7
9
11
13
In [23]:
# CHALLENGE: find product of all positive even numbers less than 20
# 2*4*6*..*18
run_prod = 1 # for product, start with 1
for n in range(1,10):
    if n % 2 == 0:
        run_prod = run_prod * n # could use short-hand: run_prod *= run_prod
print(run_prod)
384
In [29]:
a= 5
a *= 3 # same as a = a*3
print(a)
15
In [ ]:
# while loop 
In [30]:
# Use while loop to print out hello many times
n = 0
while n<4:
    print("hello")
    n += 1
hello
hello
hello
hello
In [ ]:
# WARNING: careful about infinite loop 
# Below, we forget to increase n, so n<4 is always true
n = 0
while n<4:
    print("hello")
    # forgot to do n += 1
In [32]:
# Functions 
# Bulit-in functions: print("hello"), factor(48)
print("hello") #python function 
hello
In [33]:
factor(48) # sage function
Out[33]:
2^4 * 3
In [37]:
# we can define our own functions 
def say_hello():
    print("hello, welcome to MAT 331")
    print("hello again")
In [38]:
say_hello() # calling the function
hello, welcome to MAT 331
hello again
In [42]:
# write a function that tests whether a number n is "small"
def is_small(n):
    """Returns True if n < 100.78654321. Returns False otherwise."""
    # above is Docstring, call view with is_small? 
    if n < 100.78654321:
        return True # the value of is_small(n) will be True
    else: 
        return False # the value of is_small(n) is False
In [44]:
is_small(500) # is_small(n) will be exactly n < 100.78654321
Out[44]:
False
In [55]:
# CHALLENGE: write a function that computes sum of 
# 1,...,n-1
def sum_integers(n):
    """Return 1+2+...+(n-1)."""
    run_sum = 0 # local variable, only changes it for this function
    # scope of variable is limited 
    for i in range(1,n):
        run_sum += i 
    return run_sum
In [54]:
sum_integers(10) + 77 # value of sum_integers becomes 45
Out[54]:
122
In [56]:
run_sum  # this is the old value of run_sum that we changed at beginning of class
Out[56]:
90
In [ ]: