Jorge Gandara
Jorge Gandara

Reputation: 71

Multiple Return Calls In Function

I'm trying to wrap my head around how the return call works in a function. In the example below, I'm assigning 5 to number1 and 6 to number2. Then I return both below. When I print the output, I only get "5" as a result.

Can someone please explain why it's doing this? Why does it not print both numbers?

Thanks!

def numberoutput ():
    number1 = 5
    number2 = 6
    return number1
    return number2

print (numberoutput())

Upvotes: 0

Views: 76

Answers (3)

PM 2Ring
PM 2Ring

Reputation: 55489

Here's a reasonably efficient way to find primes with a list comprehension, although it's not as efficient as the code by Robert William Hanks that I linked in the comments.

We treat 2 as a special case so we don't need to bother with any higher even numbers. And we only need to check factors less than the square root of the number we're testing.

from math import floor, sqrt

primes = [2] + [i for i in range(3, 100, 2) 
    if all(i % j != 0 for j in range(3, 1 + floor(sqrt(i)), 2))]
print(primes)

output

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

Here's an alternative (less efficient) version that iterates over your list1 (which isn't really a list, it's a range object).

list1 = range(2, 100)
primes = [i for i in list1 if not [j for j in list1 if j*j <= i and i % j == 0]]
print(primes)

Upvotes: 0

alexis
alexis

Reputation: 50218

Here's a compact way to do the loops that you ask for. Your lists should not contain 1, though.

>>> list1 = list(range(2,11))
>>> list2 = list(range(2,11))
>>> primes = [a for a in list1 if all((a % b) != 0 for b in list2 if a != b) ]
>>> primes
[2, 3, 5, 7]

There are no duplicates in the results, because the comprehension just collects elements of list1. But there are plenty of ways to improve prime number detection, of course. This just shows you how to apply comprehensions to your algorithm.

Upvotes: 3

Adrien Logut
Adrien Logut

Reputation: 820

Try this (change 10 by the number you want)

primes = []
for number in range(1,10):
    is_prime = True
    for div in range(2, number-1):
        if number % div == 0:
            is_prime = False
            break
    if is_prime:
        primes.append(number)

Be careful though, this is not efficient at all. A little improvment is to change (number - 1) by int(sqrt(number)). But that's math rules. If you want the first 1000000 primes, that won't work. You wanna perhaps check more advanced methods to find primes if you need more.

Explanation: you iterate first with all numbers between 1 and 10 - 1 = 9. This number is store into the variable "number". Then you iterate other the possible dividers. If the modulo for each pair of number and divider is 0, then it is not a prime number, you can mark it as not prime (is_prime = False) then quit your loop. At the end of the inner loop, you check the boolean is_prime and then add to the list if the boolean is set at True.

Upvotes: 0

Related Questions