In [ ]:
# last time loops, for loop
In [1]:
# get computer to print hello many times
for i in range(5):
    print("hello")
hello
hello
hello
hello
hello
In [2]:
range(10)
Out[2]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [3]:
# use loops to compute sums 
# sum of numbers between 0 and 9 (including 0 and 9)
run_sum = 0 # this will keep track of the total so far
for i in range(10):
    # at every step, add i to run_sum
    run_sum = run_sum + i 
    print(run_sum) # intermediate check  
print("The total is:")
print(run_sum)
0
1
3
6
10
15
21
28
36
45
The total is:
45
In [4]:
# CHALLENGE: compute sum of all positive integers n less than 15 such that
# n^3 - 29*n > 18
run_sum = 0 # this will keep track of the total so far
for n in range(1,15):
    # at every step, add i to run_sum
    if n^3 - 29*n > 18:
        run_sum = run_sum + n
    print(run_sum) # intermediate print statement  
print("The total is:")
print(run_sum)
0
0
0
0
0
6
13
21
30
40
51
63
76
90
The total is:
90
In [6]:
# print out all odd numbers less than 15
# use the more general remainder operator %
9 % 2 # remainder when we divide 9 by 2
Out[6]:
1
In [7]:
7 % 3 # remainder when divide 7 by 3
Out[7]:
1
In [8]:
10 % 2
Out[8]:
0
In [ ]:
# to test whether number n is even/odd
# look at n % 2
# value is 0 if n is even
# and 1 if n is odd 
In [9]:
# print out all odd numbers less than 15
for n in range(15):
    if n % 2 == 1:
        print(n)
1
3
5
7
9
11
13
In [ ]:
# CHALLENGE: find the product of all positive even numbers less than 20
In [14]:
run_prod = 1  # since we are doing a product, start with 1
for n in range(1,20):
    if n % 2 == 0:
        run_prod = run_prod * n
print(run_prod)
185794560
In [17]:
a = 4 
a += 5  # Equivalent to a = a + 5
print(a)
9
In [ ]:
# while loop 
In [19]:
# print hello many times using while loop
n=0
while n<5:
    print("hello")
    print(n)
    n += 1 # increase n by 1
hello
0
hello
1
hello
2
hello
3
hello
4
In [ ]:
# WARNING: easy to get into infinite loops with while,
# if you forget to change n

n=0
while n<5:
    print("hello")
    print(n)
    # n += 1 # increase n by 1
In [21]:
# Functions 
# Built-in functions 
print("hello")
 hello
In [22]:
factor(18) # sage function
Out[22]:
2 * 3^2
In [26]:
# Define a function say_hello using def keyword
def say_hello():
    print("hello")
    print("hello again")
In [27]:
say_hello()
hello
hello again
In [31]:
# write a function that tests whether a number is "small"
def is_small(n):
    """Return true if n is less than 100. Otherwise return false."""
    # Docstring: is_small? will give this info
    if n<100:
        return True  # this means the value of is_small(n) becomes True in this case 
    else: 
        return False
        
In [34]:
is_small(150) # will be same as 150<100
Out[34]:
False
In [36]:
if is_small(90):
    print("90 is small")
90 is small
In [37]:
# CHALLENGE: make a function that computes sum of 1,...,n-1
def sum_integers(n):
    """Return sum 1,...,n-1"""
    run_sum = 0 # variable only exists inside function (called scope)
    for i in range(1,n):
        run_sum += i
    return run_sum
    
In [40]:
sum_integers(10)
Out[40]:
45
In [41]:
sum_integers(7)
Out[41]:
21
In [42]:
run_sum # this will print the old value that we changed at the beginning of class
Out[42]:
90
In [ ]: