Python If with NOT Operator Examples

Today, We want to share with you python if not.In this post we will show you how to use not in python, hear for if not python we will give you demo and example for implement.In this post, we will learn about list to string python with an example.

Python If with NOT Operator

I can use logical not operator with Python Example IF condition. The statements inside if block code execute only if the value(boolean) is False or if the value(collection) is not blank.

Sytnax

The syntax of Example If statement with NOT logical operator is

if not value:
    statement(s)

where the data value could be of data type boolean, string, list, dict, set, etc.

If data value is of boolean data type, then NOT acts a negation operator. If data value is False, not value would be True, and the statement(s) in if-block code will execute. If data value is True, not value would be False, and the statement(s) in if-block code will not execute.

Read Also:  python remove newline from string - how to remove newline from string in python?

If data value is of data type string, then statement(s) in if-block code will execute if string is null.

If value is of type list, then statement(s) in if-block code will execute if list is clear. The same simplification holds correct for data value of other collection datatypes: dict, set and tuple.

In summary, I can use if not declaration to conditionally execute a block code of statements only if the data value is not null or not False.

Example 1: if not – Boolean

In this program, I will use Python Example not logical operator in the boolean declaration of Python Example IF.

Program

flag = False

if not flag:
	print('flag is false.')

Results

flag is false.

Example 2: Python if not – String

In this program, I will use Python Example if not declaration to dump the string only if the string is not blank.
Program

flag = ''

if not flag:
    print('String is blank.')
else:
    print(flag)

Results

String is blank.

Example 3: Python if not – List

In this program, I will use Python Example if not declaration to dump the list only if the list is not clear.

Read Also:  how to use sum function in python?

Program

flag = []

if not flag:
    print('List is blank.')
else:
    print(flag)

Results

List is blank.

Example 4: Python if not – Dictionary

In this program, I will use Python Example if not declaration to dump the dictionary only if the dictionary is not blank.
Program

flag = dict({})

if not flag:
    print('Dictionary is blank.')
else:
    print(flag)

Results

Dictionary is blank.

Example 5: Python if not – Set

In this program, I will use Python Example if not declaration to dump the set, only if the set is not blank.
Program

flag = set({})

if not flag:
    print('Set is blank.')
else:
    print(flag)

Results

Set is blank.

Example 6: Python if not – Tuple

In this program, I will use Example if not declaration to dump the tuple, only if the tuple is not blank.

Read Also:  Python programs for printing pyramid patterns

Program

flag = tuple()

if not flag:
    print('Tuple is blank.')
else:
    print(flag)

Results

Tuple is blank.

I hope you get an idea about not logical operator along with if conditional statement.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.