Register Login

Python 'If not' Syntax

Updated Apr 03, 2023

Conditional statements help users to implement their logic and handle conditions in their program. Any programming language supports conditional statements like if, if-else, if-elif-else, etc.

In Python, users can use the if not statement apart from these conditional statements. This article will discuss Python's "if with not operator," which users can use with Boolean, List, Dictionary, Tuple, String, or set.

What is an if-not statement in Python?

Users can use the If with not in Python to check whether the variable is empty or assigned with some values. This variable can be List, Tuple, Dictionary, String, Boolean, Set, etc.

The Python if not statement helps users to implement logical decisions and returns the negation value of the “if statement”. When users need to check if a particular condition is not satisfied, they can extensively use the 'if not' Python operator in two factors:

In Python, not is a logical operator that evaluates to True if the expression used with it is False. This operator is used along with the if statement, called if not statements. 

To return the negation of the if statement and to check whether an iterable is not empty.

Sytnax:

if not value:

statement(s)

This value can be Boolean, String, Integer, List, Dictionary, Set, etc.

Examples of if with not operator in Python:

  1. if not with Boolean
  2. if not with Integer
  3. if not with String
  4. if not with List
  5. if not with Dictionary
  6. if not with Set
  7. if not with Tuple
  8. if not with multiple conditions with integer

Example 1: if not with Boolean:

Code Snippet:

x = 5
if not x:
    print("This will return True")
else:
    print("This will return False")

Output:

Explanation:

In the above example, we have assigned a variable with the value 5. When we assign a variable with Numeric Zero or 0, None, or an empty value, it will return False.

Here, we assigned a numeric five in the variable "x," which indicates a True value. But then if with not operator will make it False (since the not returns the negation of the if statement).

Hence, the code block will execute the else part of the program.

Again if we assign None then it wil return:

Code Snippet:

x = None
if not x:
    print("This will return True")
else:
    print("This will return False")

Output:

Example 2: if not with Integer:

Code Snippet:

x = 5
if not x > 5:
    print("This will not return True")
else:
    print("This will not return False")
if not with String

Output:

Explanation:

The if with not operator will return either true or false based on the condition assigned to the statement. Here, we used this conditional statement with an integer and set a condition to check whether the value assigned to the variable "x" is greater than five.

The condition will return the negation of the if block.

Example 3: if not with String:

Code Snippet:

today='It rains'
if not today=='It rains':
    print('He does not need an umbrella')
else:
    print('He needs an umbrella')

Output:

Explanation:

In the above example, we have assigned a variable with the string value "It rains." Here, we assigned a condition that evaluates to true. But then if with not operator will make it False (since the not returns the negation of the if statement).

Hence, the code block will execute the else part of the program.

Example 4: if not with List:

Code Snippet:

a = [3]
if not a:
    print(a)
else:
    print('List is empty.')

Output:

Explanation:

In this example, we used a list with one value. As per the if statement, it will return the list showing the value assigned to it. The if with not operator will make it False (since the not returns the negation of the if statement).

Hence, the code block will execute the else part of the program.

Check if element present in list:

Code Snippet:

list = [3,56,34,23,56,32]
a = 56
b = 33
if not a in list:
    print('No ', a ,'Not in list', list)
else:
    print('Yes ', a ,'in list', list)
if not b in list:
    print('No', 'Not in the list', list)
else:
    print('Yes', b, 'in list', list)

Output:

Example 5: if not with Dictionary:

Code Snippet:

dir = dict({})
if not dir:
    print('Dictionary is empty.')
else:
print(dir)

Output:

Explanation:

The condition will return the negation of the if block. The if with not operator will return either true or false based on the condition assigned to the statement. Hence, the output will be ''Dictionary is empty."

Check if element present in Dictionary:

Code Snippet:

dict = {'rose':'Red', 'lotus':'white'}
a = 'rose'
if not a in dict:
    print('No ', a ,'Not in dictionary', dict)
else:
    print('Yes ', a ,'in dictionary', dict)

Output:

Example 6: if not with Set:

Code Snippet:

s = set({})
if not s:
    print('Set is empty.')
else:
    print(s)

Output:

Explanation:

The condition will return the negation of the if block. The if with not operator will return either true or false based on the condition assigned to the statement. Hence, the output will be ''Set is empty."

Check if element present in set =

Code Snippet:

set =  {3,43,45,44,23}
a = 23
if not a in set:
    print('No ', a ,'Not in set', set)
else:
    print('Yes ', a ,'in set', set)

Output:

Example 7: if not with Tuple:

Code Snippet:

tup = tuple()

if not tup:
    print('Tuple is empty.')
else:
print(tup)

Output:

Explanation:

The condition will return the negation of the if block. The if with not operator will return either true or false based on the condition assigned to the statement. Hence, the output will be ''Tuple is empty."

Check if element present in tuple

Code Snippet:

tup = {'Apple', 'Banana', 'Orange'}
a = 'Banana'

if not a in tup:
    print('No ', a ,'Not in Tuples', tup)
else:
    print('Yes ', a ,'in Tuples', tup)

Output:

Example 8: if not with multiple conditions with integer:

Code Snippet:

x = 5
y = 5
if not ((x > 10) & (y > 10)):
    print("No: x & y NOT Greater then 10")
else:
    print("Yes: x & y Greater then 10")

Output:

Explanation:

Here, we have assigned two values to two variables. The if with not operator will return either true or false based on the condition assigned to the statement.

Here, we used this conditional statement with two integers and set conditions to check whether the value assigned to the variables "x" and "y" are greater than ten. The condition will return the negation of the if block.

Conclusion:

After a clear explanation of all code snippets showing how the if-not conditional statement works in Python, we conclude that the if-not statement always negates the output of the if statement.

Based on the condition assigned, it will either return the "if not" part or the "else" part of the code block. Also, we have shown how if-not statement works with Integers, Boolean, String, Tuple, List, Dictionary, and Set.


×