Sign in to follow this  

Newbie Python Q's

This topic is 4203 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've been trying to learn Python, and I have two questions. First off, how does the follwoing this line work: a, b = b, a+b My brain tells me that it should be identical to: a = b b = a + b But apparently its not ;). Next, is this bit of code:

c = 0
d = 0

while c < 100:
    
  
    a = range(1, c / 2)
    
    for x in a:

        if (c / x) == 0:
            d = 1
            
    
    if d == 0:
        print c, ' is prime',
    
    d = 0
    c = c+1      





It's supposed to calculate all primes between 1 and 100, but it doesn't work. I think the problem iss in the use of the range() function, but once again I'm not too sure. Thanks for any help!

Share this post


Link to post
Share on other sites
If you have a, b = b, a + b it evaluates the right hand side before doing the assignment. So, for example, if a is 4 and b is 6, internally it does the moral equivalent of
a, b = b, a + b turns into
a, b = 6, 4 + 6 turns into
a, b = 6, 10 turns into
a = 6
b = 10

For the second part, it looks like you want a = range(2, c / 2)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
1) a, b = b, a + b:

It's assigning in parallel; that is, it is NOT implicitly sequencing the operations. The values on the right are calculated with the original values on the left. That's precisely WHY it is convenient. It saves you writing

temp = a
a = b
b = temp + b

2) I don't know where to begin.

You're doing integer division:

>>> 1 / 3
0

So you're not getting what you expect. You'll have to tell python in some way that you want REAL division, for example:

>>> float (1) / 3
0.33333333333333331

Your algorithm is, how should I say, weird, in that it says:

print b, ' is prime',

where b is a variable that hasn't been initialized in your code yet...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The second part should say a = range(2, c / 2),
and if (c % x) == 0:

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Your prime-finding algorithm looks so wrong as to be unintelligible, so instead of trying to fix it I'll just write one from scratch in simple python.

First, we realize this isn't the fifties and python supports procedural abstraction. So it might be a good idea to split our program into procedures. Splitting a problem into small manageable bits is generally a good idea.

A convenient function to have is one which takes a number and a list of numbers and checks if the number is divisible by any number in that list:


def divisible(anum, num_list):
for p in num_list:
if anum % p == 0: return True
return False



Note I used the modulo operator % instead of checking for remainders by doing floating point division. This says what I MEAN: 'check to see this INTEGER is divisible by that other INTEGER.'

I do NOT mean 'convert these integers into floating point values, divide them and check to see if I get the floating-point 0 back'. So I do not write what I do not mean.

Next, I think about numbers a bit, and notice that any number n that is not prime MUST have a divisor that is equal than or less than the square root of n. So I only have to check for divisors smaller than or equal to the square root of n. For that we need the int() function, which returns the first integer smaller than its floating point argument, and the sqrt() function, which returns the square root of a number. sqrt() is found in the math module, so we'll import it.

We can easily express the requirement of being prime in terms of the divisible() function: a prime is a number that is not divisible by the integers less than or equal to its square root except for 1:


from math import *

def is_prime(n):
root_n = int(sqrt(n))
return not divisible(n, range(2, root_n + 1))



Now, we wanted a list of primes up to a given number. Given the auxiliary functions we defined, this is easy to do:


def list_primes(upto):
for i in range(2, upto + 1):
if is_prime(i): print i



Let's try it:


>>> list_primes(29)
2
3
5
7
11
13
17
19
23
29



Seems to work.

Share this post


Link to post
Share on other sites

This topic is 4203 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this