Learn to Program 13 Iterables : List Comprehensions : Generators

preview_player
Показать описание


In part 13 of my Learn to Program tutorial we'll be covering some very powerful Python tools. We'll look at Iterables, List Comprehensions, Generator Functions and Generator Expressions. List Comprehensions have the power of Map and Filter all in one place. Also there are a bunch of problems for us to solve.

Thank you to Patreon supports like the following for helping me make this video

@kyleaisho
@thetwistedhat
vjFaLk
Рекомендации по теме
Комментарии
Автор

Great video! These are the features that really sold me on python. This whole series has been exceptional. Thank you!

If you wind up returning to this in the future, would you consider covering decorators and coroutines? I think those, like generators, are some of the more advanced language features in python that are really interesting. I'm sure many people would benefit from your treatment of them.

satoristeve
Автор

Derek u r the greatest person in youtube, thank you man for all these videos that you made! hopefully you will be bigger in youtube! and i want to ask you something! [19M] I am in my third four-month period in Software engineering at University! I know some C and C++ (not too much, but i do something), right now i am learning Java (specifically, abstract class), i learned HTML, CSS, XML and a little JS (JQuery) but i keep improving! I am thinking to take Database for specialty in a future. I want to hear some advices from you! and i have a question, What programming languages can you recommend me to learn in my free time nowadays?

Sorry for my english! Wish you success from Dominican Republic!

ivanvalera
Автор

Thank you so much Derek for all these amazing Tutorials! This one was a bit on the difficult side but that's how we'll raise the bar. Highly appreciate all your efforts! :)

NehaSinghon
Автор

Excellent tutorial, Derek. One of the best I've seen on the subject. . .BTW, you have almost half a MILLION subs now!!! Congratulations.

Colstonewall
Автор

Love these videos have to keep hitting the pause button and replaying them though - not a criticism

alanrobinson
Автор

Thank you Derek Banas, I learn alot from your videos. Keep up the good work.
Regard from Indonesia

sitizubaidah
Автор

Hi Derek,
at first: you make the best videos i've ever seen to learn programming stuff !!! And i am very thankful about that.

But i think in this video i found something wrong: look at 11:10
There you have this line of code:

print([i ** 2 for i in range(50) if i % 8 == 0])

The result is:
[0, 64, 256, 576, 1024, 1600, 2304]

I think this is not what you wanted. You wanted to filter the already powered values.
You get the result by doing this:

print(list(filter(lambda x: x % 8 == 0, [i ** 2 for i in range(50)])))

Result:
[0, 16, 64, 144, 256, 400, 576, 784, 1024, 1296, 1600, 1936, 2304]

It is a little bit confusing, because i think nobody wants to filter the not yet calculated temporary list like spnwinchester2 explained. is there a bug in python? or is this the way it should work?

eliahreimers
Автор

print([x**2 for x in range(1, 51) if x**2 % 8 == 0]) is the correct code at 11:28, which outputs:
[16, 64, 144, 256, 400, 576, 784, 1024, 1296, 1600, 1936, 2304]

notice that my code checks x**2 while yours checks the original x

matthewschmuckler
Автор

I have a question at 6:37 about why the Fibonacci sequence doesn't start with 1, 1, 2 instead of just 1, 2, 3, 5, 8. It dropped the second 1. I am very confused by that.

malic_zarith
Автор

Hey man! Love the videos and have learned a lot from you. I was wondering if you could do C or would that be a too difficult subject? Anyway keep doing what you're doing!

hansgoor
Автор

I noticed a pattern in one of your list examples

print( [i*i for i in range(50) if i % 4 == 0] )
# for values of i squared divisible by 8, every 4th value is put in the list

print( [i*i*i for i in rage(50) if i % 2 == 0] )
# for values of i cubed divisible by 8, every 2nd value is put in the list

SongSeeker
Автор

Hey Derek, could you maybe explain why

print([i * 2 for i in range(5) if i % 2 == 0])

OUTPUT: [0, 4, 8]


ISN'T THE SAME AS:

print([x for x in [i * 2 for i in range(5)] if x % 2 == 0])

OUTPUT [0, 2, 4, 6, 8]

XxTheIlluminatixX
Автор

Thanks for the vids Derek! Tuts have been very helpful!

my solution for first problem:

print([random.randint(1, 1001)for i in range(50)if i % 9 == 0])

Sipulikeittohomo
Автор

Great video as always but i want to point out something that's seems rather odd. The task of generating 50 values and then take them to the power of 2 and return multiples of 8 it's not quite accurate because the results are 0, 64 etc. but it should be 0, 16, 64 etc. Isn't that right? I was trying to figure out the reason why and my conclusion is that the condition checks the "i" before it's multiplied by 2. I don't know if i get it wrong but I would like your insight on that, thanks.

condafarti
Автор

Awesome video as always Derek! I would like to ask how it is possible for you to know so many programming languages?

Prof.Respect
Автор

I appreciate these videos very much thanks....

dalekriens
Автор

Hi Derek. I've got a good understanding of c++ (inheritance, operator overload, polymorphism, ect..) Is c++ a good language to keep learning in 2016, or should i learn another programming language like java or python?

marinmaksutaj
Автор

11:20 Problem: "print([x * x for x in range(1, 51) if x % 8 == 0])" this way skips zero and "print([x * x for x in range(50) if x % 8 == 0])" starts from 0. which one is good to use?

AxmedShiimax
Автор

First of all, thanks for these great tutorials! I have to learn Python and your videos make it very easy for me!
Halfway through this video, you create code to generate 50 values, take the power of 2 and return only the multiples of 8. However, the outcome is not correct. At least one value is missing: 16. How can this be explained?

GTJNWS
Автор

why you didn't use next in that for loop in that alphabetical program

manishadwani