if-else - II





Hello Folks.....!!! 
Today we are going to continue with the conditional statement if-else.

'in' operator :

  • It is a membership operator.
  • This is used to check whether a particular item exists in a list or tuple or set or dictionary.
  • Here the conditions can be written as simple English sentences.
  • Now imagine You need to check whether the pen is written in the list of items to be purchased. How will you ask your friend ? Are the pens in the list ? The same we have to do here. Consider this scenario. Let the list contain pencils, pens, papers, sketches. Look at the code below :
in operator

Now let us continue with data structures and if-else conditions.
Let us see how to check conditions for 2D lists.

Python if-else

Here there is a 2D-list 'li', where we are getting the index of element to be checked as user input. Then that element is checked whether it is less than or greater than 10 and the appropriate output is printed out.

Using 'in' operator between two lists 

Check whether one element is present in another list. Let us check the code:

Check whether one element is present in another list

Python allows you to check whether an element is present in an iterable without using any loops. It checks all the elements and returns the value.

if-else and Python dictionaries:

Consider a students record, If the ID of the student is given, his/her details should be shown. See the code below :

Python-dictionary and if-else

here, the ID to be checked is read as input. Now we check whether it is a key in the dictionary. for that we write if inp in stu.keys().  Recall the inbuilt functions of dictionary. The dict_name.keys() is used to iterate only through the keys of the dictionary.  Now if you want to display only the name, then write as :


Python-dictionaries and if-else

if-else on nested dictionaries:

Consider the scenario, If the given ID and name are correct, the student's age must be displayed. Otherwise, if the ID alone is correct, then the correct name must be displayed. If nothing is correct, then "no record found!!" will be printed. See the code below :

case 1: Both the ID and name are correct.


Python-dictionaries and if-else

case 2: only ID is correct.


Python-dictionaries and if-else

case 3: Nothing is correct.


Python-dictionaries and if-else

In the similar way you can even check whether the given String or an integer or a float is present in the list/dictionary or any other data structure.

'is' operator :

It can be used in the place of '=='. It can be used to conditions as below :
If-part works:

Python is operator

else-part works:


Python is operator



Exercise:
Consider the paragraph below:
p1 = "The dog is a pet animal. A dog has sharp teeth so that it can eat flesh very easily, it has four legs, two ears, two eyes, a tail, a mouth, and a nose. It is a very clever animal and is very useful in catching thieves. It runs very fast, barks loudly and attacks the strangers. A dog saves the life of the master from danger. One can find dogs everywhere in the world. Dogs are a very faithful animal. It has a sharp mind and a strong sense of hearing smelling the things. It also has many qualities like swimming in the water, jumping from anywhere, good smelling sense."

1. Find whether the following words are present in the Paragraph. If present, print "The word is present!!!" else print "Not present."
  • dog
  • lizards
  • sharp
  • cute
2. Check the frequency of following words and remove them from the paragraph if their count exceeds 
  • dog
  • sharp
  • animal
  • 'a'

Next Topic👉

Comments