Check out a free preview of the full Intermediate Python course
The "Conditionals" Lesson is part of the full, Intermediate Python course featured in this preview video. Here's what you'd learn in this lesson:
Nina demonstrates to utlilize if statements within list comprehensions, and reviews truthy and falsey statements.
Transcript from the "Conditionals" Lesson
[00:00:00]
>> Nina Zakharenko: We can also use conditional statements like if statements in our list comprehensions to make them even more powerful. So before I had square, right, for num in range of 6, I printed out num x num.
>> Nina Zakharenko: What if I only wanted to do this for the even numbers?
[00:00:31]
>> Nina Zakharenko: I would use an if statement, and a quick thing about the condition we're gonna use. Does everyone know what the modulo operation is? Modulo, okay. So I'll explain it very quickly. Modulo operation is the remainder that you get from division. So 6 modulo 2, you do modulo with the percentage sign, it's gonna be 0.
[00:00:56]
>> Nina Zakharenko: We wanna check if our number is divisible by two, that's how we know that it's even. But there's one little gotcha here. We learned about truthiness yesterday, right? And that we can quickly check the truthiness of something by passing it into bull. So we know that positive numbers are truthy, we know that negative numbers are truthy as well.
[00:01:24]
Which numbers are falsey in Python?
>> Speaker 2: 0.
>> Nina Zakharenko: 0, so we want to check if 6 is module 2. What if we forget to do an extra check and actually check if it's 0, will this expression be truthy or falsey? So it will be falsey, right? If I check by passing this whole expression into 2 it's false, right?
[00:01:52]
So what we actually want to check is if the number modulo 2 is equal to 0. This is a little kinda subtle bug that still gets me occasionally in my own programming. So this is one of those cases that we want to explicitly check or we are gonna get the wrong result, okay.
[00:02:12]
Going back to our squares here and you can just use the up arrow to scroll up in the raffle. My conditional goes on the right side. So first, I have my result, next I have my for loop, and last in the formula I have my if statement, I put my condition here.
[00:02:38]
So based on what we learned above, how would I check, how would I only return this if the numbers were even? What would my expression be?
>> Speaker 2: If num % 2 = the quantity 0.
>> Nina Zakharenko: Equals quantity 0, right? So that will only give me the even squares.
[00:03:00]
Let's say I had this list of even squares, and I'm gonna assign it to a variable so that I can work with it a little bit easier. Knowing everything that we know now, we know that these even squares are integers, how would we print out a comma separated list of these values as a string?
[00:03:25]
How can we combine these operations? What are the different pieces?
>> Nina Zakharenko: How do we take a list and combine it into comma separated values?
>> Speaker 2: With join.
>> Nina Zakharenko: With join. And what is on the left side of the join equation?
>> Speaker 2: The delimiter.
>> Nina Zakharenko: The delimiter. That's right.
[00:03:52]
What am I joining on? We know that even squares is my list of numbers and that join takes a list, or list comprehension, right? Or list comprehension returns the list. So what am I looping over?
>> Speaker 2: Even squares?
>> Nina Zakharenko: Yeah, I can say for even_square in even_squares, it's what I'm looping over.
[00:04:21]
But I know that join takes a list of strings, and we have a list of integers, how do we make that conversion?
>> Speaker 2: Would str(even_square)?
>> Nina Zakharenko: That's right, str(even_square). That converts from any other type to its string representation. So now we have a list of strings and we can call join on it.
[00:04:50]
If I press Enter here, we get our result. That's kinda how you start thinking putting some of these pieces together.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops