Python Program for Logic Building | How to Improve Coding Logic in Python

A Python tutorial on counting consonants in a word by checking if each character is not in a vowel list. The presenter explains why this approach is more efficient than counting consonants directly, and demonstrates the `not in` operator for membership testing.
Chapters
Let us now see how we can count consonants in a given word. When it comes to counting the consonants, I am going to take the same method which we use for vowels. So vowels are only 5 in number, so I am storing them in the list format and in the string within the variable vowel. Then I am writing a variable word in which I am storing a word in which I am going to find out the consonants. So I am writing all in lowercase, then I am Creating one variable count to keep track of all the values. Count is equal to 0. For iteration, I am using for loop for character in word, and if the character is not in vowel, it means that is a consonant. And, and when we are founding— finding that, then it will— it means we are going to update the count variable. Now I am updating the count variable with 1, and then outside of the for loop, I am going to print the count. Now I will execute and will show you how many consonants do we have. So we have in total 6 consonants in this word. So this entire word is having 6 consonants, and I'm using the way with, with, with which you can even find out the vowels as well. Okay, so let us first go through the code. So what we are trying to do, we are find— trying to find out the consonants. But in English we have 26 letters. And out of 26 letters, we have 5 vowels and rest are consonants. It means the countable, which are easy to count, are vowels. It means 5 vowels and rest are consonants. Consonants are more in number, correct? So when something is more in number, we do not have to go towards that, because if we will go, we have to go track each and everything, and it is going to be time taking. And in coding, we have to reduce the time, we have to work on time complexity. So I'm creating vowel variable Creating a variable for storing numbers and then creating an empty container, empty container, so that later I can update it. Now I'm going to iterate over the character in the word. It means c. So if c is not in vowel, it means I am going to update the count variable. Remember what we are doing? We are using not in. It means we are using an identity operator in, not in. So we are using not in specifically, and then whenever we are not finding any character in the vowel, we are updating the count variable with 1. So this is how I will keep on updating. So I is already there in the vowel container, so I will not do. N is not there, so I will add 1 for it. And then for same for G, same for D, I am going to do. So plus 1, plus 1, plus 1, and plus 1 are 6 in total. And this is what we are getting outside of the for loop, because outside of the for loop we will get only the updated last value of count. So it is very simple. You can even write down the same code for vowels. When I am using just in operator, it will count the number of vowels. But I am using— if I am using not in operator, it will count the consonants in the variable. So easy, super easy with the help of operators. So you need to learn operators in the detail of Python, which is easy.










