Discover how Python's in
and not in
operators simplify checking for the existence of letters or substrings. Learn practical examples that illustrate creating conditional logic based on vowel checks.
Key Insights
- Demonstrates the use of Python's Boolean operators
in
andnot in
to determine substring existence, such as checking if "apple" is in "pineapple" or if "grass" is not in "grasshopper." - Explains conditional logic using loops and string indexing to assess if the first and last letters of fruits (e.g., "apple," "banana," "apricot") are vowels, which guides the assignment of treats like "tart," "nectar," and "roll-up."
- Illustrates how to append items to a list conditionally and perform sorting operations, reinforcing foundational Python skills through practical exercises.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
This is a lesson preview only. For the full lesson, purchase the course here.
There are in and not in operators to check for existence. If you recall in lesson one, we looked at how to flip a boolean. You have logged in, could be true or false.
If you'd like to flip it without having to know or care what the current value is, you would just say logged in equals not logged in. So not means the opposite. You have in and not in operators which check for existence and return a boolean.
We could say, so one conditional check you can do is just to see if something's in something. Is a fruit in a list, is a letter in a word. So let's take the in and not in operators for a spin with these little examples.
We have apple. Well, we want to say something true here. We'd say apple in fruits.
And that's true. Now we want to say something false here. We'll say apple in fruits, excuse me, bunny in fruits, false.
Now we have some, we have vowels. What about fruits? Zero, zero. What's fruit zero? Fruit zero is apple, right? We could say here, apple.
Let's try this just to verify something. Let's make sure we know what we're doing here. Okay.
So, so fruits is zero equals apple. That should be true, right? It is. And fruits zero, zero should be a, right? Yeah.
So here you have a bunch of vowels. Is fruits zero, zero in vowels? Yes. Fruit zero, zero is in vowels.
In other words, the letter a, right? Fruit zero, zero. If fruit zero is apple, apple zero is a. We're asking, we're checking if a is in AEIOU. In other words, we're really checking to see, checking to see if first fruit starts with vowel is what we're doing there.
Check if first fruit starts with vowel. So the first fruit is fruit zero. The first letter is zero of that string.
And if that letter is in the vowel set, then the, that word starts with a vowel. Um, substrings. Is ape in grape? True.
Um, is P in banana? False. Is X in fruit zero? Well, we know fruit zero is apple, so there's no X in apple. Is apple in pineapple? Is fruit zero, which is apple, in pineapple? Totally.
A hundred percent. Yes. Grasshopper.
Is grass in grasshopper? Or no, let's try not in. Grass is not in grasshopper. Let's try, um, let's try a couple false ones.
Fruit zero is not in pineapple. That's false. Apple is in pineapple.
False. Grass is not in grasshopper. Totally false.
A is not in fruit zero. A not in apple? False. A is in apple.
Um, what about X? X is not in apple, right? And what about Z? Z not in fruits negative one. So whatever negative one is, watermelon, Z is not in watermelon. Right? Negative one, fruits negative one should be watermelon, and there is no Z in watermelon.
So it is true that Z is not in watermelon. So in versus not in. And you wouldn't just run these for, to print them.
This is just an exercise. You would run them in conditional logic with if statements and do things based on the not, what the not in or not in returns. Okay.
We're going to, with that in mind, we're going to go declare a variable called vowels. We're going to check if a word starts with a vowel. So apple, well, we know apple is fruit zero.
Apple fruits zero in vowels. Fruit zero is in vowels. That's false, right? Apple is not in vowels, right? You look at, you look at AEIOU, there's no apple in here, but the first letter of apple is in there.
So fruits zero, zero in vowels is true. A or apple is in AEIOU, right? So it's true. Banana, here, let's just say banana zero.
Is banana zero in vowels? B is banana zero. B is not in vowels. So you could call the zero on just the string.
You don't have to have it, you know, looking it up from the fruits list. So banana. So the B for banana is not in vowels.
Let's see if words, let's look at the end of words. The last letter, apple, negative one. We'll just, instead of using fruit zero, we'll just say negative one.
So the last letter is in vowels. E, last letter of apple is in vowels, right? It is a vowel. Banana, A, the last letter of banana is also in vowels.
But apricot, negative one, the last letter of apricot, which is T. T, last letter, let's do that, last letter of apricot is not in vowels. We're checking all these yay, nay, in, not in, from the first letter, from the last letter. Type this out.
You need to type it. All right, rolling, check, challenge, using in, use in to check for existence of letter in string. If the fruit starts and ends with a vowel, so here's the move.
We're going to loop the fruits. Loop the fruits list. If the fruit, if the current fruit, right? If fru, you know, if your current fruit as fru starts and ends with vowel, make a tart.
So that would be apple tart, right? Starts and ends with vowel. Else, if it starts with a vowel, but it doesn't end with a vowel, make nectar. That would be nectar.
It starts with a vowel, but doesn't end with a vowel. Okay, that would be, do we even have any of those? Starts with a vowel, doesn't end with a vowel. Apricot.
There's no apricot in here though. All right, let's append apricot because that starts with a vowel, doesn't end with a vowel. Add fruit that start with vowel, but do not end with vowel.
The only one I can think of off the top of my head is apricot, which we only need one to make sure it works. Fruits dot append apricot. We can think of one real quick.
Elderberry. Nah, apricot's good enough. We'll have, else if the fruit starts with a vowel, right? It doesn't end with a vowel.
If you get to the L, if you get past this if, then it does not start and end with a vowel, but it might still start with a vowel or end with a vowel. If it starts with a vowel, doesn't end with a vowel, make nectar. We'll say apricot, nectar.
Else if the fruit ends with a vowel, make a roll-up. So it doesn't start with a vowel, but it does end with a vowel. Banana roll-up.
Roll-up maybe being hyphenated. Else make a jellybean. The fruit does not start or end with a vowel.
That would be peach jellybean, say. Save all to treats list. Oh, apricot, there it is.
Remove duplicates. I think we already did that. We don't have any duplicates, do we? Nah, we should be good.
Okay, so we're going to begin. Okay, so pause. Try to do this.
Give it your best. You're making tart, nectar, roll-up, or jellybean, depending. And you're checking in the vowels list.
The vowels string. It doesn't have to be a list. It could be a list, but string.
Pause. Give it your best shot. Struggle.
Come back. Okay, here we are. Treats.
Vowels. If. No, we've got a loop for fru in fruits.
If fruit starts with, so we have fru zero in vowels and fru negative one. Remember the and operator in vowels. If the current fruit starts and ends with a vowel, we're going to make a tart.
Treats.Append.FruTart. Else, if the fruit starts with a vowel, right, if this is false, if it's false that it starts and ends with a vowel, it's possible that it starts with or ends with a vowel, right? The or is still in play just because the and is not. So, we could say elif, elif fru zero in vowels. Put a comment here.
Elif fru zero in vowels. We're going to make nectar. Elif fruit ends with vowel because what if it, you know, doesn't start and end or start, but it might end with a vowel.
Elif negative one, fru negative one, the last letter of the current fruit, then we're going to make a roll up. Else, none of that's true. That means else fru does not start or end with vowel.
We're going to make a jelly bean. All of that is going to get printed. He printed the treats rather.
There it is. It worked. Apple tart.
Oh, wait, apricot nectar. Yeah, there it is. Didn't sort it.
We can say fruits.sort. See the apricot earlier. There we go. There's every example right there.
Starts and ends with a vowel. Tart starts with a vowel, but does not end with a vowel. Nectar ends with a vowel, but does not start with a vowel.
Roll up does not start or end with a vowel. Jelly bean. And we're considering, we're not considering the Y a vowel.
I mean, you could, you could add it if you wanted Y to be a vowel. You could add it. And now you would have all these additional roll ups.
But it doesn't matter as long as we understand what we're doing. Okay. There's that.
Another loop, more conditional logic, reviewing, selecting, getting the first letter, the last letter. Let's keep going.