Simplifying If Not Logical Operation in Python

If not Python is a logical operator which is present in pre-built statements. Operators help in performing different functions or actions that are important to get a result. Python allows us to perform different actions.

What is an If Not Python Operation?

Python If Not Operator is a combination of ‘if’ and ‘not’ statements. ‘if’ keyword allows you to check if the condition of the next statements is True or False and then act accordingly. Whereas the ‘not’ keyword negates the value of boolean of statements in front of it. Moreover, the ‘if not’ statement allows you to avoid the ‘else’ keyword many times.

In this topic, we will be discussing the if-not operator. It can operate on boolean, string, list, dictionary, and sets. We can obtain negative values using the operator. This means that when we use “if not” and the output is true, then the result is shown as false. The same happens for vice-versa. In other words, if the value is false the not value will be true and if the value is true then the not value will be false.

This entire situation happens for the Boolean type. Let us take a string expression. Here the operator will only function when string values are empty. This is similar to the list as well. If the list is empty, only then the operator can perform.

Some of operators are:

  • Arithmetic operators – They help in arithmetic calculations like addition, subtraction, multiplication, and division.
  • Assignment operators – We can use this to assign a value to a variable.
  • Comparison operators – They can compare two or more variables and obtain a relative result.
  • Logical operators – They can perform logical operations based on logic.
  • Identity operators – They are used to compare two objects if they belong to the same memory area.
  • Membership operators – They can test if a sequence is present in the program.
  • Bitwise operators – It checks for the relation in binary values.

How do you Write if not Condition in Python?

The following syntax refers to how the interpreter expects to parse if not condition in your program:

if<space>not<space>condition:
<indentation>statement

# Example:

if not x==5:
    print("x is not 5")

Source: Python Official Documentation

Syntax

if not value:
    statement(s)

Parameter

Not applicable.

Examples of If Not Statement in Python

1. If Not Python on Boolean

Boolean values are usually 0 and 1.Zero can be written as false and 1 means true in a programming code.

SYNTAX

CHECK = False

if not CHECK:
       print('false.')
 
CHECK = 5

if not CHECK==5:
       print('CHECK is not 5')
else:
       print('CHECK is 5')

OUTPUT

false
CHECK is 5

2. If Not Python on String

String refers to the set of zeros or sequence of text. We store strings in double-quotes.

SYNTAX

string = ""
 
if not string:
    print('String is empty.')
else:
    print(string)

OUTPUT

String is empty

3. If Not on a List

The list can store multiple data in a single variable. The list is usually very versatile in Python as it has multiplet set of data of varied types.

SYNTAX

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

OUTPUT

List is empty

4. If Not Python on Dictionary

It is a collection of unordered and changed data. The dictionary uses index value to recognize the data. It uses the logical operator to check if the dictionary is empty. Dictionary values contain keys and values. We write them in braces.

SYNTAX

val = dict({})

if not val:
    print('Dictionary is empty.')
else:
    print(val)

OUTPUT

Dictionary is empty.

5. If Not on Set

Set is different from the dictionary. It is a collection of unordered data. They do not contain any index value. We can write to them in curly braces. We cannot use the index using a key or index value. The values in the set are usually used along with the loop. The set allows us to add and update values.

SYNTAX

val = set({})

if not val:
    print('Set is empty.')
else:
    print(a)

OUTPUT

Set is empty.

6. If Not Python on Tuple

The tuples are same as that of lists. They are a finite list of values. The sequence is in order and immutable.

SYNTAX

Val = tuple()

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

OUTPUT

Tuple is empty.

FAQs

Is != Valid in Python?

Yes. != is an expression for ‘not equals’ in layman terms. Moreover, you can use the ‘not’ keyword to keep your code clean and remove any existing ‘!=’.

Is not and != in Python same?

Technically No. != checks if the two objects have the same value, whereas is not keyword compares the memory of two objects.

Is not equal to Python?

Is not is a famous keyword that is used instead of != in Python. It compares the memory locations of two objects and returns True if those are the same. Beware: Is not checks the memory while != checks the value. Choose Wisely!

See Also

Conclusion

The if-not clearly shows the versatile nature of Python as a language. Without this logical operator, it would become difficult to check if the list, string, or tuple is empty.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments