First Python Exercises (plus some HTML)

Note: revised submission instructions below (also, this is a link to another part of this page, which is not something that we covered in HTML).

Resources for Python exercises:

Here are a couple of examples:

sum_between(low,high)

# Add up the numbers between the low first argument and the high second argument
#   low and high must be integers, with low <= high
def sum_between(low,high):
    count = low
    acc = 0
    while count <= high:
        acc = acc + count
        count = count + 1
    return acc

rev_2dig(n)

# reverse the digits of any number between 0 and 99
#   rev_2dig(95) -> 59
#   rev_2dig(8) -> 8

def rev_2dig(n):
    # one digit?
    if n < 10:
        answer = n
    else:
        # two digits:
        answer = (n%10)*10 + n/10

    return answer



What to submit to the homework server

You'll be creating a web page containing a table with your Python code answers and the results of particular tasks that each problem asks.  Your table should look like the picture below, with the same approx. layout and similar coloring.  New instructions: just submit this HTML file to the homework server (when submitting the homework, you'll see and use the "Choose File" button) -- don't upload it to your public_html directory because there are currently problems connecting to the class computers.

NOTE: when copying and pasting Python code (multiple lines at a time) into an HTML file, precede the Python with the <pre> tag and end it with the </pre> tag.  Look at the page source for the tables above.  This will force HTML to preserve the line breaks and indentation, which would otherwise be lost.

Picture of the table with answers


Exercises:

There are several Python exercises below.  You need to create a web page containing the Python functions that are the answers to those exercises. 

Problem 1:

Create a function called div23(n) which will take a single non-negative integer and return True if it's evenly divisible by both 2 and 3, and False otherwise.  Special answer: what is div23(4492738)?

Problem 2:

Create the function div23_between(low,high) that will be given 2 non-negative integers, with low <= high and will return the number of integers between low and high (including low and high) that are divisible by both 2 and 3.  Special answer: what is div23_between(0,399154)?

Problem 3:

Create the function sum_dig(n) that adds up the digits of n, where n is a non-negative number less than 1,000.  Therefore sum_dig(314) returns 8.  Special answer: what is sum_dig(667)?

Problem 4:

How many integers between 0 and 999 have the following property: that the sum of its digits is evenly divisible by 11.  For instance, 65 has that property and so does 966.  How many such numbers are there?  Create the function how_may_11() to answer that question.  Special answer: what is how_many_11()?

Challenge Problem (optional - but try your hand at it):

Create the function is_prime(n) that will be given an integer greater than 1, and will return True if it's a prime number and False if not.  Test this carefully on the low numbers.  Remember: the first 4 primes are 2, 3, 5 and 7.  Special answer: what is is_prime(12343)?