Python 'for' function - python

I am trying to write a code that returns every prime palindrome with three digits. Here is my code:
def digpalprim():
for x in range (100,1000):
if prime(x)=='prime':
if str(x)==str(x)[::1]:
return x
I've already defined the prime(x) function, it works well, that stage just determines whether x is prime or not. All in all the code works, except that it only gives me the first such a palindrome. I don't really understand why, shouldn't the program consider all the numbers between 100 and 1000? Please help?

Your function returns as soon as it finds the first such palindrome; return exits a function.
Collect your finds in a list and return that:
def digpalprim():
palindromes = []
for x in range (100,1000):
if prime(x)=='prime':
if str(x)==str(x)[::1]:
palindromes.append(x)
return palindromes
or you can make your function a generator by replacing the return with a yield statement:
def digpalprim():
for x in range (100,1000):
if prime(x)=='prime':
if str(x)==str(x)[::1]:
yield x
Now you'll have to iterate over this function or use list() to 'pull' all values out:
all_palindromes(digpalprim())
or
for palindrome in digpalprim():
print(palindrome)

You are returning the function the first time you encounter one.
def digpalprim():
palprimes = []
for x in range (100,1000):
if prime(x)=='prime':
if str(x)==str(x)[::1]:
palprimes.append(x)
return palprimes
This creates a list at the start of the function and appends each valid palindrome prime to that list, then returns the entire list (after completing all loops) instead of returning just the first one encountered.
Just remember, if Python hits a return statement, it's going to stop function execution right there and return that value regardless of any additional loops or code you may intend to be executed.

The function returns and ends as soon as the first result is found.
You may wish to add the results to a list and then print out the list.

return x This statement causes the program to return to the calling function once this statement is encountered. To return all you may put it in an list. For e.g:
you can have a list called values and append it to it, and finally return it at the end

For such small tasks, I prefer using list comprehensions:
palindromes = [x for x in range(100, 1000) if (prime(x) == 'prime') and (str(x) == str(x)[::1])]
Or (equivalently):
condition = lambda f: prime(f) == 'prime' and str(f) == str(f)[::1]
palindromes = [x for x in range(100, 1000) if condition(x)]

Related

Why i don't get the same result? Is not the two code denote the same things?

Why don't I get the same result? Don't the two codes denote the same things?
Version 1
def fun(arg):
for i in range(0,arg+1):
return (f'{i} : {i**3}')
print(fun(10))
Version 2
def fun(arg):
value = {}
for i in range(0,arg+1):
value[i] = i ** 3
return value
print(fun(10))
In the first snippet, you return from inside the for loop, prematurely terminating it and returning a string formatted with i=0.
In the second snippet, you accumulate the results to a dictionary and return the dictionary only after the loop has completed.
No, it's not the same thing, in the first code, the value will be returned on the first iteration itself.
While in the second code, all values are aggregated and then, returned.
To know why it doesn't work you need to know the fact that return stops for loop, or any kind of loop.
So in your first case the return will stop on first iteration which is i = 0 and will return 0: 0.
In your second case, you are storing your values into the variable value and by doing that when you return value, you return whole set that for loop went through which is 0 to 10.
So the problem is that any loop ends when it finds the return keyword, if you want to do the same thing on one line you would have to use dict comprehension:
def fun(arg):
return {i: i**3 for i in range(0, arg+1)}

python what's the element in []

I recently studied a python recursion function and found that the recursion stops when it uses element in []. So I made a simple test function, found that there is even no print out. So how can I understand the element in []? Why does the function stop when referring to element in []?
b=1
def simple():
for i in []:
print('i am here')
return i+b
a = simple()
Python's in keyword has two purposes.
One use in as part of a for loop, which is written for element in iterable. This assigns each value from iterable to element on each pass through the loop body. This is how your example function is using in (though since the list you're looping over is empty, the loop never does anything).
The other way you can use in is as an operator. An expression like x in y tests if element x is present in container y. (There's also a negated version of the in operator, not in. The expression x not in y is exactly equivalent to not (x in y).) I suspect this is what your recursive code is doing. This would also not be useful to do with an empty list literal (since an empty list by definition doesn't contain anything), but I'm guessing the real recursive function is a bit more complicated.
As an example of both uses of in, here's a generator function that uses a set to filter out duplicate items from some other iterable. It has a for loop that has in, and it also uses in (well, technically not in) as an operator to test if the next value from the input iterator is contained in the seen set:
def unique(iterable):
seen = set()
for item in iterable: # "in" used by for loop
if item not in seen: # "in" used here as an operator
yield item
seen.add(item)
A recursive function calls itself n-number of times, then returns a terminating value on the last recursion that backs out of the recursive stacks.
Example:
compute the factorial of a number:
def fact(n):
# ex: 5 * 4 * 3 * 2 * 1
# n == 0 is your terminating recursion
if n == 0:
return 1
# else is your recursion call to fact(n-1)
else:
return n * fact(n-1)
In your example, there is no recursive call to simple() within the function, nor are there any element inside the empty list [] to step through, therefore your for loop never executed
Its concerned about mechanism of 'for loop'.
Superficially, the iterator you want to travese (which is "[]" in you example) has a length of 0, so the body of the loop (which include "print" an so on) will not be executed.
Hope it helps.

How to write a recursive function to print a string a certain number of times?

I have been trying to write a python function which will receive two arguments: x which is the number of times to repeat and y which is the number or string to be repeated.
So far this is what I have been able to come up with, but I can't seem to be able to determine how to do it recursively.
def recurse(x, y):
final = []
if x == 0:
return ("")
else:
x = x - 1
final.append(recurse(x,y))
return final
If recurse(3,"Cat") is called, the output is meant to look like this:
print recurse(3, 'cat')
['cat','cat','cat']
How would I go about doing this?
True functional recursion should have no assignments (i.e. state).
This can be achieved using what's called an accumulator, i.e. an array that is built up successively and passed to each recursive step, until the base case occurs, causing the accumulator's final form to be returned directely, and indirectly all the way back to the initial calling instance.
>>> def recurse(Iters, text, Accumulator=[]):
... if Iters == 0: return Accumulator # base case
... return recurse(Iters-1, text, Accumulator + [text]) # recursive step
...
>>> recurse(3, 'helle')
['helle', 'helle', 'helle']
Note that there is no change of state at any point; all "updates" occur at the point of calling the next recursive step with updated arguments.
def recurse(x, y):
final = []
if times == 0:
You can't just reference "times" as if it's some kind of magic floaty variable available anywhere and everywhere. The first time in here, times will have no value. And the next time through, you're hoping you can just pick the value of times out of thin air and it will magically be what it was somewhere else, during some previous run.
The only variables you can work with are the ones completely inside your function - or ones which are passed in from outside, and returned out at the end.
def recurse(times, word):
if times == 1:
return [word]
else:
return [word] + recurse(times-1, word)
print(recurse(3, 'cat'))
Try it online at repl.it
It takes a number of times as an input, it returns [word] plus ( It takes a number of times - 1 as an input, it returns [word] plus ( It takes a number of times - 1 as an input, it returns [word] ).
Note there's no accumulator list like final = [] because each time through the function it will be empty again. The accumulation happens implicitly in the way the programming language calls functions and returns values. Python will hold [word] in memory itself while it calculates the next recurse() output.

I have a generator in my Python function; how can I return a modified list?

A little background: I've been trying to code a "Sieve of Eratosthenes" algorithm. At the request of some fine (and very patient) programmers on the StackOverflow Python chatroom, I did some reading on the enumerate() function and found a way to incorporate it into my code (yes, I'm very much a novice here). So far, my (workable; returns expected response) code looks like this:
def SieveErat(n):
numbers = [False]*2+[True]*(n-1)
for index, prime_candidate in enumerate(numbers):
if prime_candidate == True:
yield index
for x in xrange(index*index, n, index):
numbers[x] = False
primes = []
for x in SieveErat(150000):
primes.append(x)
print primes[10002]
Needless to say, the enumerate() function makes coding this much, much less awkward than whatever nested loops I had before. However, I fear that I'm not understanding something about enumerate(), because when I tried to shorten this code by including the appending into the function, I kept getting errors- namely,
File "SievErat.py", line 13
return numbers
SyntaxError: 'return' with argument inside generator
I've also tried appending all the True elements inside the list numbers to a initialized list primes, but found no luck.
Any hints or advice would be very much welcomed.
This has nothing to do with enumerate you are trying to return something in a generator which before python 3.3 is illegal, and from 3.3+ it means something entirely different.
I'd recommend you leave your function a generator if you may use it without needing a list back, and if you want list results then just call list() on the return value:
primes = list(SieveErat(150000)) #this replaces loop with .append
But to understand what went wrong, if your function still has the yield statement in it then it must return a generator object, if you don't want it to return a generator object then remove the yield statement all together:
def SieveErat(n):
numbers = [False]*2+[True]*(n-1)
for index, prime_candidate in enumerate(numbers):
if prime_candidate == True:
#yield index #no yield statement if you want to return the numbers list
for x in xrange(index*index, n, index):
numbers[x] = False
return numbers #return at end
However this would then return a list of True and False instead of the numbers that are True, you can instead hold a separate list with all the prime numbers and .append to it everytime you would yield something:
def SieveErat(n):
numbers = [False]*2+[True]*(n-1)
result = [] #start with no results
for index, prime_candidate in enumerate(numbers):
if prime_candidate == True:
results.append(index) #instead of yield index
for x in xrange(index*index, n, index):
numbers[x] = False
return results
But this feels like a step backwards from a generator, personally I'd just keep what you have posted and cast the result to a list.

writing a function that return the reversed number

I am trying to write a Python function that get a number as input and returns its reversed number as output. for example: 1234 returns 4321.
this is what I try, but it return only ''
def reverse(num):
L=[]
x=str(num)
L1=list(x)
for i in L1:
L.insert(0,i)
print 'the reversed num is:'
x=''
for i in L:
x.join(i)
return x
any ideas?
def reverse(num):
return str(num)[::-1]
or one line:
lambda x: str(x)[::-1]
Well, the easy solution is this one:
>>> int(str(1234)[::-1])
4321
Your code can be fixed by changing the part
for i in L:
x.join(i)
return x
to
for i in L:
x += i
return x
Alternatively, just replace that section by
return ''.join(L)
What was wrong with your code? Because of wrong indentation, you returned in the first iteration of the for loop. You never assigned a name to x.join(i) so the return value was lost. What you expected join to do I do not know.
First, there is an easier way by converting to string, slicing the string and converting it back to a number.
def reverse(num):
return int(str(num)[::-1])
Second, there are multiple errors in your code:
1) your return statement is in the loop, so it will return after the first iteration;
2) x does not change because x.join() creates a new string and does not modify the string x (which is immutable by the way)
3) no need to convert the string into a list since you can directly iterate over the string (for i in x: ...)
4) join() takes an iterator as an argument. No need for the second loop: return ''.join(L)
thank you all for the helpful ideas.
here is my solution:
def reverse(n):
reverse=0
while(n>0):
dig=n%10
reverse=reverse*10
reverse=reverse+dig
n=n/10
return reverse
def reverse(num)
return str(num)[::-1]
Reverse a string in Python
Other users already gave good answers. Here is a different one, for study purposes.
num = 1234
print "".join(reversed(str(num)))
# 4321
You can convert to int afterwards.

Categories

Resources