![]() |
We can use if with logical not operator in Python. The main use of the logical not operator is that it is used to inverse the value. With the help of not operator, we can convert true value to false and vice versa. When not applied to the value so it reverses it and then the final value is evaluated by the if condition. So according to its final value the if block or else block is executed. If not Python SyntaxThe syntax for an if statement with the not logical operator is:
Why do we use the ‘If not’ Python StatementThe main purpose of using if with not is to conditionally execute code based on the absence of a specific value. This helps in handling such cases where certain elements are not present. Here the condition can be of any type boolean, string, list, dict, set, tuple, etc. Basic example of Python Not Python3
Output False Python if not with BooleanIn the code, If the value of the boolean is True then by the use of the not operator it becomes false so the statements inside the else block are executed. In case,when the value of b if False then by the use of not operator it becomes True and the statements inside the if block are executed. Python3
Output: Inside the if block Python if not in String ValueIn the code it is checked , whether the string is empty or not. if the string is null then its equivalent to false so by the use of not operator it becomes true and it is printed that “String is empty”. Python3
Output: True Check if a String is Empty or not in PythonPython if not in ListIn the code it is checked if the list is empty or not .If the list is empty it returns null which is treated as equivalent to false.After the use of not operator it becomes true so statement inside the if block are executed. Python3
Output: List is not empty Python if not in DictionaryIn the code it is checked if the dictionary is empty or not .If the dictionary is empty it returns null which is treated as equivalent to false.After the use of not operator it becomes true so statement inside the if block are executed. Python3
Output: Dictionary is empty Python if not in SetIn the code it is checked if the set is empty or not .If the set is empty it returns null which is treated as equivalent to false.After the use of not operator it becomes true so statement inside the if block are executed. Python3
Output: set is empty Python if not in TupleIn the code it is checked if the tuple is empty or not .If the tuple is empty it returns null which is treated as equivalent to false.After the use of not operator it becomes true so statement inside the if block are executed. Python3
Output: tuple is empty Fastest Way to Check if a Value exists in a ListIn the code it is checked if element is present in the list or not . If element is present then it returns true and then by the use of not it becomes false so else block is executed . If the element is not present then it returns false so by use of not it becomes true and if block is executed. Python3
Output: Element is present in the list Check if String Contains SubstringIn the code it is checked if substring is present in the string or not . If substring is present then it returns true and then by the use of not it becomes false so else block is executed . If the substring is not present then it returns false so by use of not it becomes true and if block is executed. Python3
Output: substring is not present in string |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |