<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>
<Below this line, add a link to the EXACT exercise that you are stuck at.>
<In what way does your code behave incorrectly? Include ALL error messages.>
<What do you expect to happen instead?>
I am trying lambda function in pycharm and for some reason it does not run as it does in editor of code academy. I heard about python 2 vs 3 being used. I just wanna test things learned in code academy in different editors. the error is as follows: basically it doesn’t do anything. thanks!
<filter object at 0x100741780>
list = range(20)
print (filter(lambda x: x % 2== 0, list))
As a sort of supplement but with code that is highly improbable in its present form except for demonstration and study, here is an implementation of the above…
def fn1():
return lambda x: x % 2 == 0
def fn2(is_even, r):
return list(filter(is_even, range(r)))
def fn3(evens):
return list(map(lambda x: x + 1, evens))
def main():
r = 21
s = fn1()
t = fn2(s,r)
u = fn3(t)
while True:
done = 0
for b in u,s,t:
try:
b.__next__()
except:
done += 1
if done == 3:
break
print (t)
print (u)
main()
No, I did not conceive of the loop(s) although I certainly recognized the potential on first glance at the code I stole them from, The forest example from the Turtle samples in IDLE. I just thought it was so cool that we could iterate through a list of variables that are themselves tied to one or multiple functions. It bears further study, that’s for sure. But, don’t let it scare you or interfere with your track studies here. This is just a sidebar.
One more question. I tried something new. But i guess I don’t understand what “invoke” means. can you elaborate for the following example that didn’t go through. Sorry! Thanks again!
I am just concerned that after code academy i still need to learn new ways to be able to work on python 3
You’re always going to have to learn more, and you can’t know it all. So really you should get used to the idea that what you’re really only ever learning is overall concepts, making observations, looking up and applying details.
The biggest difference in Python3 is handling unicode, something you probably haven’t touched on at all. Everything else is really just something you have to take in your stride.
The same concepts apply to both languages, but they may use them differently in a few places.
For example, that filter object: What that is, is an object that knows how to filter through your other iterable. It hasn’t done it yet but will do it when you request values from it. This is called lazy evaluation and the opposite is called eager evaluation. Objects like that exist in both versions, it’s just that a few things are using them instead of lists.
Also, I have nothing kind to say about IDE’s, they hide what’s going on and you learn the IDE instead of the tools you’re really using. And when things go wrong you might not know how to fix it - when it blows up the first thing to do is close the IDE and pull up a terminal and use the real tools.
You’re free to prefer an IDE, just keep in mind that in no way is your IDE the universe. Java and C# are “IDE languages” with highly integrated IDE’s which also work quite well. Python doesn’t really give an IDE much to work with (because of type-less variables (values know their types, variables are just names) so I find Python-IDE’s a whole lot more questionable.
Python is very self-aware (reflective) so I think it makes more sense to interact with Python directly than to use an IDE. (Run the code and drop into interactive mode and then poke around while it’s running)