10/14 Try it!


squares = [x**2 for x in range(1,11)]
filter(lambda x: x >= 30 and x <= 70, squares)

I don't really 100% understand this lambda. I know that it creates a new function much faster than a def function. I don't understand how to do this lesson. Thanks.


Hello, @drewteriyaki!
Lambda is "simply" another way of creating a function, maybe it's even faster.

Q: Okay, I knew that already but how does it prints only specific values? And what is that "squares" used for?
A: It works this way: you'll first check if each element at squares, which is your iterable, follows your conditions which is to be greater or equal to 30 and less or equal to 70. If that so, it will return the value of it.

But will your condition be followed if I don't use filter()? No. The function filter()which will filter, oh well couldn't really find another way to say this, the elements that will be returned. It's a similar idea to store specific values into a list if they follow a condition, where you check using an if statement.

In fact if you try to print:

print lambda x: x >= 30 and x <= 70, squares

You'll get all the values that are in squares plus something like this:

#Had to add spaces between '<' and '>' otherwise it wouldn't show up here
#but in your compiler, it will show up with those spaces.
function  < lambda > at 0x115b160

But if you try to print:

print filter(lambda x: x >= 30 and x <= 70, squares)

You'll get a list contaning the elements that follow your conditions.

Here's the definition of the function filter():

filter(function, iterable):
Construct a list from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also has that type; otherwise it is always a list.

Just for the record: if you try to store lambda x: x >= 30 and x <= 70, squares into a variable and use type(), you'll see your variable is a tuple, since you'll have the place in memory where your lambda is and the values from the list.
But if you store filter(lambda x: x >= 30 and x <= 70, squares) into a variable and use type(), you'll see your variable is now a list where you'll be able to do all that list manipulation things that are asked for you to do every now and then.

If I wasn't clear at a certain point or my answer wasn't good, let me know and I'll find another way to explain!

Edit: filter() function definition


Yes thank you, it was clear. I realized that all I needed to do was just print the filter. I thought i was crazy or something thinking that my code wasn't right.


No problem, Drewteriyaki! Glad to help!


Tell me, after you learned Python do you think that learning other languages would be easier? I have already tried to learn a little bit of Java and it seems extremely easy because I know the concepts. Do you think computer science is a good major because I'm about to go into high school and I need to decide what I need to focus on?


@drewteriyaki: Actually, I'm still learning Python.
Even though I've finished the course and I'm currently doing the APIs coursers, there's still a lot of things that I should learn or understand better other than saying "This works this way" without really understanding what's going on behind the screen.
I still don't know everything about C++ or Python but I search on the Internet to find the answer for my questions, after trying to solve it by myself :slightly_smiling:

But okay, after you have a background in programming, considering you've programmed only with high level languages, you'll see that one is similar to another, in some ways.

For example:

int value = 10;
if (value > 5){
   std::cout << value << "is greater than 5!";
   std::cout << value << "is less than 5!";

value = 10
if value > 5:
    print " %s is greater than 5!" % value
    print " %s is less than 5!" % value

As you can see, both languages are similar but C++ doesn't mind, much, about indentation, while Python will freak out about it.

CS is a really good major but you gotta be strong! Many people get into the course and after the first semester, not many people are left.
Q: So should I gave up?
A: At the first two years of my major I thought about giving up, thought I didn't like programming but the thing is: I do! The problem was the language I was using, C++, that I'm not a huuuge fan of it but I do code. It's not easy but it's 100% WORTH IT.
Just remember: each time you code and you fail, instead of calling yourself "idiot", "dumb" and those bad things, think that you're SO smart that you found a way that your code won't work!
And don't think all codes will be done in an hour, there are times you take weeks to finish one but when you do, it's so beautiful :slightly_smiling:
Learn other languages but remember to go back to the one you've started with so you won't forget the details!

Hope my answer was good and hope that you become a CS major! Focus on doing great things, do your job (which is to study) and great things will happen!!