my name is bishop
I am a python programmer
I am a writer
I practice zen meditation
I am a musician
I am in peak physical health
I am a photographer
I speak fluent russian


and I do not exist.........yet

My first functions

August 19th, 2007 by Robert

Here are some of the functions I created for my exercises. They may not be the most efficient way of coding them, but they all seem work.

I tested the isPrime() function with the number 2**70 and it was well over 5 minutes before I got a return result. I’ll have to look in to how to speed that up.

#a simple compare function
def compare(x,y):
if x > y:
return 1
if x < y:
return 0
else:
return -1

#determines if a number is even
def isEven(x):
if x % 2 == 0:
return True
else:
return False

#determines if a number is odd
def isOdd(y):
even = is_even(y)
if even == True:
return False
else:
return True

#determines if f is a factor of n
def isFactor(n,f):
if n%f == 0:
return True
else:
return False

#determines if m is a multiple of n
def isMultiple(n,m):
if m%n == 0:
return True
else:
return False

#finds the common multiple of 2 numbers
def comult(x,y):
if type(x) != int or type(y) != int:
'''Guardian code'''
print "numbers only please"
return
if x == 0 or y == 0:
print "please enter a number greater than zero"
return
'''End guardian code'''
num = 0
remainder = 1
while remainder != 0:
num = num + 1
remainder = (x * num) % y
return (x * num)

#lists power of 2 up to 2**16
def powof2():
x = 2.0
while x < 65536.0:
print x, ' ', '\t', x * 2
x = x * 2.0

#prints multiples of number n up to number high
def print_multiples(n, high):
i = 1
while i < = high:
print n*i, '\t',
i = i + 1
print

#prints a multiplication table up to number high
def print_mult_table(high):
i = 1
while i < = high:
print_multiples(i, i)
i = i + 1

#prints n newlines using the while condition
def n_lines(n):
i = 1
while i < = n:
print
i = i + 1

#determines if n is a prime number
def isPrime(n):
i = n - 1
while i > 1:
rem = n % i
if rem == 0:
return False
else:
i = i - 1
return True

#prints all prime numbers between 1 and x
def morePrime(x):
i = 1
while i < = x:
if isPrime(i) == True:
print i, " is a prime number"
i = i + 1

#prints all prime numbers between x and y
def evenMorePrime(x,y):
if x > y:
x,y = y,x
i = 1
print “Finding all prime numbers between “, x, ” and “, y
print
while x < = y:
if isPrime(x) == True:
print x, " is a prime number"
x = x + 1

Posted in python |

4 Responses

  1. Yoshi Says:

    A few small notes for beginners. Note that python has its own comparison function, cmp(x, y). It works as such:


    def cmp(x, y):
    if x > y: return 1
    if x == y: return 0
    if x

    this works differently from you compare function, which might confuse the newcomers.

    Another problem is in your isEven and isOdd functions. You have to remember that the result of a comparison is a value (either True or False), and that if statements merely look at the value and execute their code if it is True. Therefore, the following code is perfectly legal:


    def isEven(x):
    return x % 2 == 0

    def isOdd(x):
    return not isEven(x)

    say we call the functions with 2 as an argument. the statement 2 % 2 == 0 evaluates to True, therefore the value True will be returned by isEven. the not keyword takes the value following it, and flips it. False becomes True and vice versa. Therefore, not isEven(2) evaluates to False, and that will be returned by isOdd.

    In general this code:

    if [comparison]: return True
    else: return False

    is completely unnecessary, and can easily be replaced with the following:

    return [comparison]

    one more note on comparisons: An if statement will also accept different values than True or False, and convert them to the appropriate boolean. (0, ”, [], {}, () all convert to False, everything else to True). Therefore the following statements are exactly the same


    if a == True: #code
    if a: #code

    #and of course these to
    if a == False: #code
    if not a: #code

    the second is shorter and thus always preferred.

    In short, comparisons and if statements are not unseparable. the results of comparisons can be assigned to variables returned, whatever you like. And an if statement will take anything that it can convert to a boolean value.

  2. Yoshi Says:

    I’ve been looking over this code some more and I have another remark: The for loop is your friend! A lot of the while loops in the code could be replaced with for loops easily, clarifying the code.

    Let’s consider an example:


    def print_multiples(n, high):
    i = 1
    while i

    using a for loop and the useful range function, the code becomes as such:


    def print_multiples(n, high):
    for i in range(high):
    print n*i, '\t',
    print

    You don’t have to initialize or increment the loop variable at all. It’s much shorter, and perfectly clear. Besides, it’s more pythonic.

    You can do this with a lot of the above functions. In short, if you simply want a counter loop, use for and with range (or, preferably, xrange).

  3. Yoshi Says:

    hmm, once again, part of my comment was eaten. I did write out the whole print_multiples function. This has happened to my a few times now. What’s causing it?

    looking further, everything from the ‘

  4. Yoshi Says:

    heh, I’m pretty sure now. It’s cutting off everything from the ‘less than’ sign to the end of the sentence.

    On another note, in changing the function I created a little bug. range(n) creates a list of numbers from 0 to n-1. That’s now what we want, so we use two arguments to give starting and stopping values. The correct line becomes:
    for i in range(1, high+1)

    even the best programmers write bugs ;-)

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.