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 that check for existence. If you recall in lesson one, we looked at how to flip a boolean. Your logged_in variable 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 write logged_in = not logged_in. The keyword not means the opposite. You have in and not in operators that check for existence and return a boolean.
One conditional check you can do is to see whether something is contained within something else. 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 state something true here. We'd write apple in fruits
.
And that's true. Now we want to state something false. We'll write bunny in fruits
, which returns False.
Now we have vowels. What about fruits[0][0]
? What's fruits[0]
? fruits[0]
is apple, right? We could say here “a.”
Let's try this just to verify something. fruits[0] == 'apple'
is True, and fruits[0][0] == 'a'
is also True.
So, is fruits[0][0]
in vowels? Yes—fruits[0][0]
is in vowels.
In other words, the letter “a.” If fruits[0]
is apple, apple[0]
is “a.” We're checking if “a” is in AEIOU—in other words, whether the first fruit starts with a vowel.
Check if the first fruit starts with a vowel. The first fruit is fruits[0]
; the first letter is index 0 of that string.
If that letter is in the vowel set, then that word starts with a vowel. Substrings: Is “ape” in “grape”? True.
Is “p” in “banana”? False. Is “X” in fruits[0]
? Well, fruits[0]
is apple, so there's no “X” in apple. Is apple in pineapple? Is fruits[0]
, which is apple, in pineapple? Totally.
One hundred percent. Yes—grasshopper.
Is “grass” in “grasshopper”? Or, let's try not in: “grass” is not in “grasshopper.” Let's try a couple false ones.
fruits[0]
is not in pineapple. That's False; apple is in pineapple.
False. “grass” is not in “grasshopper”—totally False.
“a” is not in fruits[0]
. “a” not in apple? False—“a” is in apple.
What about “X”? “X” is not in apple, right? And what about “z”? “z” not in fruits[-1]
—fruits[-1]
is watermelon—so “z” is not in watermelon, correct.
So it is true that “z” is not in watermelon. That's in versus not in. Normally, you wouldn't run these just to print the results.
This is just an exercise. You would run them in conditional logic with if statements and act based on what in or not in returns, okay?
With that in mind, we're going to declare a variable called vowels. We'll check if a word starts with a vowel. So apple—well, we know apple is fruits[0]
.
fruits[0] in vowels
is False—apple isn't in vowels. But the first letter of apple is, so fruits[0][0] in vowels
is True.
Banana: banana[0]
is “b”, and “b” in vowels is False.
Now let's look at the end of words. The last letter: apple[-1]
. Instead of using fruits[0]
, we'll just use index −1.
So, is the last letter in vowels? “e”—the last letter of apple—is a vowel, so that's True. “a”—the last letter of banana—is also a vowel.
But apricot[-1]
—the last letter of apricot—is “t”, which is not in vowels. We're checking all these yes/no in/not-in cases, from the first and last letters. Type this out.
You need to type it. All right—rolling into a challenge. Using in, check for the existence of a letter in a string. If a fruit starts and ends with a vowel, here’s the move:
We're going to loop through the fruits list. If the current fruit starts and ends with a vowel, make a tart.
That would be apple tart—starts and ends with a vowel. Else, if it starts with a vowel but doesn't end with one, make nectar.
Starts with a vowel but doesn't end with one—do we even have any of those? Apricot fits.
There's no apricot in here yet. All right, let's append apricot because it starts with a vowel and doesn't end with one—add a fruit that starts with a vowel but does not end with a vowel.
The only one I can think of off the top of my head is apricot—we only need one to make sure it works. fruits.append('apricot')
. We could think of another, but apricot is enough.
Elderberry? Nah, apricot is good enough. Next, else-if the fruit starts with a vowel but doesn't end with one.
If you reach this elif
, it doesn't start and end with a vowel, but it might still start with or end with one. If it starts with a vowel and doesn't end with one, make nectar—for example, apricot nectar.
Else, if the fruit ends with a vowel, make a roll-up—it doesn't start with a vowel but it does end with one. Banana roll-up.
Roll-up may be hyphenated. Else, make a jellybean—the fruit does not start or end with a vowel.
That would be peach jellybean, for instance. Save all to the treats list. Oh, apricot—there it is.
Remove duplicates—we already did that. We don’t have any duplicates, do we? Nah, we should be good.
Okay, so we're going to begin. Pause here and try to do this.
Give it your best. You’re making tart, nectar, roll-up, or jellybean, depending on the checks you run against the vowels list.
Use the vowels string—it doesn't have to be a list. It could be, but a string is fine.
Pause. Give it your best shot. Struggle a little—it helps you learn.
Come back. Okay—here we are: treats.
Vowels, loop: for fru in fruits.
If the fruit starts with—so we have fru[0]
in vowels—and ends with a vowel—fru[-1]
in vowels—remember the and operator. If the current fruit starts and ends with a vowel, we're going to make a tart.
treats.append(fru + ' tart')
. Else, if the fruit starts with a vowel. If it’s false that it starts and ends with a vowel, it could still start with or end with one—the or is still in play because and failed. We could say elif fru[0] in vowels
. (Put a comment here.)
elif fru[0] in vowels
—we're going to make nectar. elif
the fruit ends with a vowel—because it might end with a vowel even if it doesn’t start with one.
elif fru[-1] in vowels
—the last letter of the current fruit—then we're going to make a roll-up. Else, none of that's true; fru does not start or end with a vowel.
We're going to make a jellybean. All of that is going to be printed—print the treats list.
There it is—it worked: apple tart.
Oh, wait—apricot nectar. Yeah, there it is. We didn’t sort it.
We can run fruits.sort()
—see apricot earlier. There we go; every example is right there.
Starts and ends with a vowel: tart. Starts with a vowel but does not end with one: nectar. Ends with a vowel but does not start with one: roll-up.
A roll-up does not start or end with a vowel. Jellybean. And we’re not considering “y” a vowel.
You could add “y” if you wanted it to count as a vowel; doing so would create 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, and getting the first and last letters. Let's keep going.